Jump to content

Stream line $_POST


denoteone

Recommended Posts

I have 43 input  text boxes that I am going to name  box0 box1, box2 and so on. is there a trick I can use to assign them once I submit the form.  so that I don't have to do  $box_1 = $_POST['box1']; some sort of loop and then I am going to have to insert the values into a mySQL database.

 

Any help would be awesome.

 

 

Link to comment
Share on other sites

if box[] is your HTML array, you could access each individual element like so

 

echo $_POST['box'][5];//the sixth text box

 

you could loop through the whole array like so

for ($i = 0; $i < count($_POST['box']); $i++){
echo $_POST['box'][$i];
}

//OR

foreach($_POST['box'] as $box){
echo $box;
}

Link to comment
Share on other sites

so would my code look something like this

if(isset($_POST['submit'])){

for($i = 0, $i <= 43,$i ++){
$box.$i = box[i];
}

}

 

Don't do that. What if one day you added another checkbox, or took one away? Chances are you won't find the error when you define 43 like that.

Link to comment
Share on other sites

Looks like you want to do something like this?

 

if(isset($_POST['submit'])){
for ($i = 0; $i < count($_POST['box']); $i++){
${'box_' . $i} = $_POST['box'][$i];
echo ${'box_' . $i};
}

 

Variables will then be like $box_0, $box_1, etc..

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.