英文:
Posting button values using ajax
问题
我正在尝试使用Ajax的POST方法将一个值发送到PHP脚本,但是我得到了以下错误消息:
>注意:未定义索引:gamekey
以下是我的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(); // 在x秒后从其他文件加载数据的函数
}
};
以下是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">开始聊天</button>
引发问题的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);
/* 数值数组 */
$row = $result->fetch_array(MYSQLI_ASSOC);
$gamername = $row["gamername"];
$score = $row["score"];
}
?>
英文:
I am trying to post a value to a php script using post method in ajax, however I am getting
>Notice: Undefined index: gamekey
The following is my JavaScript code
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:
<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
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"];
}
?>
答案1
得分: 1
首先,你的代码中有一个语法错误,type: 'post',,
应该改为type: 'post',
,浏览器应该在开发者控制台中显示错误消息。并且在.ajax()
中使用对象作为参数:
$.ajax({
url: 'admin_dispute_handler.php',
data: {
gamekey: to_user_id
},
// 原始代码是:type: 'post',,
type: 'post',
success: function () {
window.location = 'admin_dispute_handler.php';
},
error: function (request, status, error) {
alert(error);
alert(status);
alert(request);
}
});
你可以在官方文档示例部分了解更多关于jQuery AJAX的用法。
英文:
First, you have a syntax error on type: 'post',,
, browser should raise an error message on developer console. And use object as param in .ajax()
:
$.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.
答案2
得分: 0
如果在重定向后遇到问题,请尝试以下解决方案:
$headers = apache_request_headers();
$is_ajax = (isset($headers['X-Requested-With']) && $headers['X-Requested-With'] == 'XMLHttpRequest');
在这里,如果使用 AJAX
发送请求,则$is_ajax
为真,否则返回假(null)。
希望这对您有所帮助。
英文:
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).
Hope this help to you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论