How I can get the value of input of form ONLY on submit? I have this..
<form id="addCommentForm<?PHP echo $PostID;?>" method="post" action="">
<input type="hidden" value="<?PHP echo $PostID;?>" name="comentonpost" id="comentonpost"/>
<textarea class="commentinput" name="body" id="body" cols="20" rows="5"></textarea>
<input type="submit" id="submit" value="Submit" />
</form>
$PostID is number which in every form printed in the page is different and so all form names on the page are different. In HTML, it's looking like this:
First form printed:
<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="submit" id="submit" value="Submit" />
</form>
Second printed:
<form id="addCommentForm2" method="post" action="">
<input type="hidden" value="2" name="comentonpost" id="comentonpost"/>
<textarea class="commentinput" name="body" id="body" cols="20" rows="5"></textarea>
<input type="submit" id="submit" value="Submit" />
</form>
I am getting the comentonpost id value in javascript like this:
$(document).ready(function(){
var name_element = document.getElementById('comentonpost');
var x = name_element.value;
});
I need this value so the following script will put the new comment over #addCommentContainer'+x
Here is the whole javascript:
$(document).ready(function(){
var name_element = document.getElementById('comentonpost');
var x = name_element.value;
/* The following code is executed once the DOM is loaded */
/* This flag will prevent multiple comment submits: */
var working = false;
/* Listening for the submit event of the form: */
$('#addCommentForm'+x).submit(function(e){
e.preventDefault();
if(working) return false;
working = true;
$('#submit').val('Working..');
$('span.error').remove();
/* Sending the form fileds to submit.php: */
$.post('comment.submit.php',$(this).serialize(),function(msg){
working = false;
$('#submit').val('Submit');
/*
/ If the insert was successful, add the comment
/ below the last one on the page with a slideDown effect
/*/
$(msg.html).hide().insertBefore('#addCommentContainer'+x).slideDown();
},'json');
});
});
But in this way it's taking the value of the first form printed. How can I get the value of comentonpost only on the submitted form?