我无法获取我的jquery ajax帖子来连接我的php页面

时间:2012-12-23 00:05:54

标签: php jquery html ajax

php
<?php

include 'connection.php';
echo "reached page";
$stmt = $conn->prepare("INSERT INTO flagged ( ID,URL,COUNT,comment)VALUES ( :id,    :url, :count,:COMMENT )"); 
$stmt->bindParam(':id', $id);
$stmt->bindParam(':url', $url);
$stmt->bindParam(':count', $count);
$stmt->bindParam(':COMMENT', $comment);

$id = NULL;
$url = $_POST['url']);
$count = 1;
$comment = $_POST['data']);
$stmt->execute();

?>

jquery的

$(document).ready(function() {
$('#flagForm').submit(function(){
return false;
});
$('#flag').click(function(){
    $('#comment').show();
});
$('body').on('click', '#commentSubmit', function(e) {
    $.post('flag.php',
    {
        data:$('#comment').val(),
        url:(window.location)
    },
    function(response){
        alert(response);
    });
});
});

HTML

<div id='comment'><h1 ></h1>
    <form id="flagForm" action="flag.php" method="post">
    <textarea id='commentData' placeholder='whats the problem' ></textarea>
    <input type="submit" id='commentSubmit' />
</form>
</div>

我似乎无法让我的帖子到达我的php页面。我之前从未遇到过这个问题。对象是用户标记页面。我有另一个post方法和php页面使用相同的连接,所以不能这样。任何帮助,将不胜感激。 jquery和html还有更多内容,但这与问题有关。

2 个答案:

答案 0 :(得分:2)

#comment是div,您必须使用数据在输入/ textarea上调用.val()

data:$('#commentData').val(),

我发现您在此处收到了非法调用错误url:(window.location)而是使用window.location.href

window.location是jQuery无法序列化的对象,因此您会收到该错误。

答案 1 :(得分:1)

你的PHP代码有点乱。而不是:

include 'connection.php';
echo "reached page";
$stmt = $conn->prepare("INSERT INTO flagged ( ID,URL,COUNT,comment)VALUES ( :id,    :url, :count,:COMMENT )"); 
$stmt->bindParam(':id', $id);
$stmt->bindParam(':url', $url);
$stmt->bindParam(':count', $count);
$stmt->bindParam(':COMMENT', $comment);

$id = NULL;
$url = $_POST['url']);
$count = 1;
$comment = $_POST['data']);
$stmt->execute();

你应该这样做:

include 'connection.php';

echo "reached page";

$id = NULL;
$url = $_POST['url']);
$count = 1;
$comment = $_POST['data']);

$stmt = $conn->prepare("INSERT INTO flagged ( ID,URL,COUNT,comment)VALUES ( :id,    :url, :count,:COMMENT )"); 
$stmt->bindParam(':id', $id);
$stmt->bindParam(':url', $url);
$stmt->bindParam(':count', $count);
$stmt->bindParam(':COMMENT', $comment);

$stmt->execute();

当然,你也不应该直接将用户输入插入到数据库中,但我认为这不是一个真正的应用程序......

相关问题