gpt4 book ai didi

javascript - 如何将 ajax post 连接到 PHP 文件中的特定条件?

转载 作者:行者123 更新时间:2023-11-29 22:24:26 25 4
gpt4 key购买 nike

因此,我在使用 AJAX 的 html 文件中编写了一个名为 onclick 的函数,但我希望这篇文章能够转到 mysql 中的特定表。

   $('#submitIFC').click(function(e) {

var request;

if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
var opinionIFC = $('ul.sort').sortable('toArray').join(',');

request.onreadystatechange = function() {

if ((request.readyState===4) &&(request.status===200)) {

var return_data = request.responseText;
document.getElementById('rank_ul').innerHTML= 'return_data';


// Preventing the default action triggered by clicking on the link
e.preventDefault();


e.preventDefault();
}//end of if
}//end of onreadystatechange function


//send requested movie to php file which will send to the external server
request.open("POST", "results.php", true);
request.send(opinionIFC);

document.getElementById('rank_ul').innerHTML='<img src="ajax-loader.gif">';

});

但是,如果有条件,将其连接到我的 php 似乎存在问题,我尝试复制 request.send() 上的内容,如下所示

   if($_POST['opinionIFC'])
{echo
// The data arrives as a comma-separated string,
// so we extract each post ids:
$data=explode(',',str_replace('li','',$_POST['sortdata']));

// Getting the number of objects
list($tot_objects) = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM sort_objects"));

if(count($data)!=$tot_objects) die("Wrong data!");

foreach($data as $k=>$v)
{
// Building the sql query:
$str[]='('.(int)$v.','.($tot_objects-$k).')';
}

$str = 'VALUES'.join(',',$str);

// This will limit voting to once a day per IP:
mysql_query(" INSERT INTO `sort_votes` (ip,date_submit,dt_submit)
VALUES ('".$_SERVER['REMOTE_ADDR']."',NOW(),NOW())");

// If the user has not voted before today:
if(mysql_affected_rows($link)==1)
{
mysql_query(' INSERT INTO `sort_objects` (id,votes) '.$str.'
ON DUPLICATE KEY UPDATE votes = votes+VALUES(votes)');
}

}

为什么 ajax post 请求没有过滤到我的 php 文件?

非常感谢您,非常感谢您的帮助。

最佳答案

您没有发送opinionIFC参数,请尝试:

request.send('opinionIFC=' + opinionIFC);

您还需要设置Content-type

request.setRequestHeader("Content-type","application/x-www-form-urlencoded");

关于javascript - 如何将 ajax post 连接到 PHP 文件中的特定条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30403208/

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