Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.