Jump to content

Automatically Show The Result


thelee

Recommended Posts

currently im using this coding to calculate total price.but i need to submit 1st then it will show the result.how can i make it automatically show the result without press the submit button ?

 

<?php


$dollars = 0;


// If the user post the form
if(isset($_POST['value'])) {
   foreach ($_POST['product'] as $product) {
       if(is_numeric($product) && $product >= 0) {
           $dollars += $product;
       }
   }
}


?>


<!DOCTYPE HTML>
<html>
   <head></head>
   <body>



       <form method="post">
           <label><input type="checkbox" name="product[]" value="100" /> 100</label><br/>
           <label><input type="checkbox" name="product[]" value="250" /> 250</label><br/>
           <label><input type="checkbox" name="product[]" value="350" /> 350</label><br/>
           <label><input type="checkbox" name="product[]" value="20" /> 20</label><br/>
           <label><input type="checkbox" name="product[]" value="25" /> 25</label><br/>


           <input type="submit" name="value" />
       </form>
 <p>Product : RM
   <input name="textfield" type="text" value="<?php echo $dollars; ?>">
 </p>
   </body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/271172-automatically-show-the-result/
Share on other sites

You don't need AJAX for this, a purely client-side Javascript is able to handle the request just fine.

 

Theele: Write a JS that does the same as your PHP script, and then bind it to onchange on the input fields. You might want to check out jQuery for this, as it'll help make this a lot simpler than with just plain JS.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.