[英]sending data from $.ajax to php
I am trying to send the username to php
using ajax
. 我试图使用
ajax
将用户名发送到php
。
Here is my js
. 这是我的
js
。
$('#username').blur(function(){
var username = $("#username").val();
$.ajax({
type: "POST",
url: "dbquery.php",
data: username,
success: function(result){
$("#dbdata").html(result);
}
});
});
And here is php. 这是php。
$value = $_GET['username'];
I know it is about the ajax
because everything worked fine when the ajax
part was written in pure javascript
. 我知道它是关于
ajax
因为当ajax
部分是用纯javascript
编写的时候一切正常。
将
data
作为对象发送为$_GET
通过URL参数读取传递给当前脚本的关联变量数组
data: {username:username},
You are using type:"POST"
and trying to retrieve the values in GET Array in the PHP code .Change the method to GET
您正在使用
type:"POST"
并尝试在PHP代码中检索GET数组中的值。将方法更改为GET
$('#username').blur(function(){
var username = $("#username").val();
$.ajax({
type: "GET",
url: "dbquery.php",
data: username,
success: function(result){
$("#dbdata").html(result);
}
});
});
Use below code 使用以下代码
$('#username').blur(function(){
var username = $("#username").val();
var dataString = 'username ='+ encodeURIComponent(username) ;
$.ajax({
type: "POST",
url: "dbquery.php",
data: dataString ,
success: function(result){
$("#dbdata").html(result);
}
});
});
First: You are using POST in ajax. 第一:你在ajax中使用POST。 and GET in PHP, which is wrong.
和PHP中的GET,这是错误的。
If we use POST then in the PHP file, we use $_POST[”] to get the value.
如果我们在PHP文件中使用POST,我们使用$ _POST [“]来获取值。 If we use GET then we use $_GET[]
如果我们使用GET,那么我们使用$ _GET []
Second: data is passed as an object as data: {username: username}
. 第二:数据作为
data: {username: username}
传递data: {username: username}
。 This means that if we use $_POST['username']
in the PHP file, we will get the value of the username. 这意味着如果我们在PHP文件中使用
$_POST['username']
,我们将获得用户名的值。 Your final code will be 你的最终代码将是
AJAX AJAX
$('#username').blur(function(){
var username = $("#username").val();
$.ajax({
type: "POST",
url: "dbquery.php",
data: {username:username},
success: function(result){
$("#dbdata").html(result);
}
});
});
PHP PHP
$value = $_POST['username']
Please pass data as an Object. 请将数据作为对象传递。 To access data in PHP you have to use $_POST['username'], $_POST['password'] etc
要在PHP中访问数据,您必须使用$ _POST ['username'],$ _POST ['password']等
$('#username').blur(function(){
var data = {
username:$("#username").val(),
password: $("#password").val()
};
$.ajax({
type: "POST",
url: "dbquery.php",
data: data,
success: function(result){
$("#dbdata").html(result);
}
});
})
first of all your ajax type is post but you getting value using GET[] This is mistake here. 首先你的ajax类型是post但你使用GET []获得价值这是错误的。
try this 尝试这个
$('#username').blur(function(){
var username = $("#username").val();
$.ajax({
type: "POST",
url: "dbquery.php",
data: {'username':username},
success: function(result){
$("#dbdata").html(result);
}
}); });
and you have to use this to get value 你必须用它来获得价值
$value = $_POST['username'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.