Jump to content

assign an array


denoteone

Recommended Posts

Am I assigning these form variables to the array the most efficient way?

 

HTML:

<form>
<input type="text" name="box1" >
<input type="text" name="box2" >
<input type="text" name="box3" >
<input type="text" name="box4" >
</form>

 

PHP:

$price = array("leg_s"=>$_POST['box1'], "leg_m"=>$_POST['box2], "leg_l"=>$_POST['box3'], "leg_xl"=>$_POST['box4']); 

 

 

Link to comment
Share on other sites

you can use whatever you want.  It will end up as a 2d array regardless, where $_POST is the top level and then the name you use in the form is the 2nd level.  $_POST will always start off on top.  So you can do

 

<input type="text" name="price[leg_s]" >
<input type="text" name="price[leg_m]" >
<input type="text" name="price[leg_1]" >
<input type="text" name="price[leg_x1]" >

 

And it is going to start off as for instance:

 

echo $_POST['price']['leg_s']; // echoes whatever user entered into that field

 

if you want to make it $price['leg_s'] etc... simply do

 

$price = $_POST['price'];

 

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.