0

Looking but cant find the answer though Im sure Ive used this elsewhere in my program.

I am building a navigation form that allows a person to set the name of the navigation along with an optional subtitle and breadcrumb value.

These form inputs are created dynamically and appears like so:

<input name="menu[1][title]" value="Index" />
<input name="menu[1][subtitle]" value="Our home page" />
<input name="menu[1][crumb]" value="Home" />

<input name="menu[2][title]" value="Contact" />
<input name="menu[2][subtitle]" value="Get in touch" />
<input name="menu[3][crumb]" value="Contact" />

These form inputs are created in the format menu[ID][TYPE]. From there I will use PHP to update a database matching the menu ID property to the database table ID, and the column to the menu TYPE property.

Im just stumped on how to get the inputs and through Jquery/JSON send them to the PHP script. Mainly being that the ID numbers are going to be dynamically created. Just about to help my gf unlock the keys she locked in her car so I will post a code snippet when I get back.

Thanks for the feedback. After searching for a relatively simple way to achieve this Ive opted to use strings which just means that I have to update the code if I add a menu element at a later date.

Mark Lawrence
  • 255
  • 1
  • 7
  • 17
  • Possible duplicate of: http://stackoverflow.com/questions/2627813/how-to-get-an-array-with-jquery-multiple-input-with-the-same-name – Silwing Apr 19 '13 at 07:38
  • Since you want to use jQuery/JSON to send the data to PHP I'm guessing you'll be using AJAX? jQuery's `.ajax()`'s `data` property is JSON, and you'll need to recreate the multidimensional array in here, before sending it to PHP. – Arnelle Balane Apr 19 '13 at 07:42
  • You probably don't need to but it may be simpler to squeeze all the data into one field like so ``, then separate it out server-side with PHP's `explode()`. This guarantees that all related data stays together. – Beetroot-Beetroot Apr 19 '13 at 07:48

3 Answers3

1

You can use the jquery serialize() function to send all data of a form to a PHP page. http://api.jquery.com/serialize/

Supriti Panda
  • 1,281
  • 1
  • 11
  • 12
0

Im just stumped on how to get the inputs

Submit button onclick.

Mainly being that the ID numbers are going to be dynamically created

Any element can be queried for the value of one of its attributes at any time.

send them to the PHP script.

The name/value pairs are automatically sent to a php script when the user submits the form. Why do you need to do anything?

7stud
  • 46,922
  • 14
  • 101
  • 127
0

I guess the best way would be to build an array from your inputs, and then send this array with AJAX to your PHP script

I let you check this jsFiddle: (i just updated it) http://jsfiddle.net/ds52y/13/

$( function() { /* look in the jsFiddle */ });

Hope this helps :)

Toto-Graph
  • 184
  • 5
  • Had a look, thanks for the input very much appreciated. Opted for strings over a single array just to simplify the process. – Mark Lawrence Apr 19 '13 at 10:02