Showing Total on a Form BEFORE Submission--PHP

I know this is probably very simple, and I've even tried searching on google for sample solutions...however, I must not be asking the right question.

I have an entry form for a tournament, with a basic entry amount.

There is a checkbox on the form, when if checked, adds a set amount to the basic entry.

I want to show a total on at the bottom of the form BEFORE the user clicks the submit button.  Basically this would be a toggle...if the checkbox is checked, the amount goes up.  If not, it is the basic amount.

Lets say the following is true:

$basicAmount = 100.00

$scratchTeam = 40.00

$basicAmount applies to all entries.  If the checkbox with name="scratchTeam" is checked, then I want the total at the bottom to show $basicAmount + $scratchTeam.  However, I can't do that until the Submit button is clicked and the $_POST['scratchTeam'] is sent to the browser.

I'm figuring that javascript is required here.  Can someone help, or at least point me in the direction of a tutorial on the web that discusses this?
DennisHackerAsked:
Who is Participating?
 
ansudhindraConnect With a Mentor Commented:
check this..
 
<?php
  $basicAmount = 100;
  $scratchTeam = 40;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title></title>
  <script type="text/javascript">
    <?php
      echo "var basicAmount = $basicAmount;";
      echo "var stAmount = $scratchTeam;";
    ?>
    function doUpdateTotal(ele){
      var totAmount=0;
      if(ele.checked){
        totAmount = basicAmount + parseInt(ele.value);
      } else {
        totAmount = basicAmount;
      }
      document.getElementById('totalAmount').innerHTML=totAmount;
    }
  </script>
  </head>
  <body>
      <?php
        echo "Basic : $basicAmount <br> ";
      ?>
      Scratch Team : <input type="checkbox" name="scteam" id="scteam" value="<?php echo $scratchTeam; ?>" onClick="javascript:doUpdateTotal(this)">
      <br>
      <b>Total :</b><span id="totalAmount"></span>
  </body>
</html>

Open in new window

0
 
InsoftserviceCommented:
hi,

ALGO

provide the checkbox with unique no .
on on each click call javascript function to get the value of the specific checkbox w.r.t there id.

and than add it with your basic amount
0
 
artisticsoulCommented:
Everything you need can be found here
http://www.quirksmode.org/js/formex.html
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
DennisHackerAuthor Commented:
artisticsoul:

Did you mean the site, or the page itself that you sent me to.

If it was the site, I'll have to search.  The page you sent me to STILL REQUIRES A SUBMIT before anything happens in the text box at the bottom.  What I need on my page is for the amount the person is going to pay changes AS SOON AS THE CHECKBOX IS CHECKED.
0
 
DennisHackerAuthor Commented:
insoftservice:

I'm sorry.  I don't understand your abbreviations.

The checkbox has a unique name.  Why does it need a unique no.?
What does w.r.t. stand for?
0
 
InsoftserviceCommented:
its done with respect to (i.e w.r.t) jquery .
this is not tested please do let me know if any issue .

it will check whether the checkbox is selected if true than add or else subtract the total value.
in javascript the id should have unique value u can check that i have given cricket,footabll but not same text for id . hope its clear now

<input type="text" value="400" id="totalid" name="total">


<input type="checkbox" id="cricket" name="cricket" value='100' onclick="javascript:add('cricket');">
<input type="checkbox" id="football" name="football" value='200' onclick="javascript:add('football');">
<input type="checkbox" id="golf" name="golf" value='300' onclick="javascript:add('golf');">

<SCRIPT LANGUAGE="JavaScript">


function add(id)
{
  var total = $('#totalid').val();
  var select = $('#'+id).val();

   if($('#'+id).checked== true)
   {
      var add = total+select;
   }
   else
   {
     var add = total-select;
   }
 
  $('#totalid').val(add);
}
</script>
0
 
DennisHackerAuthor Commented:
ansudhindra:

I think we are very close here.  However, it is now 12:36 AM where I live, and I won't be able to test this and adapt it until the morning.

I will let you know.  In your example, it works exactly as I want it to work.
0
 
Mohamed AbowardaSoftware EngineerCommented:
Any change on the current active page can be done by using Javascript, if you want to deal with PHP as well you will have to use AJAX.

Usually, you can use
document.getElementById('elementNameHere').innerHTML = "Contents here...";

Open in new window

to change HTML DOM element contents, such as DIV, Body, etc...

If you want to send request to PHP page and wait for response, use AJAX:
http://www.w3schools.com/php/php_ajax_php.asp
0
 
Ray PaseurCommented:
There are two layers to this question. The client presentation layer is what is addressed by the JavaScript part of things.  The server-side must still validate the input, since hackers will just bypass the JavaScript and post sewage directly into your action script.

A popular AJAX library that facilitates good communication between the server and the client (allows back-end action without an apparent page load) is jQuery.  Start exploring here:
http://docs.jquery.com/Tutorials
0
 
DennisHackerAuthor Commented:
The only thing I had to do differently was call the function in the body tag onLoad.  Before I did that, nothing would show up as a default total at the bottom of the form.  The logic that defines the default function is inside the function script in the head of the form.
0
All Courses

From novice to tech pro — start learning today.