I am trying to get 2 variables from ajax in php. With one variable its working fine. New to ajax, so I am not sure how will I include a second variable. As of now I am getting the msg_count with out any issues. My ajax script is below:
我在用php从ajax中获取两个变量。有一个变量就可以了。ajax是新手,所以我不确定如何包含第二个变量。到目前为止,我得到的msg_count没有任何问题。我的ajax脚本如下:
function addmsg(type, msg) {
$('#msg_count').html(msg);
}
function waitForMsg() {
$.ajax({
type: "GET",
url: "notification/select.php",
async: true,
cache: false,
timeout: 50000,
success: function(data) {
addmsg("new", data);
setTimeout(
waitForMsg,
1000
);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
addmsg("error", textStatus + " (" + errorThrown + ")");
setTimeout(
waitForMsg,
15000);
}
});
};
$(document).ready(function() {
waitForMsg();
});
select.php script is below:
选择。php脚本如下:
$sql = "SELECT * from notification where tousername='$tousername' and isread = 0";
$result = $con->query($sql);
$row = $result->fetch_assoc();
$count = $result->num_rows;
echo $count;
$not=$row['notification'];
echo $not;
I am able to pass the $count properly. I need to pass $not also to the ajax. How will I do that?
我能够正确地通过$count。我需要将$not传递给ajax。我该怎么做呢?
My edited php script to use it with a WHILE Loop is as follows:
我编辑的php脚本使用它与一个WHILE循环如下:
$result= mysqli_query($con,"SELECT * from notification where tousername='$tousername' and isread = 0");
while($row = mysqli_fetch_array($result)) {
$count = $result->num_rows;
$not=$row['notification_msg'];
$res=[];
$res['count'] = $count;
$res['not'] = $not;
echo json_encode($res);
1 个解决方案
#1
3
Like @guradio said, set dataType : 'json'
inside ajax properties and json_encode
data that you want to pass into success block like following code :
如@guradio所说,将数据类型设置为“json”,在ajax属性和json_encode数据中,您希望将其传递到成功块,如以下代码:
$.ajax({
....
....
dataType : 'json', // added here
success : function ( data ) {
// access data from response
// access it using data.count, data.not
console.log(data)
// just called like original code
// passed on result `data`
addmsg( type, data );
// the rest of the code
}
...
});
function addmsg(type, msg){
// access it using msg.count, msg.not
console.log(msg.count)
$('#msg_count').html(msg);
}
In Php :
在Php中:
$sql = "SELECT * from notification where tousername='$tousername' and isread = 0";
$result = $con->query($sql);
$row = $result->fetch_assoc();
$count = $result->num_rows;
$not=$row['notification'];
// added here
echo json_encode( array( 'count' => $count, 'not' => $not ) );
Edited : This depend on how you want to store the data and populate it
编辑:这取决于您希望如何存储和填充数据
// defined container outside loop
$res = [];
while($row = mysqli_fetch_array($result)) {
$count = $result->num_rows;
$not=$row['notification_msg'];
array_push( $res, array( 'count' => $count, 'not' => $not ) );
}
echo json_encode($res);
Suggestion(credited to guradio):
建议(归功于guradio):
Must be noted that, there is not necessary to add async : true
inside ajax properties as the default behavior of Ajax is asynchronous and the default value of that is TRUE
unless you wanna it to be false, but not recommended.
必须指出的是,没有必要添加async:在ajax属性中,ajax的默认行为是异步的,默认值为true,除非您希望它是假的,但不建议这样做。
#1
3
Like @guradio said, set dataType : 'json'
inside ajax properties and json_encode
data that you want to pass into success block like following code :
如@guradio所说,将数据类型设置为“json”,在ajax属性和json_encode数据中,您希望将其传递到成功块,如以下代码:
$.ajax({
....
....
dataType : 'json', // added here
success : function ( data ) {
// access data from response
// access it using data.count, data.not
console.log(data)
// just called like original code
// passed on result `data`
addmsg( type, data );
// the rest of the code
}
...
});
function addmsg(type, msg){
// access it using msg.count, msg.not
console.log(msg.count)
$('#msg_count').html(msg);
}
In Php :
在Php中:
$sql = "SELECT * from notification where tousername='$tousername' and isread = 0";
$result = $con->query($sql);
$row = $result->fetch_assoc();
$count = $result->num_rows;
$not=$row['notification'];
// added here
echo json_encode( array( 'count' => $count, 'not' => $not ) );
Edited : This depend on how you want to store the data and populate it
编辑:这取决于您希望如何存储和填充数据
// defined container outside loop
$res = [];
while($row = mysqli_fetch_array($result)) {
$count = $result->num_rows;
$not=$row['notification_msg'];
array_push( $res, array( 'count' => $count, 'not' => $not ) );
}
echo json_encode($res);
Suggestion(credited to guradio):
建议(归功于guradio):
Must be noted that, there is not necessary to add async : true
inside ajax properties as the default behavior of Ajax is asynchronous and the default value of that is TRUE
unless you wanna it to be false, but not recommended.
必须指出的是,没有必要添加async:在ajax属性中,ajax的默认行为是异步的,默认值为true,除非您希望它是假的,但不建议这样做。