lucy Posted August 5, 2009 Share Posted August 5, 2009 Yes its me again! Im writing some validation for a form. The form fully validates how i would like it to but there is a slight problem about notifying the user about erroneous fields. Whenever there is an error the script just loads the form back up again via header( "Location: ../newcustomer.php" ); and changes the variable 'error' to whatever field number caused the problem. The problem is, i do not know how i can use this numeric value to change the CSS of the file so i can make the text red or something similar, on the field which caused the problem. Another problem ive just realised, is once there is a problem, and the user is sent back to the page, all of the data entered into the form has been lost. Can someone please give me some help! newcustomer.php (the main page to display the form): <body> <?php include("php/header.php"); ?> <div id="content"> <?php include("php/newcustomerform.php"); ?> </div> </body> The form itself: <form action="php/newcustomerscript.php" method="post"> <p>Title: <select name="title" id="title" tabindex="1"> <option selected="selected"></option> <option>Mr</option> <option>Mrs</option> <option>Miss</option> </select> </p> <p>Forename: <input type="text" name="fname" id="fname" tabindex="2" /> </p> <p>Surname: <input type="text" name="sname" id="sname" tabindex="3" /> </p> <p> Address line 1: <input type="text" name="add1" id="add1" tabindex="4" /> </p> <p>Address line 2: <input type="text" name="add2" id="add2" tabindex="5" /> </p> <p>County: <input type="text" name="county" id="county" tabindex="6" /> </p> <p>Postcode: <input type="text" name="pc" id="pc" tabindex="7" /> </p> <p>Email: <input type="text" name="email" id="email" tabindex="8" /> </p> <p> <input type="reset" name="clear" id="clear" value="Reset" /> <input type="submit" name="submit" id="submit" value="Submit" /> </p> <p> </p> </form> The script which validates it: <? $user="OMMITED"; //specially created username $password="OMMITED"; $database="OMMITED"; //connect to the database $con = mysql_connect("localhost",$user,$password) or die ('Could not connect: ' . mysql_error()); //select the database mysql_select_db($database, $con); // email validation function function email_valid ($email) { if (eregi("^[a-z0-9._-]+@[a-z0-9._-]+.[a-z]{2,6}$", $email)) { return TRUE; } else { return FALSE; } } //request variables from form $title = $_REQUEST['title']; $fname = $_REQUEST['fname']; $sname = $_REQUEST['sname']; $add1 = $_REQUEST['add1']; $add2 = $_REQUEST['add2']; $county = $_REQUEST['county']; $pc = $_REQUEST['pc']; $email = $_REQUEST['email']; $error = 0; if (empty($title)) { header( "Location: ../newcustomer.php" ); $error = 1; } elseif (empty($fname)) { header( "Location: ../newcustomer.php" ); $error = 2; } elseif (empty($sname)) { header( "Location: ../newcustomer.php" ); $error = 3; } elseif (empty($add1)) { header( "Location: ../newcustomer.php" ); $error = 4; } elseif (empty($add2)) { header( "Location: ../newcustomer.php" ); $error = 5; } elseif (empty($county)) { header( "Location: ../newcustomer.php" ); $error = 6; } elseif (empty($pc)) { header( "Location: ../newcustomer.php" ); $error = 7; } elseif (email_valid ($email) == FALSE) { header( "Location: ../newcustomer.php" ); $error = 8; } //insert data into database if ($error == 0) { mysql_query("INSERT INTO customer (title, fname, sname, add1, add2, county, pc, email) VALUES ('$_POST[title]','$_POST[fname]','$_POST[sname]','$_POST[add1]','$_POST[add2]','$_POST[county]','$_POST[pc]', '$_POST[email]')") or die('Error: ' . mysql_error()); } //close connection mysql_close(); ?> Quote Link to comment Share on other sites More sharing options...
abazoskib Posted August 5, 2009 Share Posted August 5, 2009 hmm i can tthink of the best way to do it, but one way would be to add conditionals around the word 'email' for example that adds inline style color red if error =1. kind of messy to do for each one, but would work. Quote Link to comment Share on other sites More sharing options...
lucy Posted August 5, 2009 Author Share Posted August 5, 2009 I dont quite understand sorry, can you ellaborate? Thanks Lucy Quote Link to comment Share on other sites More sharing options...
abazoskib Posted August 5, 2009 Share Posted August 5, 2009 <?php if($error =1) echo '<span style=\"color=red\" Email </span> '; else echo ' Email:'; ?> <input type="text" name="email" id="email" tabindex="8" /> </p> Quote Link to comment Share on other sites More sharing options...
lucy Posted August 5, 2009 Author Share Posted August 5, 2009 that would change the colour yes, but it would not load the page with the form on, so it would simply show a red bit of text with the error, am i correct? Thanks, Lucy Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 5, 2009 Share Posted August 5, 2009 Your best bet is to submit the form to itself rather than to another script. This way you can do something like this <?php function set_field_value($form) { return isset($_POST[$form]) ? $_POST[$form] : ''; } function show_field_error($form) { global $error; return isset($error[$form]) ? '<span style="color:red"><b>'.$error[$form].'</b></span><br />' : ''; } if(isset($_POST['submit'])) { if(isset($_POST['name']) && empty($_POST['name'])) { $error['name'] = 'Please fill in your name'; } if(isset($_POST['age']) && !is_numeric($_POST['age'])) { $error['age'] = 'Please fill in your age'; } } ?> <form action="" method="post"> <?php echo show_field_error('name'); ?> Name <input type="text" name="name" value="<?php echo set_field_value('name'); ?>" /><br /> <?php echo show_field_error('age'); ?> Age <input type="text" name="age" value="<?php echo set_field_value('age'); ?>" /><br /> <input type="submit" name="submit" value="Submit" /> </form> Quote Link to comment Share on other sites More sharing options...
lucy Posted August 5, 2009 Author Share Posted August 5, 2009 wow that looks confusing for someone who knows little about PHP like myself! How would i get the form to be submitted to itself? Thanks, Lucy Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 5, 2009 Share Posted August 5, 2009 To submit the form to itself you dont set the action attribute in the form tag. As my example shows. Quote Link to comment Share on other sites More sharing options...
lucy Posted August 5, 2009 Author Share Posted August 5, 2009 Ahh right. Thanks. Il give it a bash now. Thanks, Lucy Quote Link to comment Share on other sites More sharing options...
Jago6060 Posted August 5, 2009 Share Posted August 5, 2009 Ahh right. Thanks. Il give it a bash now. Thanks, Lucy To submit the form to itself you dont set the action attribute in the form tag. As my example shows. Just a quick question. I've heard of a "PHP_SELF" function/action. What would be the difference between just leaving action blank and using the "PHP_SELF" action? Quote Link to comment Share on other sites More sharing options...
lucy Posted August 5, 2009 Author Share Posted August 5, 2009 I understand that now, thanks a lot nothing happens when all fields are valid though and the user clicks submit. How can i use the statement which i was previously using to enter data when there are no errors? it was: if ($error == 0) { mysql_query("INSERT INTO customer (title, fname, sname, add1, add2, county, pc, email) VALUES ('$_POST[title]','$_POST[fname]','$_POST[sname]','$_POST[add1]','$_POST[add2]','$_POST[county]','$_POST[pc]', '$_POST[email]')") or die('Error: ' . mysql_error()); } Thanks, Lucy Quote Link to comment Share on other sites More sharing options...
Batosi Posted August 5, 2009 Share Posted August 5, 2009 <?php if (!$error) { ## query } ?> Quote Link to comment Share on other sites More sharing options...
lucy Posted August 5, 2009 Author Share Posted August 5, 2009 Thanks a lot Quote Link to comment Share on other sites More sharing options...
lucy Posted August 5, 2009 Author Share Posted August 5, 2009 I cant figure out where to put it in order to get no errors. Ive tried placing it inside the if(isset($_POST['submit'])) main set of if statements as shown here: if(isset($_POST['submit'])) { if(isset($_POST['title']) && empty($_POST['title'])){ $error['title'] = 'Please enter a title'; } if(isset($_POST['fname']) && empty($_POST['fname'])){ $error['fname'] = 'Please enter your forename'; } if(isset($_POST['sname']) && empty($_POST['sname'])){ $error['sname'] = 'Please enter your surname'; } if(isset($_POST['add1']) && empty($_POST['add1'])){ $error['add1'] = 'Please enter the first line of your address'; } if(isset($_POST['add2']) && empty($_POST['add2'])){ $error['add2'] = 'Please enter the second line of your address'; } if(isset($_POST['county']) && empty($_POST['county'])){ $error['county'] = 'Please enter your county'; } if(isset($_POST['pc']) && empty($_POST['pc'])){ $error['pc'] = 'Please enter your postcode'; } if(isset($_POST['email']) && empty($_POST['email'])){ $error['email'] = 'Please enter your email'; } if (!$error) {mysql_query("INSERT INTO customer (title, fname, sname, add1, add2, county, pc, email) VALUES ('$_POST[title]','$_POST[fname]','$_POST[sname]','$_POST[add1]','$_POST[add2]','$_POST[county]','$_POST[pc]', '$_POST[email]')") or die('Error: ' . mysql_error()); } } which works ok-ish untill i try and redirect the user to another page by using header( "Location: http://www.google.com" ); and then it comes up with the following error, just above the text boxes for input: Warning: Cannot modify header information - headers already sent by (output started at /home/sites/foo/public_html/test/newcustomer.php:12) in /home/sites/foo/public_html/test/php/testvalid.php on line 56 which corresponds directly to the statement i have just added. I cant find a suitable place to put this if statement. Any ideas? Thanks, Lucy Quote Link to comment Share on other sites More sharing options...
watsmyname Posted August 5, 2009 Share Posted August 5, 2009 I cant figure out where to put it in order to get no errors. Ive tried placing it inside the if(isset($_POST['submit'])) main set of if statements as shown here: if(isset($_POST['submit'])) { if(isset($_POST['title']) && empty($_POST['title'])){ $error['title'] = 'Please enter a title'; } if(isset($_POST['fname']) && empty($_POST['fname'])){ $error['fname'] = 'Please enter your forename'; } if(isset($_POST['sname']) && empty($_POST['sname'])){ $error['sname'] = 'Please enter your surname'; } if(isset($_POST['add1']) && empty($_POST['add1'])){ $error['add1'] = 'Please enter the first line of your address'; } if(isset($_POST['add2']) && empty($_POST['add2'])){ $error['add2'] = 'Please enter the second line of your address'; } if(isset($_POST['county']) && empty($_POST['county'])){ $error['county'] = 'Please enter your county'; } if(isset($_POST['pc']) && empty($_POST['pc'])){ $error['pc'] = 'Please enter your postcode'; } if(isset($_POST['email']) && empty($_POST['email'])){ $error['email'] = 'Please enter your email'; } if (!$error) {mysql_query("INSERT INTO customer (title, fname, sname, add1, add2, county, pc, email) VALUES ('$_POST[title]','$_POST[fname]','$_POST[sname]','$_POST[add1]','$_POST[add2]','$_POST[county]','$_POST[pc]', '$_POST[email]')") or die('Error: ' . mysql_error()); } } which works ok-ish untill i try and redirect the user to another page by using header( "Location: http://www.google.com" ); and then it comes up with the following error, just above the text boxes for input: Warning: Cannot modify header information - headers already sent by (output started at /home/sites/foo/public_html/test/newcustomer.php:12) in /home/sites/foo/public_html/test/php/testvalid.php on line 56 which corresponds directly to the statement i have just added. I cant find a suitable place to put this if statement. Any ideas? Thanks, Lucy Well your thread is too long to go through right now, if you get error then you can use javascript to redirect. So instead of header( "Location: http://www.google.com" ); use echo "<script>document.location.href='http://www.google.com';</script>"; exit(); Quote Link to comment Share on other sites More sharing options...
lucy Posted August 15, 2009 Author Share Posted August 15, 2009 But does IE not automatically turn off javascript and the like, therefore it would be a bit pointless using javascript? Thanks Lucy Quote Link to comment Share on other sites More sharing options...
Karlos2394 Posted August 15, 2009 Share Posted August 15, 2009 Read up on.. ob_start(); ob_flush(); Quote Link to comment Share on other sites More sharing options...
lucy Posted August 16, 2009 Author Share Posted August 16, 2009 Read up on.. ob_start(); ob_flush(); ive had a quick look at them but cant really see how they can be used here? Thanks, Lucy Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 16, 2009 Share Posted August 16, 2009 ob_flush() is not the correct solution although it works. your code should be implemented more properly please see the following thread on how to redesign your application to not produce this error. http://www.phpfreaks.com/forums/index.php/topic,37442.0.html Quote Link to comment 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.