I am trying to display results from the process page on the host page. At the moment I keep getting the Uncaught ReferenceError: data is not defined error message. How do I go about fixing this?


$(document).ready(function () {
    // variable to hold request

    var request;
    // bind to the submit event of our form

    $("#commentForm").submit(function (event) {

        // abort any pending request
        if (request) {

        // setup some local variables
        var $form = $(this);

        // lets select and cache all the fields
        var $inputs = $form.find("input, select, button, textarea");

        // serialize the data in the form
        var serializedData = $form.serialize();

        // lets disable the inputs for the duration of the ajax request
        $inputs.prop("disabled", true);

        // fire off the requestp
        var request = $.ajax({
            url: "process_msg.php",
            type: "post",
            data: serializedData

        // callback handler that will be called on success
        request.done(function (response, textStatus, jqXHR) {

            // log a message to the console
            console.log("Hooray, it worked!");

            // flash update text

        // callback handler that will be called on failure
        request.fail(function (jqXHR, textStatus, errorThrown) {

            // log the error to the console

                "The following error occured: " + textStatus, errorThrown);

        // callback handler that will be called regardless
        // if the request failed or succeeded
        request.always(function () {

            // reenable the inputs
            $inputs.prop("disabled", false);

        // prevent default posting of form

<form id="commentForm">
    <textarea maxlength="255" placeholder="What's on your mind?" id="post_box"
    style="font-family: Verdana; border-color: #dedede" name="msg"></textarea>
    <br />
    <input type="hidden" name="user_id" value="<? echo $loggedin_id; ?>" />
    <input type="submit" id="post_btn" style="display: none" value="Post"
<div id='flashmessage'></div>


$msg = $_POST["msg"];
$user_id = $_POST["user_id"];

<div id="flashmessage">
<?php echo $msg; ?>


Member Avatar


Uncaught ReferenceError: data is not defined error message.

Line 40:


You didn't define flashmessage so that's why the error messages appear when you ran the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.