Calculate total price of two books using PHP and HTML -
i want know can way using php , html such provided dropdown of books, need select 2 choices out of drop down , calculate sum of price of both books.
assuming had hardcoded books say:
book 1 - $5 book 2 - $15 book 3 - $50
i know if select 1 book. no idea one. please help
code :
<?php if(isset($_post['formsubmit'])) { $varcurrentbook = $_post['formbook']; $errormessage = ""; if(empty($varcurrentbook)) { $errormessage = "<li>you forgot select book!</li>"; } if($errormessage != "") { echo("<p>there error form:</p>\n"); echo("<ul>" . $errormessage . "</ul>\n"); } else { switch($varcurrentbook) { //can use here find option clicked } exit(); } } ?> <form action="<?php echo htmlentities($_server['php_self']); ?>" method="post"> <label for='formbook'>select book</label><br> <select name="formbook"> <option value="0">select book...</option> <option value="15">the secret</option> <option value="10">the fairy tales</option> <option value="5">all words</option> <option value="100">pinaacle studio</option> <option value="120">harry potter</option> <option value="200">thinking in java</option> </select> <input type="submit" name="formsubmit" value="submit" /> </form>
you need use multiple select as:
<form action="<?php echo htmlentities($_server['php_self']); ?>" method="post"> <label for='formbook'>select book</label><br> <select name="formbook[]" multiple><!--the multiple attribute--> <option value="0">select book...</option> <option value="15">the secret</option> <option value="10">the fairy tales</option> <option value="5">all words</option> <option value="100">pinaacle studio</option> <option value="120">harry potter</option> <option value="200">thinking in java</option> </select> <input type="submit" name="formsubmit" value="submit" /> </form>
also note how changed name formbook
formbook[]
. changes $_post['formbook']
array of options.
then can access as:
<?php foreach ($_post['formbook'] $names) { print "you have selected $names<br/>"; /*your need change code here accordingly.*/ } ?>
Comments
Post a Comment