ovalb
ovalb

Reputation: 605

Add post data dynamically to later submit with form

noob web developer here!

I have a simple login form that submits user info to a server. Other than the classic username, email and password, the user needs to select a series of tags, and a level associated with each tag.

So I have, inside the form tag, a <select> with the predefined tags, an <input type='range'> to select the level for that particular tag, and a <button> that should allow to add the tag-level values.

When I click the add button, I want to add somehow the tag-level values of that specific combination in a multi-dimensional array AND add them to the post request variables, so that when I click the submit button of the form the server receives something like that:

// 'normal' form stuff
$user = $_POST['user'];
$psw = ...
//  ...
$values = $_POST['values']; 
// this is the bidimensional array I was talking about:
// values = [
//      [tag1, level1],
//      [tag2, level2],
//      ...
//      [tagN, levelN]];

I've tried with the adding the <input type='hidden' name='tag' value='level'> for each tag, but since neither the number (nor the name) of the tags that will be chosen is predetermined, so it's hard to fetch those values server-side.

Is there a trivial way to do this?

Upvotes: 0

Views: 54

Answers (1)

LuisH
LuisH

Reputation: 33

<input type="text" value="level_1" name="tag[0]">
<input type="text" value="level_2" name="tag[1]">

and the value you received like this

array
   'tag' => 
       0 => string 'level_1' (length=7)
       1 => string 'level_2' (length=7)

Upvotes: 2

Related Questions