多个变量ajax到php


multiple variable ajax to php

简单明了,这就是我想要创建的:

function addComment($post_id, $user_id, $comment) {
    var post_id = $post_id;
    var user_id = $user_id;
    var comment = $comment;
    var xhr;
    if (window.XMLHttpRequest) xhr = new XMLHttpRequest();
    else xhr = new ActiveObject("Microsoft.XMLHTTP");
    var url = 'commentcreate.php?post_id=' + post_id + '&user_id=' + user_id + '&comment=' + comment
    xhr.open('GET', url, false);
    xhr.onreadystatechange = function () {
    if (xhr.readyState===4 && xhr.status===200) {
            div.innerHTML = xhr.responseText;           
        }
    }
    xhr.send();
    return false;

然后是PHP文件它将接收这些变量

if(isset($_GET['post_id']) && isset($_GET['user_id']) && isset($_GET['comment'])) $post_id = $_GET['post_id'] && $user_id = $_GET['user_id'] && $comment = $_GET['comment'];

是一段非常难读的代码意味着的工作方式如下:

if (
    isset($_GET['post_id'])
    && isset($_GET['user_id'])
    && isset($_GET['comment'])
) {
    $post_id = $_GET['post_id'];
    $user_id = $_GET['user_id'];
    $comment = $_GET['comment'];
}

所以基本上,我想要一个函数,需要3个变量,并将它们发送到一个php文件,在那里我可以做我的事情,并将它们添加到数据库(创建一个评论)。但这行不通。它工作与1个变量,但我不知道如何让它工作与更多的1。我猜这是错误的php方面,但我可能是错的。有什么建议吗?

尝试更改

if(isset($_GET['post_id']) && isset($_GET['user_id']) && isset($_GET['comment']))  
   $post_id = $_GET['post_id'] && 
   $user_id = $_GET['user_id'] && 
   $comment = $_GET['comment'];

if(isset($_GET['post_id']) && isset($_GET['user_id']) && isset($_GET['comment'])) {
   $post_id = $_GET['post_id'];
   $user_id = $_GET['user_id']; 
   $comment = $_GET['comment'];
 }

在你的php文件中尝试这样做

if($_GET['post_id'] && $_GET['user_id'] && $_GET['comment']) {
$post_id = $_GET['post_id'];
$user_id = $_GET['user_id'];
$comment = $_GET['comment'];
//your stuff
}