ballhogjoni Posted December 17, 2006 Share Posted December 17, 2006 This is my script to validate a form using php. For some reason its not working. Please help me. I'm sorry if its long. I can't isolate the problem.<html> <head> <title>Form Validation</title> </head> <body> <? function error_bool($error, $field) { if($error[$field]) { print("<td style=color:red>"); } else { print("<td>"); } } //This is where we connect to the DB//Taken out till script is ready function show_form() { global $HTTP_POST_VARS, $print_again, $error; ?> <form action="" method="post"> <table width="400" border="1" cellspacing="0" cellpadding="0"> <tr> <?php error_bool($error, "firstname"); ?> First Name </td> <td><input name="firstname" type="text" id="firstname" value="<? echo $_POST["firstname"]; ?>"></td> </tr> <tr> <?php error_bool($error, "lastname"); ?> Last Name </td> <td><input name="lastname" type="text" id="lastname" value="<? echo $_POST["lastname"]; ?>"></td> </tr> <?php error_bool($error, "address"); ?> Address </td> <td><input name="address" type="text" id="address" value="<? echo $_POST["address"]; ?>"></td> </tr> </tr> <?php error_bool($error, "city"); ?> City </td> <td><input name="city" type="text" id="city" value="<? echo $_POST["city"]; ?>"></td> </tr></tr> <?php error_bool($error, "state"); ?> State </td> <td><select name="state" type="text" id="state" value="<? echo $_POST["state"]; ?>"> <option selected value="">Select State</option> <option value="AL">AL - Alabama</option> <option value="AK">AK - Alaska</option> <option value="AZ">AZ - Arizona</option> <option value="AR">AR - Arkansas</option> <option value="CA">CA - California</option> <option value="CO">CO - Colorado</option> <option value="CT">CT - Connecticut</option> <option value="DE">DE - Delaware</option> <option value="DC">DC - District of Columbia</option> <option value="FL">FL - Florida</option> <option value="GA">GA - Georgia</option> <option value="HI">HI - Hawaii</option> <option value="ID">ID - Idaho</option> <option value="IL">IL - Illinois</option> <option value="IN">IN - Indiana</option> <option value="IA">IA - Iowa</option> <option value="KS">KS - Kansas</option> <option value="KY">KY - Kentucky</option> <option value="LA">LA - Louisiana</option> <option value="ME">ME - Maine</option> <option value="MD">MD - Maryland</option> <option value="MA">MA - Massachusetts</option> <option value="MI">MI - Michigan</option> <option value="MN">MN - Minnesota</option> <option value="MS">MS - Mississippi</option> <option value="MO">MO - Missouri</option> <option value="MT">MT - Montana</option> <option value="NE">NE - Nebraska</option> <option value="NV">NV - Nevada</option> <option value="NH">NH - New Hampshire</option> <option value="NJ">NJ - New Jersey</option> <option value="NM">NM - New Mexico</option> <option value="NY">NY - New York</option> <option value="NC">NC - North Carolina</option> <option value="ND">ND - North Dakota</option> <option value="OH">OH - Ohio</option> <option value="OK">OK - Oklahoma</option> <option value="OR">OR - Oregon</option> <option value="PA">PA - Pennsylvania</option> <option value="PR">PR - Puerto Rico</option> <option value="RI">RI - Rhode Island</option> <option value="SC">SC - South Carolina</option> <option value="SD">SD - South Dakota</option> <option value="TN">TN - Tennessee</option> <option value="TX">TX - Texas</option> <option value="UT">UT - Utah</option> <option value="VT">VT - Vermont</option> <option value="VA">VA - Virginia</option> <option value="WA">WA - Washington</option> <option value="WV">WV - West Virginia</option> <option value="WI">WI - Wisconsin</option> <option value="WY">WY - Wyoming</option> </select></td> </tr></tr> <?php error_bool($error, "zipcode"); ?> Zip Code </td> <td><input name="zipcode" type="text" id="zipcode" value="<? echo $_POST["zipcode"]; ?>"></td> </tr> <tr> <?php error_bool($error, "phone"); ?> Phone </td> <td><input name="phone" type="text" id="phone" value="<? echo $_POST["phone"]; ?>"></td> </tr> <tr> <?php error_bool($error, "email"); ?> Email </td> <td><input name="email" type="text" id="email" value="<? echo $_POST["email"]; ?>"></td> </tr> <tr> <td><input type="image" src="article_cash/images/8.gif" name="Submit" value="Submit"></td> <td> </td> </tr> </table> </form> <? } if(isset($_POST["Submit"])) { check_form(); } else { show_form(); } function check_email_address($email) { // First, we check that there's one @ symbol, and that the lengths are right if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) { // Email invalid because wrong number of characters in one section, or wrong number of @ symbols. return false; } // Split it into sections to make life easier $email_array = explode("@", $email); $local_array = explode(".", $email_array[0]); for ($i = 0; $i < sizeof($local_array); $i++) { if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) { return false; } } if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name $domain_array = explode(".", $email_array[1]); if (sizeof($domain_array) < 2) { return false; // Not enough parts to domain } for ($i = 0; $i < sizeof($domain_array); $i++) { if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) { return false; } } } return true; } function check_form() { global $HTTP_POST_VARS, $error, $print_again; $error['firstname'] = false; if($_POST["firstname"]=="") { $error['firstname'] = true; $print_again = true; $message="You Must Enter Your First Name<br>"; } $error['lastname'] = false; if($_POST["lastname"]=="") { $error['lastname'] = true; $print_again = true; $message="You Must Enter Your Last Name<br>"; } $error['address'] = false; if($_POST["address"]=="") { $error['address'] = true; $print_again = true; $message="You Must Enter Your Address<br>"; } $error['city'] = false; if($_POST["city"]=="") { $error['city'] = true; $print_again = true; $message="You Must Enter Your City<br>"; } $error['state'] = false; if($_POST["state"]=="") { $error['state'] = true; $print_again = true; $message="You Must Enter Your State<br>"; } $error['zipcode'] = false; if($_POST["zipcode"]=="") { $error['zipcode'] = true; $print_again = true; $message="You Must Enter Your Zip Code<br>"; } $error['phone'] = false; if($_POST["phone"]=="") { $error['phone'] = true; $print_again = true; $message.="You Must Enter Your Phone Number<br>"; } if(!check_email_address($_POST['email'])) { $error['email'] = true; $print_again = true; $message.="Invalid Email Address<br>"; } if($print_again) { show_form(); } else { show_form(); $message="All Fields are valid <br> Now, In this way you can validate the other textfield as well<br> You can insert data into table"; } echo ("$message"); } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/30945-form-validation-help/ Share on other sites More sharing options...
kenrbnsn Posted December 17, 2006 Share Posted December 17, 2006 Since you are using an image for the submit button, checking if the submit button is pressed is slightly different when using MSIE (your test will work when you use Firefox).You should test to see if the $_POST['Submit_x'] or $_POST['Submit_y'] is set.Ken Link to comment https://forums.phpfreaks.com/topic/30945-form-validation-help/#findComment-142789 Share on other sites More sharing options...
ballhogjoni Posted December 17, 2006 Author Share Posted December 17, 2006 Thx i figured it out. Now the question is how can I redirect to a different page once the form is validated? Link to comment https://forums.phpfreaks.com/topic/30945-form-validation-help/#findComment-142799 Share on other sites More sharing options...
mlin Posted December 17, 2006 Share Posted December 17, 2006 you can use javascript, or better yet, you can do all your validation before anything is output to the browser, and use:header("Location: new_uri"); Link to comment https://forums.phpfreaks.com/topic/30945-form-validation-help/#findComment-143103 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.