javascript - Converting HTML functions into PHP scripts. (two select values to multiply) -
my job convert following javascript functions php , display correct results. how use php multiply 2 values of numa , numb select options? discount must displayed in discount input box, grand total in total input box. having difficulties creating php script takes 2 selected options's values , multiplies them, while displaying 2 results in boxes below.
this html code functions need convert php.
<!doctype html> <html> <head> <title> </title> <script> var numa = 0; function totala(vala) { if (vala) { numa = number(vala);} var disc = numb*numa; var totalcost = (numa - disc); document.getelementbyid("total").value = "$" + totalcost.tofixed(2); document.getelementbyid("discount").value = "$" + disc.tofixed(2);} var numb = 0; function totalb(valb) { if (valb) { numb = number(valb);} var disc = numb*numa; var totalcost = (numa - disc); document.getelementbyid("total").value = "$" + totalcost.tofixed(2); document.getelementbyid("discount").value = "$" + disc.tofixed(2);} </script> </head> <div align="center"> <br> <form name id="main"> <select id="numa" placeholder="please select service" onchange="totala(this.value);"> <option value="" disabled selected>select service</option> <option value="125">makeover</option> <option value="60">hair styling</option> <option value="35">manicure</option> <option value="200">permanent makeup</option> </select> <br><br> <select id="numb" placeholder="please select discount" onchange="totalb(this.value);"> <option value="" disabled selected>select discount</option> <option value="0">0%</option> <option value=".1">10%</option> <option value=".2">20%</option> </select> <br><br> <td>discount coupon: <input id="discount" name="showtotal" type="text" value="$0.00" size="10" readonly/></td> <td>grand total: <input id="total" name="showtotal" type="text" value="$0.00" size="10" readonly=""/></td> <br><br> </form> <hr> </div> </html>
first update form:
<form action="" method="post"> <select name="service" id="service" placeholder="please select service"> <option value="" disabled selected>select service</option> <option value="125">makeover</option> <option value="60">hair styling</option> <option value="35">manicure</option> <option value="200">permanent makeup</option> </select> <br><br> <select name="discount" id="discount" placeholder="please select discount"> <option value="" disabled selected>select discount</option> <option value="0">0%</option> <option value=".1">10%</option> <option value=".2">20%</option> </select> <br><br> <button type="submit">calculate</button> </form>
process form submission php (calculate total cost & discount):
<?php $service = isset($_post['service']) ? $_post['service'] : 0; $discount = isset($_post['discount']) ? $_post['discount'] : 0; $total_discount = number_format($discount * $service, 2); $total_cost = number_format($service - $total_discount, 2); ?>
finally, display total cost & discount:
<p>discount coupon: <input id="discount" type="text" value="$<?php echo $total_discount; ?>"/></p> <p>grand total: <input id="total" type="text" value="$<?php echo $total_cost; ?>"/></p>
Comments
Post a Comment