0

Hello there i want to ask how i can get print the $POST value from HTML from after Javascript post?

Here is my HTML submit forms:

<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="./comments.js"></script>
</head>
<div id="addCommentContainer">
<form class="add-comment-form" id="addCommentForm3" method="post" action="">
<input type="hidden" value="3" name="comentonpost" id="comentonpost" />
<textarea class="commentinput" name="body" id="body" cols="20" rows="5"></textarea>
<input type="button" class="add-comment-submit" value="Submit" />
</form>
</div>
<div id="addCommentContainer">
<form class="add-comment-form" id="addCommentForm3" method="post" action="">
<input type="hidden" value="3" name="comentonpost" id="comentonpost" />
<textarea class="commentinput" name="body" id="body" cols="20" rows="5"></textarea>
<input type="button" class="add-comment-submit" value="Submit" />
</form>
</div>

Here is my Javascript:

$(document).ready(function () {

$(".add-comment-submit").on("click", function (e) {



    e.preventDefault();



    var submit = $(this);



    if (submit.hasClass("working")) return false;



    submit.addClass("working");

    submit.val("Working...");



    $.post('submit.php',$(this).serialize(),function(msg){

        submit.removeClass("working");

        submit.val("Submit");

        $("<div></div>").html(msg).fadeIn("slow").insertBefore(submit.parent($(".addCommentContainer"))).slideDown();

    });

});

});

Here is my submit.php which is printing "Hello World" after submit:

<?PHP 
$message = 'Hello World';
echo $message;
?>

I don't know why but it seems it's not posting the information on submit.php or i am at wrong somewhere because when i try this in submit.php:

<?PHP 
$message = $_POST["body"];
echo $message;
?>

It's not printing what is typed in the text area with name "body". How i make it print what is writen in the body input text area?

Thanks in advance!

1 Answers1

0

You are using this inside the trigger, but the trigger is on the button, not the form, so this references the button. This also means that you are serializing the button not the form. Try this POST call in stead:

$.post('submit.php',$(this).parent().serialize(), ...

EDIT:

By the way, it is usually better practice to add the trigger to the form, e.g. as explained here.

Community
  • 1
  • 1
MaX
  • 1,765
  • 13
  • 17