Jump to content

Confirmation page with Multidimensional array values


fbords

Recommended Posts

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!

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

<?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>';

?

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

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.