Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with Form 2

Status
Not open for further replies.

dmears1

Technical User
Jun 18, 2003
208
US
I have a pretty basic form here:
I am using a js function to validate the fields. I have a php statement to check and see if the submit button has been pressed:

Code:
if ($submit) {
//process form

Here's the problem: when you click "Sign Me Up" ($submit), the js function does work, however, after you click "ok" the form is posted anyway without giving the user the chance to make corrections.

I know the problem is with the php statement "if ($submit)--process form" so I've tried changing the condition of the if statement but have had no success.

Here is the js:
Code:
<script language="JavaScript">
function IsFormComplete(FormName) {
	var x       = 0
	var FormOk  = true
	
	while ((x < document.forms[FormName].elements.length) && (FormOk)) {
		if (document.forms[FormName].elements[x].value == '') {
			alert('Please enter the '+document.forms[FormName].elements[x].name +' and try again.')
			document.forms[FormName].elements[x].focus()
			FormOk = false
		}
		x ++
	}
	return FormOk
}

and the php if statement:
Code:
<?php
if ($submit) {
	//process form
	$db = @mysql_connect ('123.123.123.123', 'username', 'password') or
    die("Couldn't connect to localhost database with username: username");
	$db_selected = @mysql_select_db ('table', $db) or
    die("Couldn't hook into database: table");
	$sql = "INSERT INTO table(event, email, fname, lname, address, city, state, zip, country, phone, referral, guest_email, guest_fname, guest_lname, guest_address, guest_city, guest_state, guest_zip, guest_country, guest_phone, cc_number, cc_expiration, ccv_number, cc_name, guest_cc_number, guest_cc_expiration, guest_ccv_number, guest_cc_name) VALUES ('$event', '$email', '$fname', '$lname', '$address', '$city', '$state', '$zip', '$country', '$phone', '$referral', '$guest_email', '$guest_fname', '$guest_lname', '$guest_address', '$guest_city', '$guest_state', '$guest_zip', '$guest_country', '$guest_phone', '$cc_number', '$cc_expiration', '$ccv_number', '$cc_name', '$guest_cc_number', '$guest_cc_expiration', '$guest_ccv_number', '$guest_cc_name')";
	$results = @mysql_query ($sql) or
    die('Statement failed: ' . $sql);
} else {
	//display form
?>

Thanks in advance for any help.

 
That's because you are confusing client side with server side validation.

If you make a form with an action that will submit the form to a script when the submit button is pressed then that is what will happen.

You will need to remove the action and do the actual submission through javascript - once the form data is validated on the client side.

Foamcow Heavy Industries - Web design and ranting
Buy Languedoc wines in the UK
 
The problem is in your submit tag with the onsubmit handler. You need to return the value:
Code:
<form onsubmit="[!]return[/!] IsFormComplete(FormName)">

By the way, further javascript questions should be asked in the javascript forum.

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Thanks for the help. You got me on the right track. On the submit button I had the following HTML:
Code:
<input name="submit" type="submit" value="Sign Me Up!" OnClick=IsFormComplete("signmeup")>
I changed it to this and it seems to have solved the problem:
Code:
<input name="submit" type="submit" value="Sign Me Up!" OnClick="return IsFormComplete('signmeup')">
Thanks again!

 
It is better still to put that code in the form tag like kaht showed. That will be more full proof (if user presses enter key, form is usually submitted but the button was never actually clicked.

Even better is to do your validation on the server. I can turn off javascript with one click and bypass all your validation.
 
Thanks Vragabond.
I will give it a shot in the form tag. I didn't think about the button not being clicked.
I will be adding server-sided validation (php) as well, but I wanted to add the client-side first to ensure I have the js working properly first. This way the form is validated regardless of whether or not js is enabled.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top