如何将ajax帖子连接到PHP文件中的特定条件?

时间:2015-05-22 18:07:43

标签: javascript php jquery mysql ajax

所以我在我使用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文件?

非常感谢你,非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

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

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

您还需要设置内容类型

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