1

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 $con within it and it always has the $user variable.

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">
        </form>

Ajax:

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

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

        $.ajax({ 
            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!");
                    alert(data);
                } else {
                    /*$("#newsletter-form")[0].reset();
                    $('.newsletter-popup').fadeIn(350).delay(2000).fadeOut();*/
                }
            },
            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));
    }
8
  • 1
    Did you forget to return false; at the bottom of your submit? If it's not there and everything happens quickly you may not see a page flash and you've just submitted to the page you're on. Commented Oct 20, 2016 at 3:48
  • @PHPglue Could you please elaborate. Commented Oct 20, 2016 at 3:49
  • 1
    return false; after your AJAX function or a submit button will submit old school style. Commented Oct 20, 2016 at 3:50
  • 1
    Try uncommenting the commented out //event.preventDefault(); too Commented Oct 20, 2016 at 3:51
  • var home_comment = $("#home_comment").val(); vs <textarea cols="15" id="home-comment"... > See the issue? Maybe you should just serialize() the form to prevent tyops in the future. Hint: _ and - are not equal. Commented Oct 20, 2016 at 3:53

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.