PHP Form with Drop down Menus {SOLVED}

Ok i have been on all night searching for an answer to this and nothing similar so hope someone out there can help me out. Here is the code im working with very basic to make it easy.

<form method="post" action="index.php">
<select>
<option name="object1" [b]value="5"[/b] selected="selected">Object1</option>
<option name="object2" [b]value="3"[/b]>Object2</option>
</select>
<br />
<select>
<option name="object3" [b]value="0"[/b] selected="selected">Object3</option>
<option name="object4" [b]value="1"[/b]>Object4</option>
</select>
<br />
<b>Total</b>
[b]<?php

?>[/b]
<br />
<input type="submit" value="Calculate">

OK here is my problem when a person chooses the option from each drop down menu i want to grab those values and put them into an equation to make it easy (just multiply them) to come out with the total. I also would like to have the options still selected when it goes to the next page.

<form method="post" action="index.php"> 
<select> 
<option name="object1" value="5" selected="selected">Object1</option> 
<option name="object2" value="3">Object2</option> 
</select> 
<br /> 
<select> 
<option name="object3" value="0" selected="selected">Object3</option> 
<option name="object4" value="1">Object4</option> 
</select> 
<br /> 
<b>Total</b> 
<?php 

?> 
<br /> 
<input type="submit" value="Calculate"> 

Okay first off the select syntax that I usually see is:

<select name="example">
<option value="ex1">Example 1</option>
<option value="ex2">Example 2</option>
</select>

<select name="test">
<option value="test1">Test 1</option>
<option value="test2">Test 2</option>
</select>

This code works and doing it this way will make everything you want done easier. On the PHP part you might use the following code to multiply the outcome:
[php]

<?php $total = $_POST['example'] * $_POST['test']; ?>

[/php]

And to display the selected on the next page, I put the options in an array and while the foreach statment lays out the code check to see of the $_POST value matches the value. This might seem like a lot, but this does work:

[php]

<?php $example = array('Example 1' => 'ex1', 'Example 2' => 'ex2'); $test = array('Test 1' => 'test1', 'Test 2' => 'test2'); ?> <?php foreach($example as $key => $value){ echo ''.$key.''; } ?> <?php foreach($test as $key => $value){ echo ''.$key.''; } ?> [/php]

Hope this helps you.

OK thanks to get the total worked until I added in the arrays and everything went down the drain here is the original code im working with when i view it, does not keep the values for the next page and now its not calculating anything for the total. Plz help.

Im also having the form post back to the same page could this be causing a problem. So when you click calculate it will refresh the page with the new total with what has been selected.

[php]

Refining Yield Calculator

Refining Yield Calculator

Station Equipment <?php $s_equip = array('75%'=>'0.75', '50%'=>'0.5', '40%'=>'0.4', '35%'=>'0.35', '32%'=>'0.32', '30%'=>'0.30'); $refine_sk = array('Level 0'=>'0', 'Level 1'=>'1', 'Level 2'=>'2', 'Level 3'=>'3', 'Level 4'=>'4', 'Level 5'=>'5'); $ref_eff_sk = array('Level 0'=>'0', 'Level 1'=>'1', 'Level 2'=>'2', 'Level 3'=>'3', 'Level 4'=>'4', 'Level 5'=>'5'); $ore_pro_sk = array('Level 0'=>'0', 'Level 1'=>'1', 'Level 2'=>'2', 'Level 3'=>'3', 'Level 4'=>'4', 'Level 5'=>'5'); ?> <?php foreach($s_equip as $key => $value){ echo ''.$key.''; } ?>
Refining Skill <?php foreach($refine_sk as $key => $value){ echo ''.$key.''; } ?>
Refinery Efficiency Skill <?php foreach($ref_eff_sk as $key => $value){ echo ''.$key.''; } ?>
Ore Processing Skill <?php foreach($ore_pro_sk as $key => $value){ echo ''.$key.''; } ?>
Total <?php $total = $_POST['s_equip'] + $_POST['refine_sk'] + $_POST['ref_eff_sk'] + $POST['ore_pro_sk']; echo $total; ?> %
[/php]

Admin Edit: Added PHP code tags for readability. Please see http://phphelp.com/guidelines.php for posting guidelines

[php]

<?php foreach($ore_pro_sk as $key => $value){ echo ''.$key.''; } [/php] Here is the first problem I am seeing. the $_POST array in each foreach statement needs to be the same as the name of the menu. Above the menu name is ore_pro_sk so the post array would be $_POST['ore_pro_sk'] fix all your foreach statements so the $_POST matches your menu name. See if that will fix your problem.

Thank you for catching that. I have fixed it. But unfortunately that was not the problem. Here i have it posted on the interenet at http://www.ascensionscorp.com/refining.php so you can see whats happening same problems as before. no calculations and not keeping the same selections.

I have fixed your problem by grabbing your code and fiddling around with it on my dreamweaver and testing it out on my local webserver and found the problems. This code is the fixed version:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
<title>Refining Yield Calculator</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
</head> 
<body> 
<h2>Refining Yield Calculator</h2> 
<form method="post" action="refining.php"> 
<table cellspacing="0" cellpadding="2" border="0"> 
<tr> 
  <td width="150"> 
   <b>Station Equipment</b> 
  </td> 
  <td width="100"> 
<?php 
$s_equip = array('75%' => 0.75, '50%' => 0.5, '40%' => 0.4, '35%' => 0.35, '32%' => 0.32, '30%' => 0.30); 
$refine_sk = array('Level 0' => 0, 'Level 1' => 1, 'Level 2' => 2, 'Level 3' => 3, 'Level 4'=> 4, 'Level 5' => 5); 
$ref_eff_sk = array('Level 0'=> 0, 'Level 1' => 1, 'Level 2' => 2, 'Level 3' => 3, 'Level 4'=> 4, 'Level 5' => 5); 
$ore_pro_sk = array('Level 0'=> 0, 'Level 1' => 1, 'Level 2' => 2, 'Level 3' => 3, 'Level 4'=> 4, 'Level 5' => 5); 
?> 
   <select name="s_equip"> 
<?php 
foreach($s_equip as $key => $value){ 
    echo '<option value="'.$value.'"'; 
    if($_POST['s_equip'] == $value) echo 'selected="selected"'; 
    echo '>'.$key.'</option>'; 
} 
?> 
   </select> 
  </td> 
</tr> 
<tr> 
  <td> 
   <b>Refining Skill</b> 
  </td> 
  <td> 
   <select name="refine_sk"> 
<?php 
foreach($refine_sk as $key => $value){ 
    echo '<option value="'.$value.'"'; 
    if($_POST['refine_sk'] == $value) echo 'selected="selected"'; 
    echo '>'.$key.'</option>'; 
} 
?> 
   </select> 
  </td> 
</tr> 
<tr> 
  <td> 
   <b>Refinery Efficiency Skill</b> 
  </td> 
  <td> 
   <select name="ref_eff_sk"> 
<?php 
foreach($ref_eff_sk as $key => $value){ 
    echo '<option value="'.$value.'"'; 
    if($_POST['ref_eff_sk'] == $value) echo 'selected="selected"'; 
    echo '>'.$key.'</option>'; 
} 
?> 
   </select> 
  </td> 
</tr> 
<tr> 
  <td> 
   <b>Ore Processing Skill</b> 
  </td> 
  <td> 
   <select name="ore_pro_sk"> 
<?php 
foreach($ore_pro_sk as $key => $value){ 
    echo '<option value="'.$value.'"'; 
    if($_POST['ore_pro_sk'] == $value) echo 'selected="selected"'; 
    echo '>'.$key.'</option>'; 
} 
?> 
   </select> 
  </td> 
</tr> 
<tr> 
  <td> 
   <b>Total</b> 
  </td> 
  <td> 
   <b> 
   <?php 
   $total = $_POST['s_equip'] + $_POST['refine_sk'] + $_POST['ref_eff_sk'] + $_POST['ore_pro_sk']; 
   echo $total; 
   ?> 
   %</b> 
<tr> 
  <th colspan="2"><input type="submit" value="Calculate"></th> 
</tr> 
</table> 
</form> 
</body> 
</html>

Your problem was with your foreach statement and it was basically my fault for not rechecking the code I gave you.

[php]

<?php foreach($ore_pro_sk as $key => $value){ ***echo ''.$key.''; } [/php] The line with the *'s is wrong. If you take a look at the new script at those section you'll realize how I fixed it. Also just to let you know in the calculation part of the script you had a $POST instead of $_POST but it's all fixed. Let me know if you have any other problems.
Sponsor our Newsletter | Privacy Policy | Terms of Service