Solved

Showing Total on a Form BEFORE Submission--PHP

Posted on 2011-09-12
10
251 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

911 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

25 Experts available now in Live!

Get 1:1 Help Now