I am trying to pass a single variable from javascript to PHP using jQuery AJAX.
我试图使用jQuery AJAX将单个变量从javascript传递给PHP。
Javascript:
使用Javascript:
$(document).ready(function() {
$('.ok').on('click', function(e){
var value = 5;
$.ajax({
url: 'tabulka.php',
type: "POST",
data: {x : value},
success: function(){
alert(value);
}
});
});
});
PHP:
PHP:
if($_SERVER['REQUEST_METHOD']){
$value = $_POST['x'];
echo $value;
}
I get the alert from ajax with the expected value but in PHP I get this error: Notice: Undefined index: value in C:\xampp\htdocs\test\tabulka.php on line 71
When I uncomment the dataType: 'json'
I don't even get the alert from AJAX.
我从ajax获得了具有预期值的警报但在PHP中我收到此错误:注意:未定义的索引:第71行的C:\ xampp \ htdocs \ test \ tabulka.php中的值当我取消注释dataType时:'json'I甚至没有从AJAX获得警报。
3 个解决方案
#1
1
Instead of this
而不是这个
data: ({value : value}),
Try using this,
试试这个,
data: "value="+value,
And you are not at all passing "OK" but you are receiving it. If you want to pass OK also as a parameter try this.
你并没有完全“好”,但是你正在接受它。如果你想传递OK作为参数试试这个。
data: "value="+value+"&OK=Somevalue",
That should work.
这应该工作。
#2
1
Your JavaScript
你的JavaScript
data: {'value' : value}
notice the single quote and no parenthesis
注意单引号而没有括号
Your PHP:
你的PHP:
If(isset($_POST['value'])){
$val = $_POST['value'];
}
Your JavaScript was formatted incorrectly. data
is an object, or an associative array in other words.
您的JavaScript格式不正确。数据是一个对象,换句话说就是一个关联数组。
Your PHP code has unnecessary string comparison. Whenever a post is made to a PHP script, you will access them using $_POST array.
您的PHP代码有不必要的字符串比较。每当发布一个PHP脚本的帖子时,您将使用$ _POST数组访问它们。
For instance, if you posted this via AJAX
例如,如果您通过AJAX发布此内容
data:{ 'name' : 'jake', 'age' : 1024}
You would access them in PHP by
你可以在PHP中访问它们
$name = $_POST['name']
$age = $_POST['age']
Also, it is wise to use PHP isset
to verify that a post variable exists before trying to access them. That way you will not have an exception if expected post data is not.
此外,在尝试访问它们之前,最好使用PHP isset验证post变量是否存在。这样,如果没有预期的后期数据,您就不会有例外。
#3
0
check following line
检查以下行
var value = $(this).find('td:first').html();
set some hard coded value for 'value' variable like below
为'value'变量设置一些硬编码值,如下所示
var value='some value';
if above works then check the value you assigned using $(this).find('td:first').html()
如果以上工作,然后使用$(this).find('td:first')检查您分配的值.html()
#1
1
Instead of this
而不是这个
data: ({value : value}),
Try using this,
试试这个,
data: "value="+value,
And you are not at all passing "OK" but you are receiving it. If you want to pass OK also as a parameter try this.
你并没有完全“好”,但是你正在接受它。如果你想传递OK作为参数试试这个。
data: "value="+value+"&OK=Somevalue",
That should work.
这应该工作。
#2
1
Your JavaScript
你的JavaScript
data: {'value' : value}
notice the single quote and no parenthesis
注意单引号而没有括号
Your PHP:
你的PHP:
If(isset($_POST['value'])){
$val = $_POST['value'];
}
Your JavaScript was formatted incorrectly. data
is an object, or an associative array in other words.
您的JavaScript格式不正确。数据是一个对象,换句话说就是一个关联数组。
Your PHP code has unnecessary string comparison. Whenever a post is made to a PHP script, you will access them using $_POST array.
您的PHP代码有不必要的字符串比较。每当发布一个PHP脚本的帖子时,您将使用$ _POST数组访问它们。
For instance, if you posted this via AJAX
例如,如果您通过AJAX发布此内容
data:{ 'name' : 'jake', 'age' : 1024}
You would access them in PHP by
你可以在PHP中访问它们
$name = $_POST['name']
$age = $_POST['age']
Also, it is wise to use PHP isset
to verify that a post variable exists before trying to access them. That way you will not have an exception if expected post data is not.
此外,在尝试访问它们之前,最好使用PHP isset验证post变量是否存在。这样,如果没有预期的后期数据,您就不会有例外。
#3
0
check following line
检查以下行
var value = $(this).find('td:first').html();
set some hard coded value for 'value' variable like below
为'value'变量设置一些硬编码值,如下所示
var value='some value';
if above works then check the value you assigned using $(this).find('td:first').html()
如果以上工作,然后使用$(this).find('td:first')检查您分配的值.html()