user3163404
user3163404

Reputation: 359

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:

or can also be:

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) {}

Upvotes: 0

Views: 1489

Answers (1)

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) {}

Upvotes: 3

Related Questions