fbords Posted July 9, 2007 Share Posted July 9, 2007 I was hoping someone could help a newbie with a script I'm trying to write. I have an HTML form that uses a multidimensional array to generate a select box. The array is as follows: $Shirts = array ( "Cotton T-shirt" => "7.00", "Cotton Long Sleeve T-shirt" => "8.00" ); At the drop down box, I use the following to populate all the items: echo '<SELECT name=Shirt1Type> <option selected value="">--------------Shirt Type--------------</option>'; foreach ($Shirts as $Shirt_Type => $Shirt_Price) { echo '<OPTION value='.$Shirt_Type.'>'.$Shirt_Type.'</option>'; } echo '</select>'; This all works fine. However, on my confirmation page, I want to be able to write the $Shirt_Price variable and I can't seem to figure it out. All of my other variables are retrieved when using $_POST[''], but it's a little more tricky when the value is in an array. My ultimate goal is to be able to print 7.00 if Cotton T-shirt was selected, 8.00 if Cotton Long Sleeve T-shirt was selected, etc. Thanks! Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 9, 2007 Share Posted July 9, 2007 Your New select box: <?php echo '<SELECT name=Shirt1Type> <option selected value="">--------------Shirt Type--------------</option>'; foreach ($Shirts as $Shirt_Type => $Shirt_Price) { echo '<OPTION value='.$Shirt_Type.'_'.$Shirt_Price.'>'.$Shirt_Type.'</option>'; } echo '</select>'; ?> The processing page: <?php list($sType,$sPrice) = explode("_",$_POST['shirt1Type']); echo 'Choosen Shirt: '.$sType.'<br>'; echo 'Shirt Price: '.$sPrice; ?> Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 9, 2007 Share Posted July 9, 2007 <?php echo '<SELECT name=Shirt1Type> <option selected value="">--------------Shirt Type--------------</option>'; foreach ($Shirts as $Shirt_Type => $Shirt_Price) { echo '<OPTION value='.$Shirt_Price.'>'.$Shirt_Type.'</option>'; } echo '</select>'; ? Quote Link to comment Share on other sites More sharing options...
Barand Posted July 9, 2007 Share Posted July 9, 2007 try <?php $Shirts = array ( "Cotton T-shirt" => "7.00", "Cotton Long Sleeve T-shirt" => "8.00" ); /** * process submitted data */ if (isset($_POST['sub'])) { $shirt = $_POST['ShirtType'] ; echo "Type : $shirt, Price: $Shirts[$shirt]"; } /** * the form */ echo '<form method="post">'; echo '<SELECT name="ShirtType"> <option selected value="">--------------Shirt Type--------------</option>'; foreach ($Shirts as $Shirt_Type => $Shirt_Price) { echo '<OPTION value="'.$Shirt_Type.'">'.$Shirt_Type.'</option>'; } echo '</select>'; echo '<input type="submit" name="sub" value="Submit">'; echo '</form>'; ?> Quote Link to comment Share on other sites More sharing options...
fbords Posted July 9, 2007 Author Share Posted July 9, 2007 Little Guy, your edits did the trick. May I ask you to elaborate a little bit on how this works, as I'd like to learn the best I can. Specifically, this line: list($sType,$sPrice) = explode("_",$_POST['shirt1Type']); Thanks! Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 9, 2007 Share Posted July 9, 2007 let me share my thoughts hmm!! list($sType,$sPrice) = explode("_",$_POST['shirt1Type']); list===Assign variables as if they were an array $sType,$sPrice== container of the contents of the array this is where the values are being pass explode("_",$_POST['shirt1Type']); ==converted into an array in which the $sType,$sPrice get their values hope that makes a little sense Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.