Jump to content

form processing loop


morraine

Recommended Posts

hello, im trying to process a form which has 10 identical sections with some kind of loop.

 

the form is like

 

<INPUT type=checkbox value=Yes name=fieldcheck1>

<INPUT  size=42 name=name1>

<INPUT  size=42 name=phone1>

<INPUT  size=42 name=address1>

 

<INPUT type=checkbox value=Yes name=fieldcheck2>

<INPUT  size=42 name=name2>

<INPUT  size=42 name=phone2>

<INPUT  size=42 name=address2>

 

<INPUT type=checkbox value=Yes name=fieldcheck3>

<INPUT  size=42 name=name3>

<INPUT  size=42 name=phone3>

<INPUT  size=42 name=address3>

 

and on to like this 10 times

 

now i m trying to colect the data from each section of the form with a loop that will check the check box and if its YES then it will insert the data for the section into the database and then increment the varibles number and do the same for the next section until it gets to the end with no sections left to save to database.

 

does any one know how to setup a while loop to do this?

 

 

Link to comment
Share on other sites

A for loop is easier:

 

for ($i = 1; $i <= 10; $i++) {
  echo $_POST['name' . $i];
}

 

However, it is probably even easier to use a different method of gathering the data:

 

<INPUT type=checkbox value=Yes name="userdata[1][checkbox]">
<INPUT  size=42 name="userdata[1][name]">
<INPUT  size=42 name="userdata[1][phone]">
<INPUT  size=42 name="userdata[1][address]">

<INPUT type=checkbox value=Yes name="userdata[2][checkbox]">
<INPUT  size=42 name="userdata[2][name]">
<INPUT  size=42 name="userdata[2][phone]">
<INPUT  size=42 name="userdata[2][address]">

<INPUT type=checkbox value=Yes name="userdata[3][checkbox]">
<INPUT  size=42 name="userdata[3][name]">
<INPUT  size=42 name="userdata[3][phone]">
<INPUT  size=42 name="userdata[3][address]">

 

You would end up with a multidimenstional array in post:

 

foreach ($_POST['userdata'] as $number => $data) {
  echo $data['name'];
}

Link to comment
Share on other sites

Thank you both for you input! ;D

 

hitman6003 i think i will use you solution because i was thinking of a way to collect the data regardless of the amount of sections in the form because i may add more identical sections with javascript but your solution would compensate for any amount of section.

 

Great! thanks guys ;D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.