Paul Paul - 10 months ago 33
Ajax Question

PHP file not recognizing data sent from ajax file

I am attempting to create an ajax call to my php file to insert data. The issue I am running into is the data I send over is not being recognized.

The variables you will see in the php file is what is not being recognized:

$home_comment = $_POST['home_comment'];
$username = $user->data()->username;

I am running an ini file that has the connection
within it and it always has the

Does anyone see what I am doing wrong?

<form action="" method="POST" id="comment-form">
<label for="comment">Comment</label>
<textarea cols="15" id="home-comment" name="comment" placeholder="Message" rows="5" maxlength="1000" required></textarea><br>
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
<input id="comment-button" name="submit" type="submit" value="Post">


$("#comment-form").on("submit", function (event) {

var home_comment = $("#home_comment").val();

url: "comment-send.php",
type: "POST",
data: {
"home_comment": home_comment
success: function (data) {
// console.log(data); // data object will return the response when status code is 200
if (data == "Error!") {
alert("Unable to post comment!");
} else {
error: function (xhr, textStatus, errorThrown) {
alert(textStatus + " | " + errorThrown);
//console.log("error"); //otherwise error if status code is other than 200.

PHP file:

$home_comment = $_POST['home_comment'];
$username = $user->data()->username;
$okay = true;

if ( $okay ) {

$comment_insert = "
INSERT INTO home_comments
(id, user_id, username, comment)
VALUES(?, ?, ?, ?)
$comment_stmt = $con->prepare($comment_insert);
$comment_stmt->execute(array('', $user_id, $username, $home_comment));

Answer Source

Check this:

here the id: home-comment with a hyphen

var home_comment = $("#home_comment").val();

here you are referring it with underscore

Resolve this and try again