Solved

Showing Total on a Form BEFORE Submission--PHP

Posted on 2011-09-12
10
250 Views
Last Modified: 2012-05-12
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?
0
Comment
Question by:DennisHacker
10 Comments
 
LVL 15

Expert Comment

by:Insoftservice
ID: 36527316
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
 
LVL 2

Expert Comment

by:artisticsoul
ID: 36527332
Everything you need can be found here
http://www.quirksmode.org/js/formex.html
0
 

Author Comment

by:DennisHacker
ID: 36527369
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
 

Author Comment

by:DennisHacker
ID: 36527373
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
 
LVL 13

Accepted Solution

by:
ansudhindra earned 500 total points
ID: 36527393
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 15

Expert Comment

by:Insoftservice
ID: 36527400
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
 

Author Comment

by:DennisHacker
ID: 36527411
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
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 36528164
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36528690
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
 

Author Closing Comment

by:DennisHacker
ID: 36533414
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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now