942,956 Members | Top Members by Rank

Ad:
Aug 10th, 2010
0

Javascript form output

Expand Post »
Hi guys

Im having a bit of trouble with my form,

I basically want the values from the checkboxes to output to a div, I also want to give the check boxes a numerical value eg:

if monday conference selected that would cost £80
if Tuesday conference selected theat would cost £60

and output a total based on the selections.

its basically confirming the users choices and outputting the total cost to them.

I have made a start already but Ive hit a point where Im absolutely stuck.

Thanks in advance for any help offered xxx

Mands


This is the form

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div class="form">
  2. <h1>Online Bookings</h1>
  3.  
  4. <form id ="theForm" action="mailto:" method="post" enctype="text/plain" ><table>
  5. <tr><td>
  6. <tr><td>Name:</td><td><input type="text" name="input" id="name" size="50" /></td></tr>
  7. <tr><td>Email Address:</td><td><input size="30" id="txtEmail" name="input" type="text" /></td></tr>
  8. <tr><td>Tel:</td><td><input size="30" id="txtNumber" name="input" type="text" /></td></tr></table>
  9. <table>
  10. <tr><td><strong> Conference Attendance</strong></td></tr>
  11.  
  12. <tr>
  13. <td>Monday</td><td><input type="checkbox" name="conferenceatt" id="confsun" value="I will be attending Monday" /></td></tr><tr>
  14. <td>Tuesday<td><input type="checkbox" name="conferenceatt" id="confmon" value="I will be attending Tuesday" /> </td></tr><tr>
  15. <td>Wednesday<td><input type="checkbox" name="conferenceatt" id="conftue" value="I will be attending Wednesday" /> </td></tr><tr>
  16. <td>Thursday<td><input type="checkbox" name="conferenceatt" id="confwed" value="I will be attending Thursday" /></td></tr><tr>
  17. <td>Friday<td><input type="checkbox" name="conferenceatt" id="confthur" value="I will be attending Friday" /> </td></tr></table>
  18. <table>
  19. <tr><td><strong>College Accommodation</strong> </td></tr>
  20. <tr>
  21. <td>Sunday Evening</td><td><input type="checkbox" id="collsun" name="Sunday Accommodation" value="Sunday accommodation required" /></td></tr><tr>
  22. <td>Monday Evening</td><td><input type="checkbox" id="collmon" name="Monday Accommodation" value="Monday accommodation required" /></td></tr><tr>
  23. <td>Tuesday Evening</td><td><input type="checkbox" id="colltue" name="Tuesday Accommodation" value="Tuesday accommodation required" /></td></tr><tr>
  24. <td>Wednesday Evening</td><td><input type="checkbox" id="collwed" name="Wednesday Accommodation" value="Wednesday accommodation required" /></td></tr><tr>
  25. <td>Thursday Evening</td><td><input type="checkbox" id="collthur" name="Thursday Accommodation" value="Thursday accommodation required" /></td></tr> </table>
  26. <br />
  27. <input type="button" id="write" value="Write" />
  28. <input type="button" id="erase" value="Erase" />
  29.  
  30. </form>
  31.  
  32.  
  33. </div>
  34. <div class="righttext" id="righttext">
  35.  
  36. </div>

and this is the javascript
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. window.onload = function(){
  2. //get the elements we need
  3. var write = document.getElementById('write');
  4. var erase = document.getElementById('erase');
  5. var display = document.getElementById('righttext');
  6. var name = document.getElementById('name');
  7. var email = document.getElementById('txtEmail');
  8. var number = document.getElementById('txtNumber');
  9. var confs = document.getElementById('confsun');
  10. var confm = document.getElementById('confmon');
  11. var conft = document.getElementById('conftue');
  12. var confw = document.getElementById('confwed');
  13. var confth = document.getElementById('confthur');
  14. var colls = document.getElementById('collsun');
  15. var collm = document.getElementById('collmon');
  16. var collt = document.getElementById('colltue');
  17. var collw = document.getElementById('collwed');
  18. var collth = document.getElementById('collthur');
  19.  
  20. //assign the onclick events with their handler functions
  21.  
  22. write.onclick = function(){
  23. var message = '<strong>' + 'You requirements are:' + '</strong>' + '<br />' + 'Name: ' + name.value + '<br />' + 'Email: ' + email.value + '<br />' + 'Tel: ' + number.value ;
  24.  
  25. display.innerHTML = message;
  26.  
  27. };
  28.  
  29.  
  30.  
  31. erase.onclick = function() {
  32. display.innerHTML = "";
  33. //clear the form field
  34. display.value="";
  35. };
  36.  
  37. }; //end of window.onload
Last edited by mands; Aug 10th, 2010 at 8:09 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mands is offline Offline
14 posts
since Jun 2009
Aug 10th, 2010
0
Re: Javascript form output
You can retrieve the state of a checkbox with the checked property:

javascript Syntax (Toggle Plain Text)
  1. write.onclick = function() {
  2. // calculate total
  3. var total = 0;
  4. if (confs.checked) {
  5. total += 100;
  6. }
  7. if (confm.checked) {
  8. total += 80;
  9. }
  10. // and so on
  11.  
  12. var message = '<strong>' + 'You requirements are:' + '</strong>' + '<br />' +
  13. 'Name: ' + name.value + '<br />' +
  14. 'Email: ' + email.value + '<br />' +
  15. 'Tel: ' + number.value + '<br />' +
  16. 'Total:' + total + '£';
  17.  
  18. display.innerHTML = message;
  19. };
Reputation Points: 13
Solved Threads: 2
Newbie Poster
gumape is offline Offline
16 posts
since Aug 2010
Aug 10th, 2010
0
Re: Javascript form output
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var write = document.getElementById('write');
  2. var erase = document.getElementById('erase');
  3. var display = document.getElementById('righttext');
  4. var name = document.getElementById('name');
  5. var email = document.getElementById('txtEmail');
  6. var number = document.getElementById('txtNumber');
  7. var confs = document.getElementById('confsun');
  8. var confm = document.getElementById('confmon');
  9. var conft = document.getElementById('conftue');
  10. var confw = document.getElementById('confwed');
  11. var confth = document.getElementById('confthur');
  12. var colls = document.getElementById('collsun');
  13. var collm = document.getElementById('collmon');
  14. var collt = document.getElementById('colltue');
  15. var collw = document.getElementById('collwed');
  16. var collth = document.getElementById('collthur');
Are you sure that these variables will be objects instead of null values? The reason is that you are attempting to obtain all of these variables 'onload' while these elements may or may not be loaded yet? Maybe I am wrong...
Reputation Points: 229
Solved Threads: 238
Posting Virtuoso
Taywin is offline Offline
1,723 posts
since Apr 2010
Aug 10th, 2010
0
Re: Javascript form output
Thanks gumape!!

Wow thats just what I needed but couldnt see for looking!!!

Very happy I can now amend the rest to suit my needs based on what youve given me!!

Much appreciated

Mands xxx
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mands is offline Offline
14 posts
since Jun 2009
Aug 11th, 2010
0
Re: Javascript form output
Java is a popular high language computer language that is used all over the world. Even if you are knowledgeable on languages like C and C++, you can undergo <a href="http://www.developintelligence.com/">java training</a> to make your resume shine in the next interview you appear for.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Dokemion is offline Offline
1 posts
since Aug 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: controlling div's scroll range & position
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Changing Flash param w/ Javascript





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC