0

Hi guys.
Im new to PHP by necessity really due to a friend of mine asking me a favour.
I know vb.net so not completely new to the whole programming scene.
But anyway,

I have approx 5 drop down lists. Each item in each drop down has a value "option value=5" for example.The "5" means £5.

I need to add all the option values from each drop down menu to come up with a final price.

How would i go about adding the selected option values?

regards

Ross

3
Contributors
14
Replies
15
Views
5 Years
Discussion Span
Last Post by diafol
0

Hi Ross,

You will need to add the lists to a form, and give a NAME to each list.
When the user hits the submit button, you can collect the $_POST data for each list and calculate the total. $total = $_POST['List1'] + $_POST['List2'] + $_POST['List3'] + $_POST['List4'] + $_POST['List5']; If you want to do it without having a submit button, you may need to look into AJAX.

0

yeah i was trying to create a simple version first before ajax being new to PHP an all.

Thanks for that, ill have a look into it.

PS. how will that script add together the values. The values are different to the text in the drop down list

<select rel="condition"> 
					
					<option value="10">Excellent</option> 
					<option value="5">Good</option> 
					<option value="0">Poor</option> 
				</select>

thsi is what i have at the moment - well a snipet

Edited by rosstafarian: n/a

0
<select rel="condition" name="condition">
     <option value="10">Excellent</option>
     <option value="5">Good</option>
     <option value="0">Poor</option>
</select>

When the form is posted, $_POST["condition"] is value of the option selected by the user.
If you add a different name to each select statement, you can collect each of the values and perform the calculation.

0

ahhh i see

thankyou

ill keep this thread open as ill more than likely get into some trouble :D

0

ran into a few troubles

this is my test1.php file --

<html>
<body>


<form action ="results.php" method="post">


				<select rel="screen size" name="ssize"> 
					<option value="10">under</option> 
					<option value="20">higher</option> 
				</select>
				
				
				
				
				<select rel="condition" name="condition"> 
					<option value="10">Excellent</option> 
					<option value="5">Good</option> 
					<option value="0">Poor</option> 
				</select>
				
				
				
				<select rel="optical drive" name="opticalDrive"> 
					<option value="20">DVD drive</option> 
					<option value="5">CD drive</option> 
					<option value="0">No drive</option> 
				</select>
				
				
				<input type="submit" />
				</form>
				
			
				
				
				
				
</body>
</html>

and this is my results.php file ----

<html>
<body>

<?php

$total = $_POST['ssize'] + $_POST['condition'] + $_POST['opticalDrive'];

echo $total;

?>

</body>
</html>

when i click submit, the results.php is blank. whats gone wrong?

0

It works fine for me. I copy/pasted the above code (and removed the rel tags).

Try to view the source code of the blank page, see if there is any generated code

0

right click inspect element on Chrome shows its some how done this :S

<body>

<!--?php

$total = $_POST['ssize'] + $_POST['condition'] + $_POST['opticalDrive']; ?-->

Price is <!--?php echo $total; ?-->




</body>
0

ok now im being stupid - running it off my pc seemed to be the problem.

ran it on my website all good. thanks mate

0

before you assume an expression like this, ensure that the variables are set and are valid. You should protect yourself:

<?php
if(isset($_POST['ssize']) && isset($_POST['condition']) && isset($_POST['opticalDrive']) && is_int($_POST['ssize']) && is_int($_POST['condition']) && is_int($_POST['opticalDrive'])){
    $total = $_POST['ssize'] + $_POST['condition'] + $_POST['opticalDrive'];
    echo "total = " . $total;
}else{
    echo "Some data was invalid.<br />";
    print_r($_POST);
}
?>

Edited by diafol: n/a

0

As ardav suggests, you should NEVER TRUST POST DATA.

Before you use any of the variables in results.php you should perform a few basic tests. if (isset($_POST['ssize'])) will check if the variable has been set before you try to use it. if (is_int($_POST['ssize'])) will check that the value is an integer, not some nasty HTML.

Edited by Zagga: n/a

0

ahhh i see - will get the basics sorted and add in some of that stuff.

Cheers guys, much appreciated

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.