Current location: Hot Scripts Forums » Programming Languages » PHP » Form Values


Form Values

Reply
  #1 (permalink)  
Old 02-17-06, 06:57 AM
Web-Frenzy Web-Frenzy is offline
Newbie Coder
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Question Form Values

I am working on a custom order script for a computer website, it consists of drop down boxes. When they select an item from the dropdown box and click on the Update Price button then it shows the current subtotal. Unfortunatley when they click Update Price the dropdown boxes go back to Please Choose an Item. Here is a link to it,

http://www.acacustompcs.com/beta/ordersys/order.php

i looked all over the internet. I cant seem to find an answer on how to preserve the data they submit in this form. Help is appreciated

Thanks in advance,
Chris B
Reply With Quote
  #2 (permalink)  
Old 02-17-06, 06:59 AM
Web-Frenzy Web-Frenzy is offline
Newbie Coder
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Here is the code.


<?php

echo'
<html>
<link href="http://www.web-frenzy.com/images/wf.css" rel="stylesheet" type="text/css">
<table class="body-text-bold-mid">
<tr>
<td>
<form method=post>
<br>Motherboards:
<br>
<SELECT name="var" class="body-text-bold-mid">
<OPTION value="0">Please choose one
<OPTION value="20">Option 1 - $20
<OPTION value="40">Option 2 - $40
<OPTION value="100">Option 3 - $100
</SELECT>
<br>
</td>
</tr>
<tr>
<td>
<br>Hard Drives:
<br>
<SELECT name="var2" class="body-text-bold-mid">
<OPTION value="0">Please choose one
<OPTION value="20" name="Option 1 - $20">Option 1 - $20
<OPTION value="40" name="Option 2 - $40">Option 2 - $40
<OPTION value="100" name="Option2 - $100">Option 3 - $100
</SELECT>
<br>
</td>
</tr>
<tr>
<td>
<input type=submit value="Update Price"></input>
</form>
</td>
</tr>
</table>
';
$var = $_POST['var'];
$var2 = $_POST['var2'];
$total = $var + $var2;
echo "Total:<b> ";
echo "$$total";
echo "</b> ";
?>
Reply With Quote
  #3 (permalink)  
Old 02-17-06, 07:34 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
This works for me:

PHP Code:

<?php


echo'
<html>
<link href="http://www.web-frenzy.com/images/wf.css" rel="stylesheet" type="text/css">
<table class="body-text-bold-mid">
<tr>
<td>
<form method=post>
<br>Motherboards:
<br>
<SELECT name="var" class="body-text-bold-mid">
<OPTION value="0">Please choose one</option>'
;

// Put all options in an array
$options = array(
            
'Option 1 - $20' => 20,
            
'Option 2 - $40' => 40,
            
'Option 3 - $100' => 100
                
);
        
        
// Loop through the array
        
foreach ($options as $option => $price)
        {
            echo 
'<option value="'$price .'"';
                
                
// Add a "selected" to the option where it belongs to
                
if ($_POST["var"] == $price)
                {
                    echo 
' selected="selected"';
                }
            
            echo 
'>'$option .'</option>'"\n";
        }

echo 
'
</SELECT>
<br>
</td>
</tr>
<tr>
<td>
<br>Hard Drives:
<br>
<SELECT name="var2" class="body-text-bold-mid">
<OPTION value="0">Please choose one</option>'
;

foreach (
$options as $option => $price)
{
    echo 
'<option value="'$price .'"';
            
        if (
$_POST["var2"] == $price)
        {
            echo 
' selected="selected"';
        }
            
    echo 
'>'$option .'</option>'"\n";
}

echo 
'
</SELECT>
<br>
</td>
</tr>
<tr>
<td>
<input type=submit value="Update Price"></input>
</form>
</td>
</tr>
</table>
'
;
$var $_POST['var'];
$var2 $_POST['var2'];
$total $var $var2;
echo 
"Total:<b> ";
echo 
"$$total";
echo 
"</b> ";
?>
Should go in the PHP section though!
Reply With Quote
  #4 (permalink)  
Old 02-17-06, 08:58 AM
Web-Frenzy Web-Frenzy is offline
Newbie Coder
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
im a newbie at php, i can understand a little bit of what you added but can you explain it a littler better? by the way it works perfectly, thank you very much.
Reply With Quote
  #5 (permalink)  
Old 02-17-06, 09:10 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Well the submitted option needs a "selected="selected"", so that it shows up next time. This can't be done in HTML only. This is why a added an array with all the values of the "select" and added the loop. The loop goes through all elements in the array, and echoes an <option> for each, and checks each if the submitted value is the same as the current value. If so than does it add the "selected". I don't know if there's another way to do that, but I always do it like that. I hope you understand.
Reply With Quote
  #6 (permalink)  
Old 02-17-06, 09:12 AM
Web-Frenzy Web-Frenzy is offline
Newbie Coder
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
yes i understand now, thanks for your time
Reply With Quote
  #7 (permalink)  
Old 02-17-06, 12:32 PM
Christian's Avatar
Christian Christian is offline
Community VIP
 
Join Date: Mar 2005
Location: ProgrammingTalk
Posts: 2,449
Thanks: 0
Thanked 6 Times in 5 Posts
## Thread Moved to the Correct Forum ##
__________________
:: ImperialBB :: New version in the works! :: http://www.imperialbb.com ::

:: Have a question about the board? The Rules? An Infraction/Warning? :: Contact Form ::
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Auto Dealer Script nuzzle PHP 17 04-14-10 08:34 PM
how do I update form, and hidden values in a form from a popup window? lordmerlin JavaScript 2 12-13-05 02:05 PM
getting form values Baho ASP 4 09-02-05 04:02 PM
code as form values? Erynn PHP 3 09-20-04 01:49 PM
formmail problem gscraper Perl 12 08-27-04 03:06 AM


All times are GMT -5. The time now is 06:34 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.