We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,154 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

stop php form submittal with javascript?

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>
3
Contributors
2
Replies
5 Days
Discussion Span
1 Year Ago
Last Updated
3
Views
dancks
Junior Poster
144 posts since Nov 2010
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 0

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

Airshow
WiFi Lounge Lizard
Moderator
2,782 posts since Apr 2009
Reputation Points: 370
Solved Threads: 388
Skill Endorsements: 9
<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

baig772
Junior Poster
155 posts since Mar 2011
Reputation Points: 29
Solved Threads: 10
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0653 seconds using 2.75MB