[英]Posting button values using ajax
I am trying to post a value to a php script using post method in ajax, however I am getting我正在尝试使用 ajax 中的 post 方法将值发布到 php 脚本,但是我得到了
Notice: Undefined index: gamekey
注意:未定义索引:gamekey
The following is my JavaScript code以下是我的 JavaScript 代码
function adminchat(row) {
var to_user_id = $('#start_chat_' + row).data('key_' + row);
var dataString = 'gamekey=' + to_user_id;
fetch_chat()
setInterval(function () {
fetch_chat();
}, 5000)
setInterval(function () {
autoRefresh_div();
}, 1000);
function fetch_chat() {
$.ajax({
url: 'admin_dispute_handler.php',
data: dataString,
type: 'post',,
success: function () {
window.location = 'admin_dispute_handler.php';
},
error: function (request, status, error) {
alert(error);
alert(status);
alert(request);
}
});
}
function autoRefresh_div() {
$("#result").load("load.php").show(); // a function which will load data from other file after x seconds
}
};
Below is the html:下面是html:
<button type="button" class="btn btn-info btn-xs start_chat" id="start_chat_1" onclick="adminchat(1)" data-key_1="34354661" data-toggle="modal" data-target="#exampleModal">Start Chat</button>
The section of the php code giving me issue:给我问题的 php 代码部分:
<?php
if ($_SESSION['logged_in']==FALSE) {
header("Location:http://localhost/the_site/login/login.html");
} else {
require_once 'connection.php';
$conn = new mysqli(host,user,password,db);
session_start();
$Game_key = $_POST['gamekey'];
$_SESSION['gamekey'] = $_POST['gamekey'];
$username = $_SESSION['username'];
$query = "SELECT gamername,score From ongoing_game_list Where Game_Key = $Game_key";
$result = $conn->query($query);
/* numeric array */
$row = $result->fetch_array(MYSQLI_ASSOC);
$gamername = $row["gamername"];
$score = $row["score"];
}
?>
First, you have a syntax error on type: 'post',,
, browser should raise an error message on developer console.首先,您在
type: 'post',,
上有语法错误,浏览器应该在开发人员控制台上引发错误消息。 And use object as param in .ajax()
:并在
.ajax()
使用 object 作为参数:
$.ajax({
url: 'admin_dispute_handler.php',
data: {
gamekey: to_user_id
},
// Original is: type: 'post',,
type: 'post',
success: function () {
window.location = 'admin_dispute_handler.php';
},
error: function (request, status, error) {
alert(error);
alert(status);
alert(request);
}
});
You can learn more about jQuery AJAX usage at official docs example section .您可以在官方文档示例部分了解有关 jQuery AJAX 用法的更多信息。
If you facing issue after redirecting then try below solution如果您在重定向后遇到问题,请尝试以下解决方案
$headers = apache_request_headers();
$is_ajax = (isset($headers['X-Requested-With']) && $headers['X-Requested-With'] == 'XMLHttpRequest');
here you got $is_ajax
true if send request using AJAX
other wise return false(null).在这里,如果使用
AJAX
发送请求,则$is_ajax
true,否则返回 false(null)。
Hope this help to you.希望这对你有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.