javascript - passing looping data through ajax data -


my form contains hidden input looping. in case, declare hidden input in ajax data manually without looping. how loop them in ajax data?

here's form script

<form method="post" name="myform">    <?php for($i=1;$i<=5;$i++) { ?>             <input type="hidden" name="data<?php echo $i; ?>" value="data<?php echo $i; ?>">    <?php } ?>    <input type='button' name='submitdata' value='submit' onclick='submitdata();'> </form> 

here's ajax script

function submitdata() {    var form = document.myform;    $.ajax({      url: 'process.php',      type: 'post',      data: {           data1 : form["data1"].value,           data2 : form["data2"].value,           data3 : form["data3"].value,           data4 : form["data4"].value,           data5 : form["data5"].value      },      success: function (result) {           console.log(result);      },      error: function () {           console.log("error");      }   }); } 

your hidden inputs have name , values,

use .serialize()

encode set of form elements string submission

data : $('form[name=myform]').serialize() 

this return name=value pairs.

if need {name:value}, use .each()

var formdata = {} $('form :input:hidden[name^="data"]').each(function(){     formdata[this.name] = this.value; }); 

and in ajax,

data : formdata , 

demo


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -