通过ajax/json发送两个变量

时间:2023-01-03 18:13:47

I tried to send two variables via ajax/json but something gone wrong.

我试图通过ajax/json发送两个变量,但是出了问题。

first:

第一:

var searchbox = $(this).val();

var datastring = 'searchword='+ searchbox;

second:

第二:

<?php $lang = $_GET[lang]; ?>

ajax/json:

ajax / json:

$.ajax({
    type: "POST",
    url: "files/search.php",
    data: dataString+"&lang=<?php $lang ?>",
    cache: false,
    success: function(html)
    {
    ...(etc)

on search.php page I need:

在搜索。我需要php页面:

$searchrequest = $_POST['searchword'];

$lang = $_POST['lang'];

and $searchrequest works, but $lang don't use transfered data.

$searchrequest可以工作,但是$lang不使用转移数据。

I tried to make Array of this two data and others, but nothing. Can some help or tell better way to do this?

我试着把这两个数据和其他数据组成数组,但是什么都没有。有什么可以帮助或者告诉更好的方法吗?

2 个解决方案

#1


2  

Use a string to access the array fields:

使用一个字符串访问数组字段:

<?php $lang = $_GET['lang']; ?>

and you have to echo the value contained in $lang:

你必须重复$lang中包含的值:

 data: dataString+"&lang=<?php echo $lang ?>",

But if the string in $lang contains any other unsafe URI character (most likely not as you are getting it from $_GET, but still) you have to use the escpape function:

但是,如果$lang中的字符串包含任何其他不安全的URI字符(很可能不是从$_GET中获得的,但仍然是),则必须使用escpape函数:

data: dataString+"&lang=" + escape('<?php echo $lang ?>'),

or even better (also encodes searchbox properly:

或者更好(也可以正确编码searchbox:

data: {searchword: searchbox, lang: "<?php echo $lang ?>"},

Alternatively, you can send the variable via GET by appending it to the URL:

另外,您也可以通过GET将变量附加到URL:

url: "files/search.php?lang=<?php echo $lang ?>",

#2


1  

This code you used:

你使用这段代码:

<?php $lang ?>

Does not print anything. You need to

不打印任何东西。你需要

<?php echo $lang; ?>

#1


2  

Use a string to access the array fields:

使用一个字符串访问数组字段:

<?php $lang = $_GET['lang']; ?>

and you have to echo the value contained in $lang:

你必须重复$lang中包含的值:

 data: dataString+"&lang=<?php echo $lang ?>",

But if the string in $lang contains any other unsafe URI character (most likely not as you are getting it from $_GET, but still) you have to use the escpape function:

但是,如果$lang中的字符串包含任何其他不安全的URI字符(很可能不是从$_GET中获得的,但仍然是),则必须使用escpape函数:

data: dataString+"&lang=" + escape('<?php echo $lang ?>'),

or even better (also encodes searchbox properly:

或者更好(也可以正确编码searchbox:

data: {searchword: searchbox, lang: "<?php echo $lang ?>"},

Alternatively, you can send the variable via GET by appending it to the URL:

另外,您也可以通过GET将变量附加到URL:

url: "files/search.php?lang=<?php echo $lang ?>",

#2


1  

This code you used:

你使用这段代码:

<?php $lang ?>

Does not print anything. You need to

不打印任何东西。你需要

<?php echo $lang; ?>