gpt4 book ai didi

javascript - PHP 到 Ajax Post 使用 JQuery,全局事件有效,但 POST 似乎从未正确设置

转载 作者:行者123 更新时间:2023-12-02 17:53:03 25 4
gpt4 key购买 nike

我正在尝试使用 $.ajax 调用将数据发布到表单。当输入表单的值发生更改时会触发该事件。为了简单起见,我将所有内容都放在 C9.io 上,以便可以看到我的所有代码。

http://c9.io/noman2000/php-ajax

这是启动该函数的脚本:

<?php 
// Strictly for a demo, this isn't how it is actually written.
?>
<form>
<input type="text" id="user-name">
</form>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script> $jQ = jQuery.noConflict();</script>
<script type="text/javascript" src="ajaxGlobaltests.js"></script>
<script type="text/javascript">

$jQ(document).ready(function() {

/*
The DOMNodeInserted is a wonky event at best. This should be refactored to a function that is exited upon
completion and that uses SetInterval to prevent it from firing too often and killing page speed.
But this is just a demo.
*/
$jQ(document).on('DOMNodeInserted', function(e) {
var element = e.target;


var userName = $jQ('#user-name');
console.log(userName.val());
if(userName){
userName.on('change', function(){
$jQ(this).css('background-color', '#EEE');
$jQ.ajax({
url: 'submit.php',
type: 'post',
data: {action: userName.val()}
// .serialize() is used for multiple values, but I think this will work for singular.
}).done(function(){
console.log('It has been done.');
}); // end ajax call
return false;
});//End change
}//End if
}); //End DOM Node Insert
}); // End on Ready.
</script>

用于捕获此AJAX事件的submit.php脚本:

<?php
if($_POST['action'])
{
$user_info['username'] = 'test data';
?>
<script type="text/javascript">
// C9 registers and Error pushing between javascript and php, but this works fine in production.
console.log(<?php echo "The post data is {$_POST['action']} and the userID is {$user_info['username']}"; ?>);
</script>


<?php

if(class_exists('dbde_mysql')){
$uName_db = new dbde_mysql;
$uName_connect = mysqli_connect($uName_db->host, $uName_db->user, $uName_db->pass, $uName_db->db) or die("Error " . mysqli_error($uName_db));
$uName_str = "UPDATE users SET {$_POST['action']} WHERE id = {$user_id}";
$sql_res = mysqli_query($uName_db, $uName_str) or die("sql error: $uName_str\n");
}else{
// Future proofing. This ensures that a new database can be created if the class doesn't exist.
// uName is connected, not a problem. On this demo, yes, it obviously doesn't work.
?>

<?php
}


} // Post action field.
?>

我的全局 Ajax 事件寄存器都显示代码开始和执行,没有错误。 .done 事件也会触发控制台消息。然而,submit.php 中似乎没有任何作用。那么我的 $_POST['action'] 变量正确还是我在这里遗漏了其他内容?

最佳答案

尝试将其添加到 PHP 文件的顶部以查看您收到的内容:

//if HTML recd, otherwise handle for type Eg. die(json_encode($_POST['action']));
die( $_POST['action'] );

然后,在您的 .done() 函数中,显示您收到的内容:

.done(function(retd){
//If HTML received, otherwise handle for type
console.log('Received: ' + retd);
});

这至少可以让您看到 PHP 端收到的内容。

<小时/>

您可以在下面添加:

$num_rows = mysqli_num_rows($sql_res);
die(' Received rows: ' . $num_rows);

关于javascript - PHP 到 Ajax Post 使用 JQuery,全局事件有效,但 POST 似乎从未正确设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21192316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com