• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

jquery on submit not running

I can't figure out what I am missing.  The page is called index.html.  
When i click the submit button, the jquery submit code does not seem to be running at all.  The text boxes don't get the value they should and the alerts are not even firing.

eventually when this is working the action is going to be changed to a php file that will use the numbers in the text boxes.

What am I missing???

<html>
<head>
	<style type="text/css">
		.option { 
			text-align: center;
		}

		th {
			width: 100px;
			background-color:#6699FF;
		}

		th.sectionHeader {
			text-align: left;
			font-size: 20;
			background-color:transparent;
		}

		.altShade {
			background-color:#ABC6BD;
		}
	</style>

	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
	<script>
	$('#survey').submit(function()
	{ alert("got here");
		var result = 0;
		for (i=1; i<8; i++)
		{
			result = 0;
			$(".S"+i+":checked").each(function () {
				result += parseFloat($(this).val());
			});
			alert(result);
			$("#sectionTotal"+i).val(result);
		}
		alert(result);
		return true; 
	});
	</script>

</head>

<body>		
	<form id="survey" action="index.html" method="post">

	<table id='section1' border='0'>
		<tr>
			<th class='sectionHeader'>Teamwork</th>
			<th>Strongly Disagree</th>
			<th>Disagree</th>
			<th>Neither</th>
			<th>Agree</th>
			<th>Strongly Agree</th>
		</tr>
		<tr>
			<td class='question'>Section 1 Question 1</td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="5"/></td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="4"/></td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="3"/></td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="2"/></td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="1"/></td>
		</tr>
		<tr class='altShade'>
			<td class='question'>Section 1 Question 2</td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="5"/></td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="4"/></td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="3"/></td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="2"/></td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="1"/></td>
		</tr>
		<tr>
			<td class='question'Section 1 Question 3</td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="5"/></td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="4"/></td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="3"/></td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="2"/></td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="1"/></td>
		</tr>
		<input type='text' id='sectionTotal1' value=''/>
	</table>

	<table id='section2' border='0'>
		<tr>
			<th class='sectionHeader'>Teamwork</th>
			<th>Strongly Disagree</th>
			<th>Disagree</th>
			<th>Neither</th>
			<th>Agree</th>
			<th>Strongly Agree</th>
		</tr>
		<tr>
			<td class='question'>Section 2 Question 1</td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="5"/></td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="4"/></td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="3"/></td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="2"/></td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="1"/></td>
		</tr>
		<tr class='altShade'>
			<td class='question'>Section 2 Question 2</td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="5"/></td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="4"/></td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="3"/></td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="2"/></td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="1"/></td>
		</tr>
		<tr>
			<td class='question'>Section 2 Question 3</td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="5"/></td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="4"/></td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="3"/></td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="2"/></td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="1"/></td>
		</tr>
		<input type='text' id='sectionTotal2' value=''/>
	</table>

	</br></br>
	<input type="submit" value="View your results">
	</form>
</body>
</html>

Open in new window

0
KCTechNet
Asked:
KCTechNet
  • 2
2 Solutions
 
Melih SARICAOwnerCommented:
When u set the submit of the form. Form on Js side is createted yet.

Move ur <script> block after the form.. then u ll see its working

<html>
<head>
	<style type="text/css">
		.option { 
			text-align: center;
		}

		th {
			width: 100px;
			background-color:#6699FF;
		}

		th.sectionHeader {
			text-align: left;
			font-size: 20;
			background-color:transparent;
		}

		.altShade {
			background-color:#ABC6BD;
		}
	</style>

	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
	
</head>

<body>		
	<form id="survey" action="index.html" method="post">

	<table id='section1' border='0'>
		<tr>
			<th class='sectionHeader'>Teamwork</th>
			<th>Strongly Disagree</th>
			<th>Disagree</th>
			<th>Neither</th>
			<th>Agree</th>
			<th>Strongly Agree</th>
		</tr>
		<tr>
			<td class='question'>Section 1 Question 1</td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="5"/></td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="4"/></td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="3"/></td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="2"/></td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="1"/></td>
		</tr>
		<tr class='altShade'>
			<td class='question'>Section 1 Question 2</td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="5"/></td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="4"/></td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="3"/></td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="2"/></td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="1"/></td>
		</tr>
		<tr>
			<td class='question'Section 1 Question 3</td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="5"/></td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="4"/></td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="3"/></td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="2"/></td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="1"/></td>
		</tr>
		<input type='text' id='sectionTotal1' value=''/>
	</table>

	<table id='section2' border='0'>
		<tr>
			<th class='sectionHeader'>Teamwork</th>
			<th>Strongly Disagree</th>
			<th>Disagree</th>
			<th>Neither</th>
			<th>Agree</th>
			<th>Strongly Agree</th>
		</tr>
		<tr>
			<td class='question'>Section 2 Question 1</td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="5"/></td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="4"/></td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="3"/></td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="2"/></td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="1"/></td>
		</tr>
		<tr class='altShade'>
			<td class='question'>Section 2 Question 2</td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="5"/></td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="4"/></td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="3"/></td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="2"/></td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="1"/></td>
		</tr>
		<tr>
			<td class='question'>Section 2 Question 3</td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="5"/></td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="4"/></td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="3"/></td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="2"/></td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="1"/></td>
		</tr>
		<input type='text' id='sectionTotal2' value=''/>
	</table>

	</br></br>
	<input type="submit" value="View your results">
	</form>
<script>
	$('#survey').submit(function()
	{ alert("got here");
		var result = 0;
		for (i=1; i<8; i++)
		{
			result = 0;
			$(".S"+i+":checked").each(function () {
				result += parseFloat($(this).val());
			});
			alert(result);
			$("#sectionTotal"+i).val(result);
		}
		alert(result);
		return true; 
	});
	</script>

</body>
</html>

Open in new window

0
 
KCTechNetAuthor Commented:
that did the trick.  

But I notice that $_POST["sectionTotal1"] has no value on the 'receiving' php page, but  $_POST["S1Q1"] does.  I am guessing that is because the sectionTotal1 is populated during the submit and not prior to??  Although when I change the submit function to return false I see that the text boxes are populated correctly.
0
 
Julian HansenCommented:
Two things

1. Although having your scripts at the end of your page is a good practice, you should also wrap your jquery script in document ready code like shown below. This will guarantee that your code does not run until the page has loaded.
$(function() {
  $('#survey').submit(function()  {
  alert("got here");
    var result = 0;
    for (i=1; i<8; i++)
    {
      result = 0;
      $(".S"+i+":checked").each(function () {
        result += parseFloat($(this).val());
      });
      alert(result);
      $("#sectionTotal"+i).val(result);
    }
    alert(result);
    return true; 
  });
});

Open in new window


2. Your sectionTotal value is not coming through because the element does not have a name.  Change to
<input type='text' id='sectionTotal1' name='sectionTotal' value=''/>

Open in new window


That should resolve both issues
0
 
KCTechNetAuthor Commented:
Ahh.   I missed that!!  Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now