1,105,221 Community Members

stop php form submittal with javascript?

Member Avatar
dancks
Junior Poster
149 posts since Nov 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I was given this assignment:
Create an html form. Include at least two text boxes, at least one of which must be numeric, a set of at least three radio buttons, a submit button and a reset button. Upload to your account. Make sure the permissions are correct. Name it hw1.html. Create a php script called hw1.php using an ASCII text editor that takes the data and creates a simple response html page verifying the data entered. For full credit, an if statement using the value of the numeric field must be included to give different output (such as messages, colors, etc.) and error checking must be included. Submit the entire url in the dropbox.

I didn't know how to go about the "numeric only" thing so I decided I would use javascript to check the value before submittal. Unfortunately, it doesn't run at all and the form gets submitted regardless. Since its past due I feel comfortable asking for help. I want to know how I could have done this better and how I could've done it in javascript.

h1.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml$
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--
http://javascript.about.com/library/blva…
-->
<title>Homework 1</title>
<script type="text/javascript">
<!--
function onlyNumbers()
{
var b = document.getElementById('id').value;
if(isNaN(b))
{
alert("Numeric value for student id needed");
window.location.href="h1.html";
}
}
function checkForm()
{
var b = document.getElementById('id').value;
var d = b * 2;
if(d == b)
{
alert("Numeric value for student id needed");
document.forms['form'].submitit.disabled…
if(navigator.appName == "Microsoft Internet Explorer")
{
window.document.execCommand('Stop');
}
else
{
window.stop();
}
window.navigate("cs4.sunyocc.edu/~j.d.da…
}
else
{
document.forms['form'].submitit.disabled…
document.forms['form'].submit();
}
}
//-->
</script>
</head>
<body>
<form id="form" name="form" method="post" action="h1.php" onsubmit="checkform();">
<p>Name:<input type="text" name="name" id="name" /></p>
<p>Enter ID:<input type="text" name="id" id="id" /></p>
<p>You Are:

full time: <input type="radio" name="student" id="student" value="full" />

part time: <input type="radio" name="student" id="student" value="part" />
</p>
<p>Financial aid?<input type="checkbox" name="aid" value="yes" id="aid" /></p>
<input type="submit" name="submit" value="submit" />
<input type="button" name="reset" value="reset" onClick="window.navigate("cs4.sunyocc.ed…
</form>
</body>
</html>
Member Avatar
Airshow
WiFi Lounge Lizard
2,798 posts since Apr 2009
Reputation Points: 333 [?]
Q&As Helped to Solve: 393 [?]
Skill Endorsements: 11 [?]
Team Colleague
 
1
 

It's quite simple.

Arrange for checkForm() to return false if any of the validations fails or true if they all pass.

Also, modify the HTML such that the returned value is itself returned by the onsubmit handler:

<form ... onsubmit="return checkform();">

Airshow

Member Avatar
baig772
Junior Poster
165 posts since Mar 2011
Reputation Points: 19 [?]
Q&As Helped to Solve: 11 [?]
Skill Endorsements: 0 [?]
 
0
 
<input type="submit">

always submits the form regardless of any javascript validation
you should try

<input type="button" on click="checkValidation()">

and then submit your form using javascript

form.submit()

function

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: