Auto Populating Form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dpi
    New Member
    • Jan 2007
    • 11

    #1

    Auto Populating Form

    Hello All,

    I am created an Event Sign up form. They have a bunch of different conferences during the event people can sign up for. What i want to do is when they check an event, the costs of the event is automatically added to a "total" field at the bottom so people know how much they are paying. I assumed this would be simple, but i cant find for the life of me how to do it.

    Any help would be very greatly appreciated. I am not a programmer, so if anyone knows the actually code to do this and where it goes, that would be perfect.

    Thanks in advance.

    Cory
  • AricC
    Recognized Expert Top Contributor
    • Oct 2006
    • 1885

    #2
    What language are you planning on doing this in? Does the total need to be saved somewhere like a database? Generally, we don't just write scripts for people we offer assistance.


    Aric

    Comment

    • dpi
      New Member
      • Jan 2007
      • 11

      #3
      If there is a simple way to implement a javascript function into an already established HTML form that uses php to process that would be great.

      I wouldnt expect it to be custom written, I would assume there has to be something like this out there that has already been done, or at least elements that I can use.

      Like i said, I have the way to process the forms and can create the forma nd everything else, I just need that one little function to take add up what has been checked and give a total in a box. It is basically just something for the users benefit, and to make it simplier for the people inputting the payments to see what the total was.

      Thanks.

      Cory

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        You will obviously need Javascript for this. Post your HTML code (in tags).

        See this, this and this to help you.

        Comment

        • dpi
          New Member
          • Jan 2007
          • 11

          #5
          So basically I want the value from 'adult_attend_# ' to be multplied by 160, and the resulting total to appear in the 'adult_attend_t otal' field. Then I want the values of 'adult_attend_t otal' and 'child_attend_t otal' to be added together in 'total' field.

          There is actually going to be more to the form, but basically using the same concept, one number that is inputed needs to be multiplied by a fixed number to show a total for that line, then multiple totals added together for a grand total.

          Thanks


          Code:
          <form id="FormName" action="apgform1.7.php"  method="get" name="FormName">
          <table width="422" border="0" cellspacing="2" cellpadding="0">
          <tr>
          
          <td width="71">Name</td>
          
          <td colspan="2"><input type="text" name="name" size="24" tabindex="1"/>
          </td>
          </tr>
          <tr>
          
          <td width="71">Address</td>
          
          <td colspan="2"><input type="text" name="address" size="24" tabindex="2"/>
          </td>
          </tr>
          <tr>
          
          <td width="71">Phone</td>
          
          <td colspan="2"><input type="text" name="phone" size="24" tabindex="3" /></td>
          </tr>
          <tr>
          
          <td width="71"></td>
          
          <td><input type="submit" name="submitted" tabindex="18" /></td>
          
          <td></td>
          </tr>
          </table>
          <fieldset>
          <legend align="left">Conference Registration Options</legend>
          <table width="508" border="0" cellspacing="2" cellpadding="3">
          
          <tr>
          
          <td colspan="5" width="505">Full Conferecne Registration <font size="2">(From Firday Home Hospitality through Sunday Brunch)</font></td>
          
          </tr>
          
          <tr>
          
          <td width="112"><font size="2">Adults Attending</font></td>
          
          <td width="24"><font size="2"><input type="text" name="adult_attend_#" size="2" tabindex="1" /></font></td>
          
          <td width="66"><font size="2">x $160.00=</font></td>
          
          <td colspan="2" align="left" width="279"><font size="2">$<input type="text" name="adult_attend_total" readonly="readonly" size="12" /></font></td>
          
          </tr>
          
          <tr>
          
          <td width="112"><font size="2">Children attending</font></td>
          
          <td width="24"><input type="text" name="children_attend_#" size="2" tabindex="2" /></td>
          
          <td width="66"><font size="2">x $95.00=</font></td>
          
          <td colspan="2" align="left" width="279"><font size="2">$<input type="text" name="child_attend_total" readonly="readonly" size="12" /></font></td>
          
          </tr>
          
          <tr>
          
          <td colspan="3" align="left" width="218"><font size="2">Total Conference Registration Fee</font></td>
          
          <td align="left" width="103"><font size="2">$<input type="text" name="total" readonly="readonly" size="12" /></font></td>
          
          <td width="168"><button name="buttonName" type="button">recalculate</button></td>
          
          </tr>
          
          </table>
          
          </fieldset>
          </form>
          Last edited by acoder; Jan 29 '07, 08:32 PM. Reason: Code in proper tags + removed extra whitespace

          Comment

          • dpi
            New Member
            • Jan 2007
            • 11

            #6
            Any ideas?

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              This should really be in the Javascript forum, but I'll answer here.

              Set the id for your input fields, e.g.
              [HTML]<input type="text" name="adult_att end_#" id="adult_atten d" ...>[/HTML]
              then use, e.g.
              Code:
              document.getElementById('adult_attend').value
              to access the values. Set these values to variables, e.g.
              Code:
              var attendno = ...
              then use these variables to perform the calculations, e.g.
              Code:
              var adultTotal = attendno * 160;
              The rest should be easy. Follow the links that I gave too.

              Post your code if you get stuck (in code tags please).

              Hope that helps.

              Comment

              • dpi
                New Member
                • Jan 2007
                • 11

                #8
                isnt this the javascript forum?

                Ok, bare with me as I am not a programmer.

                So does the:

                Code:
                document.getElementById('adult_attend_full_#').value
                and

                Code:
                var adult_attend_full_total = adult_attend_full_# * 160;
                Need to be in a <script></script> in the header?

                Do i need to do the:
                Code:
                document.getElementById('adult_attend_full_#').value
                for each value?

                This is what I had put in, but it didnt work:

                Code:
                <script>document.getElementById('adult_attend_full_#').value
                																	document.getElementById('adult_attend_full_total').value
                																	
                																	var adult_attend_full_total = adult_attend_full_# * 160;
                																	
                																	</script>
                Do i need a "Calculate" button or anything, or is it automatic?

                Thanks again.

                Cory

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by dpi
                  isnt this the javascript forum?
                  Yes, this is the Javascript forum, but it was moved from the HTML/CSS forum after I posted that message.

                  Originally posted by dpi
                  So does the:

                  Code:
                  document.getElementById('adult_attend_full_#').value
                  and

                  Code:
                  var adult_attend_full_total = adult_attend_full_# * 160;
                  Need to be in a <script></script> in the header?
                  Yes, in a function in the head section of an HTML page.

                  Originally posted by dpi
                  Do i need to do the:
                  Code:
                  document.getElementById('adult_attend_full_#').value
                  for each value?

                  This is what I had put in, but it didnt work:

                  Code:
                  <script>document.getElementById('adult_attend_full_#').value
                  document.getElementById('adult_attend_full_total').value
                  var adult_attend_full_total = adult_attend_full_# * 160;
                  </script>
                  Do i need a "Calculate" button or anything, or is it automatic?
                  That is not going to work because it needs to go in a function, e.g.

                  Code:
                  <script>
                  function calculate() {
                   document.getElementById('adult_attend_full_#').value
                   document.getElementById('adult_attend_full_total').value
                   var adult_attend_full_total = adult_attend_full_# * 160;
                  }
                  </script>
                  It's up to you how you wish to calculate it. You can use a button and use the onclick handler:
                  [HTML]<input type="button" name="calculate " value="Calculat e" onclick="calcul ate()">[/HTML]
                  or you can use and onblur handler on a text input box in the same manner. It's up to you.

                  Comment

                  • dpi
                    New Member
                    • Jan 2007
                    • 11

                    #10
                    I appreciate all your help. I still cant seem to get it to work. I simplified it down so I could figure it out, here is the code with what you suggested:

                    Code:
                    <html>
                    
                    	<head>
                    		
                    		<script><!--
                    function calculate() {
                     document.getElementById('adult_attend_full_#').value
                     document.getElementById('adult_attend_full_total').value
                     var "adult_attend_full_total" = "adult_attend_full_#" * 160;
                    }
                    //-->
                    </script>
                    	</head>
                    
                    	<body>
                    		<form id="FormName" action="(EmptyReference!)" method="get" name="FormName">
                    			<fieldset>
                    				<legend align="left">Conference Registration Options</legend>
                    				<table width="520" border="0" cellspacing="2" cellpadding="3">
                    					<tr>
                    						<td colspan="5" align="left" bgcolor="silver" width="510"><strong>Full Conferecne Registration</strong> <font size="2">(From Friday Home Hospitality through Sunday Brunch)</font></td>
                    					</tr>
                    					<tr>
                    						<td align="left" width="111"><font size="2">Adults Attending</font></td>
                    						<td align="left" width="26"><font size="2"><input type="text" name="adult_attend_full_#" id="adult_attend_full_#" size="2" tabindex="1" /></font></td>
                    						<td align="left" width="64"><font size="2">x $160.00=</font></td>
                    						<td colspan="2" align="left" width="285"><font size="2">$<input type="text" name="adult_attend_full_total" id="adult_attend_full_total" size="12" /><input type="button" name="calculate" value="Calculate" onclick="calculate()"></font></td>
                    					</tr>
                    				</table>
                    			</fieldset>
                    		</form>
                    		<p>
                    			
                    		</p>
                    	</body>
                    
                    </html>
                    Still doesnt want to work for me.

                    Thanks again, I really appreciate this.

                    Cory

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Change your calculate() function to the following:
                      Code:
                      function calculate() {
                       var adultAttend = document.getElementById('adult_attend_full_#').value;
                       document.getElementById('adult_attend_full_total').value = adult_attend_full_# * 160;
                      }
                      or similar. Notice that I have removed the quotes around the variable. Also, you cannot just say "get the value" as you have done in your code, you have to set it to a variable as I have done and then you can use that in calculating the total.

                      Hope that helps.

                      Comment

                      • dpi
                        New Member
                        • Jan 2007
                        • 11

                        #12
                        I just noticed I didnt have any link for the form process the info, or a submit button on this one (it is on the real one) I didnt think it matters.




                        Originally posted by dpi
                        I appreciate all your help. I still cant seem to get it to work. I simplified it down so I could figure it out, here is the code with what you suggested:

                        Code:
                        <html>
                        
                        	<head>
                        		
                        		<script><!--
                        function calculate() {
                         document.getElementById('adult_attend_full_#').value
                         document.getElementById('adult_attend_full_total').value
                         var "adult_attend_full_total" = "adult_attend_full_#" * 160;
                        }
                        //-->
                        </script>
                        	</head>
                        
                        	<body>
                        		<form id="FormName" action="(EmptyReference!)" method="get" name="FormName">
                        			<fieldset>
                        				<legend align="left">Conference Registration Options</legend>
                        				<table width="520" border="0" cellspacing="2" cellpadding="3">
                        					<tr>
                        						<td colspan="5" align="left" bgcolor="silver" width="510"><strong>Full Conferecne Registration</strong> <font size="2">(From Friday Home Hospitality through Sunday Brunch)</font></td>
                        					</tr>
                        					<tr>
                        						<td align="left" width="111"><font size="2">Adults Attending</font></td>
                        						<td align="left" width="26"><font size="2"><input type="text" name="adult_attend_full_#" id="adult_attend_full_#" size="2" tabindex="1" /></font></td>
                        						<td align="left" width="64"><font size="2">x $160.00=</font></td>
                        						<td colspan="2" align="left" width="285"><font size="2">$<input type="text" name="adult_attend_full_total" id="adult_attend_full_total" size="12" /><input type="button" name="calculate" value="Calculate" onclick="calculate()"></font></td>
                        					</tr>
                        				</table>
                        			</fieldset>
                        		</form>
                        		<p>
                        			
                        		</p>
                        	</body>
                        
                        </html>
                        Still doesnt want to work for me.

                        Thanks again, I really appreciate this.

                        Cory

                        Comment

                        • dpi
                          New Member
                          • Jan 2007
                          • 11

                          #13
                          Originally posted by acoder
                          Change your calculate() function to the following:
                          Code:
                          function calculate() {
                           var adultAttend = document.getElementById('adult_attend_full_#').value;
                           document.getElementById('adult_attend_full_total').value = adult_attend_full_# * 160;
                          }
                          Thanks, I know I just must be completly missing something here, but this still wont work:

                          Code:
                          <html>
                          
                          	<head>
                          		
                          		<script><!--
                          		
                          		function calculate() {
                           var adultAttend = document.getElementById('adult_attend_full_#').value;
                           document.getElementById('adult_attend_full_total').value = adult_attend_full_# * 160;
                          }
                          //-->
                          </script>
                          	</head>
                          
                          	<body>
                          		<form id="FormName" action="(EmptyReference!)" method="get" name="FormName">
                          			<fieldset>
                          				<legend align="left">Conference Registration Options</legend>
                          				<table width="520" border="0" cellspacing="2" cellpadding="3">
                          					<tr>
                          						<td colspan="5" align="left" bgcolor="silver" width="510"><strong>Full Conferecne Registration</strong> <font size="2">(From Friday Home Hospitality through Sunday Brunch)</font></td>
                          					</tr>
                          					<tr>
                          						<td align="left" width="111"><font size="2">Adults Attending</font></td>
                          						<td align="left" width="26"><font size="2"><input type="text" name="adult_attend_full_#" id="adult_attend_full_#" size="2" tabindex="1" /></font></td>
                          						<td align="left" width="64"><font size="2">x $160.00=</font></td>
                          						<td colspan="2" align="left" width="285"><font size="2">$<input type="text" name="adult_attend_full_total" id="adult_attend_full_total" size="12" /><input type="button" name="calculate" value="Calculate" onclick="calculate()"></font></td>
                          					</tr>
                          				</table>
                          			</fieldset>
                          		</form>
                          		<p>
                          			
                          		</p>
                          	</body>
                          
                          </html>
                          Cory

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            Sorry, I noticed a mistake I made. Change your calculate() function to:
                            Code:
                            function calculate() {
                             var adultAttend = document.getElementById('adult_attend_full_#').value;
                             document.getElementById('adult_attend_full_total').value = adultAttend * 160;
                            }
                            or similar. Notice that I have used the variable adultAttend that I assigned the value of the adult attend figure to. In the previous code, I accidentally left in adult_attend_fu ll_# which is not set to anything!

                            Comment

                            • dpi
                              New Member
                              • Jan 2007
                              • 11

                              #15
                              Originally posted by acoder
                              Sorry, I noticed a mistake I made. Change your calculate() function to:
                              Code:
                              function calculate() {
                               var adultAttend = document.getElementById('adult_attend_full_#').value;
                               document.getElementById('adult_attend_full_total').value = adultAttend * 160;
                              }
                              or similar. Notice that I have used the variable adultAttend that I assigned the value of the adult attend figure to. In the previous code, I accidentally left in adult_attend_fu ll_# which is not set to anything!
                              Thanks, that all makes sense, but it still wont do anything.
                              http://www.rotarytrici ties.com/test.html

                              Comment

                              Working...