简体   繁体   中英

PHP POST serialize() data from dynamic form ajax

I have a script on jQuery and HTML5 that creates a form dynamically.

Then I use:

var OForm = $('#OForm');

// Find disabled inputs, and remove the "disabled" attribute
var disabled = OForm.find(':input:disabled').removeAttr('disabled');

// serialize the form
var Values = OForm.serialize();

// re-disabled the set of inputs that you previously enabled
disabled.attr('disabled','disabled');

console.log(Values);

Then I use ajax to POST the Values to PHP.

Problem:

If the form was not dynamic. I can get the values:

$name = $_POST['name'] 

and so on..

But the problem is that I have some fields in my form that are numbered.

Example:

  • name1
  • name2
  • name3

or can also be:

  • name3
  • name10
  • name23

How can I get these values to insert them on MySQL?

SOLVED

Use arrays instead of numbered keys.

<input name="name[10]" value="Josh"><input name="name[23]" value="Peter">

Submitted:

$_POST["name"] = array(
    "10" => "Josh",
    "23" => "Peter",
);

You can use foreach to traverse all names:

foreach ($_POST["name"] as $key => $value) {}

Use arrays instead of numbered keys.

<input name="name[10]" value="Josh"><input name="name[23]" value="Peter">

Submitted:

$_POST["name"] = array(
    "10" => "Josh",
    "23" => "Peter",
);

You can use foreach to traverse all names:

foreach ($_POST["name"] as $key => $value) {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM