bugzy Posted March 30, 2012 Share Posted March 30, 2012 Hello I have this regisration form with some javascript on it. I have this code to my submit button <input type="button" name="Submit" value="Submit" onclick="SubmitForm(); return false;" > Everytime I'm trying to click the submit button it aint working it's not even validating all the values that I have put there. I wonder what the problem is. Here's the full code of that page <?php header("Expires: Thu, 17 May 2001 10:17:17 GMT"); // Date in the past header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header ("Pragma: no-cache"); // HTTP/1.0 session_start(); if (!isset($_SESSION['SESSION'])) require ( "./db_connection.php"); $arVals = array(); require_once("./session_func.php"); // make sure the seesion vars are initialized... reset ($arVals); while (list ($key, $val) = each ($arVals)) { if (!isset($_SESSION[$key])) $_SESSION[$key] = ""; if ($_SESSION[$key] == "NULL") $_SESSION[$key] = ""; }$arVals = array(); require_once("./session_func.php"); // make sure the seesion vars are initialized... reset ($arVals); while (list ($key, $val) = each ($arVals)) { if (!isset($_SESSION[$key])) $_SESSION[$key] = ""; if ($_SESSION[$key] == "NULL") $_SESSION[$key] = ""; } if ($_SESSION["status"] == "") $_SESSION["status"] = 0; $flg = ""; $error = ""; if (isset($HTTP_GET_VARS["flg"])) $flg = $HTTP_GET_VARS["flg"]; switch ($flg) { case "yellow": $error = "<br><font class=\"txt12_red\">That Email Address already exists in our Database.<br>Please Select Another.<BR></font>"; break; case "red": $error = "<br><font class=\"txt12_red\">Please fill out all the required fields.<br>Please Try Again.<BR></font>"; break; case "blue": $error = "<br><font class=\"txt12_red\">Your Session has Expired.<br>Please Login Again.</font><BR>"; break; case "pink": $error = "<br><font class=\"txt12_red\"><BR>The Special Code you entered is not valid.<br>Please Try Again or Leave that field blank. </font><BR>"; break; case "white": $error = "<br><font class=\"txt12_red\"><BR>The fields are too long for our Database.<br>Please correct your data via this form.</font> <BR>"; break; default: $error = ""; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>User Registration Form</title> <script language="javascript"> function SubmitForm() { var form = document.forms[0]; var bRequired = true; if ((form.access_period.selectedIndex < 1) || (form.fname.value.length < 1) || (form.lname.value.length < 1) || (form.email.value.length < 1) || (form.phone.value.length < 1) || (form.city.value.length < 1) || (form.status.value.length < 1) || (form.province.value.length < 1) || (form.password.value.length < 1) || (form.password2.value.length < 1) { alert("Please fill out all the required fields."); bRequired = false; } if (!bRequired) return false; form.status.value = form.status.selectedIndex; form.submit(); } </script> </head> <body><center><br /><br /> <?php echo $error; ?><br /><br /> <form name="registration" method="post" action="./registered.php"> <table> <tr> <td>First Name:</td> <td><input type="text" name="fname" value="<?php $_SESSION['fname'] ?>" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>Last Name:</td> <td><input type="text" name="lname" value="<?php $_SESSION['lname'] ?>" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>E-Mail Address:</td> <td><input type="text" name="email" value="<?php $_SESSION['email'] ?>" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>Phone:</td> <td><input type="text" name="phone" value="<?php $_SESSION['phone'] ?>" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>City:</td> <td><input type="text" name="city" value="<?php $_SESSION['city'] ?>" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>Status:</td> <td> <select name="status" id="status"> <option>Pls. Select One <option value="single">Single <option value="relationship">In a Relationship <option value="complicated">It's Complicated </select> </td> <td>*</td> </tr> <tr> <td>Province:</td> <td><input type="text" name="province" value="<?php echo $_SESSION['province'] ?>" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>Password:</td> <td><input type="password" name="password" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>Confirm Password:</td> <td><input type="password" name="password2" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td> <input type="reset" name="Submit2" value="Reset"></td> <td> <input type="button" name="Submit" value="Submit" onclick="SubmitForm(); return false;" ></td> </tr> </table> </form> </center> <script language="javascript"> // set the selection box values... form.status.selectedIndex = parseInt("<?php echo $_SESSION['status'] ?>"); </script> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/260036-submit-button-form-is-not-working/ Share on other sites More sharing options...
Psycho Posted March 30, 2012 Share Posted March 30, 2012 This is a JavaScript problem, not a PHP problem, moving to the correct forum. Now, to your problem, I don't think that form will ever POST because you have a "return false" in the trigger. But, you should not call your form validation in the submit button because a form can also be submitted by using the enter key if you are not in an input field. the proper way to do it is with the onsubmit trigger of the form. Then the validation function(s) will return a true if validation passes and the form posts, else it returns false and the form doesn't post. But, your current JS code has errors. You are missing a closing paren after the list of conditions at the beginning of the function. Also, you are referencing fields that don't exist (i.e. access_period). Here is the updated HTML portion of your script which should work <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>User Registration Form</title> <script language="javascript"> function validateForm() { var form = document.forms[0]; if (!form.fname.value.length || !form.lname.value.length || !form.email.value.length || !form.phone.value.length || !form.city.value.length || !form.status.selectedIndex || !form.province.value.length || !form.password.value.length || !form.password2.value.length ) { //Validation failed alert("Please fill out all the required fields."); return false; } //Validation passed return true; } </script> </head> <body><center><br /><br /> <pre> <?php print_r($_POST); ?> </pre> <?php echo $error; ?><br /><br /> <form name="registration" method="post" action="./registered.php" onsubmit="return validateForm();""> <table> <tr> <td>First Name:</td> <td><input type="text" name="fname" value="<?php $_SESSION['fname'] ?>" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>Last Name:</td> <td><input type="text" name="lname" value="<?php $_SESSION['lname'] ?>" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>E-Mail Address:</td> <td><input type="text" name="email" value="<?php $_SESSION['email'] ?>" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>Phone:</td> <td><input type="text" name="phone" value="<?php $_SESSION['phone'] ?>" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>City:</td> <td><input type="text" name="city" value="<?php $_SESSION['city'] ?>" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>Status:</td> <td> <select name="status" id="status"> <option>Pls. Select One <option value="single">Single <option value="relationship">In a Relationship <option value="complicated">It's Complicated </select> </td> <td>*</td> </tr> <tr> <td>Province:</td> <td><input type="text" name="province" value="<?php echo $_SESSION['province'] ?>" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>Password:</td> <td><input type="password" name="password" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>Confirm Password:</td> <td><input type="password" name="password2" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td> <input type="reset" name="Reset" value="Reset"></td> <td> <input type="submit" name="Submit" value="Submit" ></td> </tr> </table> </form> </center> <script language="javascript"> // set the selection box values... form.status.selectedIndex = parseInt("<?php echo $_SESSION['status'] ?>"); </script> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/260036-submit-button-form-is-not-working/#findComment-1332868 Share on other sites More sharing options...
bugzy Posted March 30, 2012 Author Share Posted March 30, 2012 Hey thank you very much! It's now redirecting but it's not validating the suppose no. of character on the function. Do I need to put some values there? Sorry I'm new in both php and clueless about javascript. I only search that javascript code online. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/260036-submit-button-form-is-not-working/#findComment-1332869 Share on other sites More sharing options...
bugzy Posted March 30, 2012 Author Share Posted March 30, 2012 Hey nevermind! Got it working now! Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/260036-submit-button-form-is-not-working/#findComment-1332871 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.