我无法得到我的jquery ajax帖子来连接我的php页面

时间:2021-12-25 08:06:54
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>

I can't seem to get my post to reach my php page. I have never had this problem before. The object is to flag a page by users. I have another post method and php page in using the same connection so that can't be it. Any help would be appreciated. And there is more to the jquery and html but this is all that is relative to the problem.

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

2 个解决方案

#1


2  

#comment is a div, you have to call .val() on the input/textarea with the data

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

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

I see you get an illegal invocation error here url:(window.location) use window.location.href instead .

我看到你在这里得到一个非法的调用错误url:(window.location)改为使用window.location.href。

window.location is an object that jQuery cant serialize so you get that error.

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

#2


1  

You PHP code is a bit out of order. Instead of this:

你的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();

You should be doing this:

你应该这样做:

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();

Of course, you also should not be inserting user input directly into a database, either, but I assume this is not a real application at this point...

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

#1


2  

#comment is a div, you have to call .val() on the input/textarea with the data

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

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

I see you get an illegal invocation error here url:(window.location) use window.location.href instead .

我看到你在这里得到一个非法的调用错误url:(window.location)改为使用window.location.href。

window.location is an object that jQuery cant serialize so you get that error.

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

#2


1  

You PHP code is a bit out of order. Instead of this:

你的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();

You should be doing this:

你应该这样做:

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();

Of course, you also should not be inserting user input directly into a database, either, but I assume this is not a real application at this point...

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