如何通过ajax传递变量?

时间:2016-01-06 09:51:28

标签: php jquery ajax

我想通过AJAX请求将变量从post.php传递给addcomment.php。我已经尝试过下面的代码,但似乎没有用。它重新加载页面但没有任何反应,也没有数据插入数据库

post.php中

//variables to pass
$userid = $row['userid'];
$uid = $row['uid'];
$postid = $row['postid'];

<form method='post' name='form' action='' class='commentbox'>
<textarea name='content' id='content'></textarea><br />
<input type='submit' value='Comment' name='submit' class='comment_button'/>
</form>

<script type="text/javascript" >
    var userfrom = '<?php echo $uid ?>';
    var userto = '<?php echo $userid ?>';
    var postid = '<?php echo $postid ?>';

    $(function() {
        $(".comment_button").click(function() {
            var test = $("#content").val();
            var dataString = 'content='+ test;

            if (test == '') {
                alert("Please Enter Some Text");
            } else {
                $.ajax({
                    type: "POST",
                    url: "addcomment.php",
                    data: {    
                        dataString: dataString, 
                        userfrom: userfrom, 
                        userto: userto,
                        postid: postid 
                    }
                    cache: false,
                    success: function(html){
                        $(".display").show(html);
                    }
                });
            } 
            return false;
        });
    });
</script>

addcomment.php

    if (isset($_POST['dataString'], $_POST['userto'], $_POST['userfrom'], $_POST['postid'])) {
        $userid = $_POST['userto'];
        $uid = $_POST['userfrom'];
        $postid = $_POST['postid'];
        $comment = $_POST['dataString'];
        $com = $db->prepare("INSERT INTO comments (comment,userto, userfrom, post) VALUES (:comment, :userto, :userfrom, :post)");
        $com->execute(array(':comment'=>$comment,':userto'=>$userid,':userfrom'=>$uid,':post'=>$postid));
}

2 个答案:

答案 0 :(得分:2)

为userid,uid和postid创建隐藏字段并分配值。 在脚本(post.php)中获取值 var userfrom = $(“#uid”)。val(); 。它会工作

答案 1 :(得分:0)

请尝试下面的代码,只需替换下面的代码。

因为添加按钮类型提交而重新加载页面,对于ajax事件,您需要设置类型按钮,请检查以下代码:

<?php 
//variables to pass
$userid = $row['userid'];
$uid = $row['uid'];
$postid = $row['postid'];
?>
<form method='post' name='form' action='' class='commentbox'>
<textarea name='content' id='content'></textarea><br />
<input type='button' value='Comment' name='submit' class='comment_button'/>
</form>

<script type="text/javascript" >
    var userfrom = '<?php echo $uid ?>';
    var userto = '<?php echo $userid ?>';
    var postid = '<?php echo $postid ?>';

    $(function() {
        $(".comment_button").click(function() {
            var test = $("#content").val();
            var dataString = 'content='+ test;

            if (test == '') {
                alert("Please Enter Some Text");
            } else {
                $.ajax({
                    type: "POST",
                    url: "addcomment.php",
                    data: {    
                        dataString: dataString, 
                        userfrom: userfrom, 
                        userto: userto,
                        postid: postid 
                    },
                    cache: false,
                    success: function(html){
                        $(".display").show(html);
                    }
                });
            } 
            return false;
        });
    });
</script>