ironman32 Posted March 30, 2009 Share Posted March 30, 2009 I have been trying to validate the data in this form so that It gives an error message if the field are empty. Here is the form <html> <body> <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <form name="form1" method="post" action="insert.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td colspan="3"><strong>Register Details </strong></td> </tr> <tr> <td width="78">Username</td> <td width="6">:</td> <td width="294"><input name="username" type="text" id="username"></td> </tr> <tr> <td>Password</td> <td>:</td> <td><input name="password" type="password" id="password"></td> </tr> <tr> <td>Email</td> <td>:</td> <td><input name="email " type="text" id="email"></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Register"></td> </tr> </table> </td> </form> </tr> </table> </html> </body> And here is my insrt.php page which processe the form data <?php session_start(); $_SESSION['username'] = $_POST['username']; ?> <?php $con = mysql_connect("lo","db","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dontwork_workout", $con); // variables are run through mysql_real_escape_string() to avoid sql injection $user = mysql_real_escape_string($_POST['username']); $email = mysql_real_escape_string($_POST['email']); $pass = mysql_real_escape_string($_POST['password']); $sql="INSERT INTO user (username, email, password) VALUES ('$_POST[username]','$_POST[email]','$_POST[password]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con) ?> I would like to out put an error message if the fields are empty and a link to return to the form with the message. Any guidance/help would be appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/151830-form-validation/ Share on other sites More sharing options...
corbin Posted March 30, 2009 Share Posted March 30, 2009 <td><input name="email " type="text" id="email"></td> Shouldn't that be: <td><input name="email" type="text" id="email"></td>? Anyway: To check if a variable is empty is very easy. if(empty($var)) { //do something. } Quote Link to comment https://forums.phpfreaks.com/topic/151830-form-validation/#findComment-797258 Share on other sites More sharing options...
lonewolf217 Posted March 30, 2009 Share Posted March 30, 2009 <?php if(!empty($_POST['username']) || !empty($_POST['password']) || !empty($_POST['email'])) { echo "there are empty fields in your form"; echo "<a href=\"form.php\">Go Back</a> to the form"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151830-form-validation/#findComment-797261 Share on other sites More sharing options...
corbin Posted March 30, 2009 Share Posted March 30, 2009 if(!empty($_POST['username']) || !empty($_POST['password']) || !empty($_POST['email'])) Errr.... I think you mean to leave out all of those !. Quote Link to comment https://forums.phpfreaks.com/topic/151830-form-validation/#findComment-797267 Share on other sites More sharing options...
lonewolf217 Posted March 30, 2009 Share Posted March 30, 2009 yea .. i did Quote Link to comment https://forums.phpfreaks.com/topic/151830-form-validation/#findComment-797272 Share on other sites More sharing options...
cberube09 Posted March 30, 2009 Share Posted March 30, 2009 First of all, you want to be using $user, $email, and $pass in your insert statement. Secondly, here is how I would write the code. I would say... $user = $_POST['username']; $email = $_POST['email']; $pass = $_POST['password']; if((empty($user))) { error1 = array('username'=>'<b>Error:</b> The username field is empty!<br>'); } if((empty($email))) { error2 = array('email'=>'<b>Error:</b> The email field is empty!<br>'); } if((empty($pass))) { error3 = array('password'=>'<b>Error:</b> The password field is empty!<br>'); } if(!isset($error1) && !isset($error2) && !isset($error3)) { $sql="INSERT INTO user (`username`, `email`, `password`) VALUES ('%s', '%s', '%s')", mysql_real_escape_string($user, $con), mysql_real_escape_string($email, $con), mysql_real_escape_string($pass, $con)); $result=mysql_query($query) or die('Sorry, could not query the database'." Error: ".mysql_error()); if ($result) { echo "<div>Successful submit</div>"; }else { echo "<div>Unsuccessful submit</div>"; } } else { echo'<div>'; if(isset($error1['username'])){echo $error1['username'];} if(isset($error2['email'])){echo $error2['email'];} if(isset($error3['password'])){echo $error2['password'];} } Quote Link to comment https://forums.phpfreaks.com/topic/151830-form-validation/#findComment-797274 Share on other sites More sharing options...
corbin Posted March 30, 2009 Share Posted March 30, 2009 Why use an array in your code if you're not going to use the keys for something useful? (For example, the keys could be used to make certain input elements have red borders or something indicative of an error.) Also, why not just do: $errors = array(); if(something) { $errors[] = "Some error message."; } if(count($errors) != 0) { //errors! print them out in a foreach() loop. } else { //wooo! } Quote Link to comment https://forums.phpfreaks.com/topic/151830-form-validation/#findComment-797279 Share on other sites More sharing options...
cberube09 Posted March 30, 2009 Share Posted March 30, 2009 Well my arrays do something useful, its just that I don't know what css elements he is using. And I am a bit overtired but I believe that what you wrote doesn't work for some reason or another...not quite sure about that though. Quote Link to comment https://forums.phpfreaks.com/topic/151830-form-validation/#findComment-797304 Share on other sites More sharing options...
corbin Posted March 30, 2009 Share Posted March 30, 2009 if(something) { Is of course made up, but besides that, why would it not work? Also with my way, you could still use multi dimensional arrays. That way, you don't have to check a ton of variables; you can do one simple count(). Quote Link to comment https://forums.phpfreaks.com/topic/151830-form-validation/#findComment-797308 Share on other sites More sharing options...
cberube09 Posted March 30, 2009 Share Posted March 30, 2009 Hmm maybe I shouldn't be getting myself into this right now but if there is a bunch of different checks that you want to run and you use if(something) { $errors[] = "Some error message."; } then wouldn't that only check one condition? You don't want multiple messages for one condition, but different error messages for different conditions, so you would use multiple arrays. Am I thinking about this correctly? Quote Link to comment https://forums.phpfreaks.com/topic/151830-form-validation/#findComment-797320 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.