I am using php while loop and want to get value for ajax. my code is
while{
<form class="commentform">
<input type="hidden" class="proid" name="proid[]" value="<?=$rr['id']?>">
<input type="text" class="form-control wac" name="comval[]" placeholder="Write a comment..">
</form>
}
<script type="text/javascript">
$(document).ready(function(){
$('.commentform').on('submit', function(e){
var comment = $(this).next('.proid').attr('value');
$('#res').html(comment);
alert(comment);
return false;
<script>
please guide to get value in while loop for ajax on form submit. Thanks in advance
You need to use .find()
/.children()
as input
is a child not a sibling. Additionally use .val()
to get the current value
var comment = $(this).find('.proid').val();
instead of
var comment = $(this).next('.proid').attr('value');
You could also serialize all the inputs of the current form and run through each one like this :
$('.commentform').on('submit', function(e){
var formInputs = jQuery(this).serializeArray();
jQuery.each(formInputs, function(i, field){
console.log(field.name + " : " + field.value);
});
});