Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Showing Total on a Form BEFORE Submission--PHP

Posted on 2011-09-12
10
Medium Priority
?
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 2000 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
 
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 111

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

618 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