Big_R Posted April 9, 2011 Share Posted April 9, 2011 Okay with some help I was able to create a working guestbook! How ever I need a bit of help with validation. I would like to have it so that when someone doesn't fill out a required field a message is displayed next to the improperly filled out field. How would I go about adding this to my form or process code? So for example: if someone doesn't fill out the "name" field and clicks "sign" it will not submit but instead will display a message such as "Please fill out your name" next to the form field. Also will I am here I wanted to ask. How can I make it so that when a non-required field is left empty and posted it will fill that section out with default text? So for example: if someone decides they do not wish to fill out the "Favorite part of my site" or "Favorite Pat Song" field than submits the form than it will fill that section with a default message like this: Posted by: someone ([email protected]) 2011/04/09 Favorite Pat Song: -- Favorite Part of the Site: -- comments here And I promise this is my very last question. I would like for email to be required but would like to allow the user to decide whether it is kept private or not. How do I make it so that when someone doesn't type their email in it does like what I stated above, it will show a message next to the field telling them to fill out a valid email but also if they select to keep their email private it will than post a default value in place of where the email would have been. for example: if the email field is left empty or is an invalid email it will display a message such as "Please fill out a valid email address" next to the form field. and than if they do so but also check the "private" box it will post their message in the guestbook with a default value in place of the email like this: Posted by: someone (Private) 2011/04/09 Favorite Pat Song: -- Favorite Part of the Site: -- comments here so that was a lot here's my guestbook code: <?php <span style='color:#ff0000'><b>*</b></span><span> = required field</span><br /><br /> <form name='guestbook' action='process.php' method='post'> <table width='550' border='0' cellspacing='2' cellpadding='0'> <tr valign='top'> <td width='550px' class='what'><span style='color:#ff0000'><b>*</b></span> Your Name:</td> <td width='550px'><input name='name' type='text' id='name' size='32' /></td> </tr> <tr valign='top'> <td width='550px' class='what'><span style='color:#ff0000'><b>*</b></span> Email Address:</td> <td width='550px'><input name='email' type='text' id='email' size='32' /><input type='checkbox' name='private' value='Private' />Private</td> </tr> <tr valign='top'> <td width='550px' class='what'>Your Favorite Pat Song?:</td> <td width='550px'><input name='song' type='text' id='song' size='32' /></td> </tr> <tr valign='top'> <td width='550px' class='what'>Your Favorite Part of my Site?:</td> <td width='550px'><input name='part' type='text' id='part' size='32' /></td> </tr> <tr valign='top'> <td width='550px' class='what'><span style='color:#ff0000'><b>*</b></span> Comment:</td> <td width='550px'><textarea name='comments' cols='28' rows='6' id='comments' class='bodytext'></textarea></td> </tr> <tr> <td class='bodytext'> </td> <td align='left' valign='top'><input name='submit' type='submit' class='btn' value='Sign' /></td> </tr> </table> </form>"; }else{ $connect = mysql_connect("127.0.0.1","patben_admin","pepsi_1990") or die('Error connecting'); $db = mysql_select_db("patben_db") or die('Error selecting db'); $query = mysql_query("SELECT * FROM guestbook order by id desc"); $num_rows = mysql_num_rows($query); if($num_rows > 0) { //display entries while($row = mysql_fetch_array($query)){ echo ' <table> <tr> <td> <b>Posted by:</b> '.$row['name'].' ('.$row['email'].')<br /> <b>'.$row['date'].'</b><br /> <b>Favorite Pat Song:</b> '.$row['song'].'<br /> <b>Favorite Part of the Site:</b> '.$row['part'].' </td> </tr> <tr> <td> '.nl2br($row['comments']).' <hr /> </td> </tr> </table>'; } }else{ echo "No Entries... <a href='guestbook.php?page=sign'>Be the first!</a>"; } } ?> and here's the code that processes the form (separate file): <?php if($_POST['submit']) { $connect = mysql_connect('127.0.0.1','patben_admin','pepsi_1990') or die('Error connecting'); $db = mysql_select_db('patben_db') or die('Error selecting db'); $date = date("Y-m-d"); $name = strip_tags($_POST['name']); $email = strip_tags($_POST['email']); $song = strip_tags($_POST['song']); $part = strip_tags($_POST['part']); $comments = nl2br($_POST['comments']); $query = mysql_query("insert into guestbook values('','$date','$name','$email','$song','$part','$comments')"); header("location: guestbook.php"); }else{ header("location: guestbook.php"); } ?> I have not added a section in my database for the check box so I am also unsure how to do that (if I need to). Any help would be greatly appreciated! Thank you Quote Link to comment https://forums.phpfreaks.com/topic/233205-form-validation/ Share on other sites More sharing options...
Jnerocorp Posted April 9, 2011 Share Posted April 9, 2011 i think for pre-form processing you would have to use JavaScript Quote Link to comment https://forums.phpfreaks.com/topic/233205-form-validation/#findComment-1199309 Share on other sites More sharing options...
Pikachu2000 Posted April 9, 2011 Share Posted April 9, 2011 Javascript is not validation. Even if JS is used, the data must be validated server-side. Quote Link to comment https://forums.phpfreaks.com/topic/233205-form-validation/#findComment-1199310 Share on other sites More sharing options...
perky416 Posted April 9, 2011 Share Posted April 9, 2011 Hi Big_R, I use the following for my form validation: Put this somewhere near the top of your page: $error = array(); Where you have your form field use: <td width='550px'><input name='name' type='text' id='name' size='32' /><?php if (empty(!$_POST['field_name'])) { $error[] = "Please enter your name"; } ?></td> Then where you write the input to your database use: if (empty($error)) { mysql_query("insert into guestbook values('','$date','$name','$email','$song','$part','$comments')"); } else { echo implode($error); This is basically saying if you dont have an error, write to the database, otherwise display the error message. For writing a default message you could use something like: if (empty($_POST['part'])) { $favourite_part = "--"; } else { $favourite_part = $_POST['part']; } This code is untested but it should work. Hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/233205-form-validation/#findComment-1199318 Share on other sites More sharing options...
perky416 Posted April 9, 2011 Share Posted April 9, 2011 Sorry i missed off them email part. To validate the email use: <td width='550px'><input name='email' type='text' id='email' size='32' />if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {$error[] = "The email address you have entered is invalid!";}</td> Id create another field in the database table called "private". Then you could use: if (isset($_POST['private'])){ $private = "yes"; } else { $private = "no"; } Then in your mysql query that you use to write your values to the database, add: private='$private' Finally on the page where you display the guest book data, you could use: if ($private == "yes"){ echo "Private"; } else { echo "$email"; } Again this is untested, but it should work. It may not be the most efficient method but im a little short on time to play around with it. Quote Link to comment https://forums.phpfreaks.com/topic/233205-form-validation/#findComment-1199323 Share on other sites More sharing options...
Big_R Posted April 9, 2011 Author Share Posted April 9, 2011 thank you for all your help, I look forward to trying this. I just have 2 quick questions 1. What data type would the field "private" need to be in my data base? 2. Since my form is already in <?php ?> tags and just echoed out than do I have to put the <?php ?> tags around the bits you add for the error messages like this: <td width='550px'><input name='name' type='text' id='name' size='32' /><?php if (empty(!$_POST['field_name'])) { $error[] = "Please enter your name"; } ?></td> or do I leave those tags out since the entire form is already wrapped in <?php ?> tags? thank you again. Quote Link to comment https://forums.phpfreaks.com/topic/233205-form-validation/#findComment-1199424 Share on other sites More sharing options...
Big_R Posted April 11, 2011 Author Share Posted April 11, 2011 okay I added the validation script suggested and I have no idea why I am getting this error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING on line 62 It's probably something I did stupid, what am I doing wrong? I added the "private" field to my database, its a tinyint data type. Is that the write data type? Thank you for all the help and thank you in advance. my edited guestbook script: <?php $error = array(); if(isset($_GET['page'])) { echo " <span style='color:#ff0000'><b>*</b></span><span> = required field</span><br /><br /> <form name='guestbook' action='process.php' method='post'> <table width='550' border='0' cellspacing='2' cellpadding='0'> <tr valign='top'> <td width='550px' class='what'><span style='color:#ff0000'><b>*</b></span> Your Name:</td> <td width='550px'><input name='name' type='text' id='name' size='32' /><?php if (empty(!$_POST['name'])) { $error[] = "Please enter your name"; } ?></td> </tr> <tr valign='top'> <td width='550px' class='what'><span style='color:#ff0000'><b>*</b></span> Email Address</td> <td width='550px'><input name='email' type='text' id='email' size='32' />if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {$error[] = "The email address you have entered is invalid!";} <input type='checkbox' name='private' value='private' />Keep Private</td> </tr> <tr valign='top'> <td width='550px' class='what'>Your Favorite Pat Song?:</td> <td width='550px'><input name='song' type='text' id='song' size='32' /></td> </tr> <tr valign='top'> <td width='550px' class='what'>Your Favorite Part of my Site?:</td> <td width='550px'><input name='part' type='text' id='part' size='32' /></td> </tr> <tr valign='top'> <td width='550px' class='what'><span style='color:#ff0000'><b>*</b></span> Comment:</td> <td width='550px'><textarea name='comments' cols='28' rows='6' id='comments' class='bodytext'><?php if (empty(!$_POST['comments'])) { $error[] = "Please enter a comment"; } ?></textarea></td> </tr> <tr> <td class='bodytext'> </td> <td align='left' valign='top'><input name='submit' type='submit' class='btn' value='Sign' /></td> </tr> </table> </form>"; }else{ $connect = mysql_connect("127.0.0.1","patben_admin","pepsi_1990") or die('Error connecting'); $db = mysql_select_db("patben_db") or die('Error selecting db'); $query = mysql_query("SELECT * FROM guestbook order by id desc"); $num_rows = mysql_num_rows($query); if($num_rows > 0) { //display entries while($row = mysql_fetch_array($query)){ echo ' <table> <tr> <td> <b>Posted by:</b> '.$row['name'].' if ($private == "yes"){ echo "Private"; } else { echo "$email"; }<br /> <b>'.$row['date'].'</b><br /> <b>Favorite Pat Song:</b> if (empty($_POST['song'])) { $song = "--"; } else { $song = $_POST['song']; } <b>Favorite Part of the Site:</b> if (empty($_POST['part'])) { $part = "--"; } else { $part = $_POST['part]; } </td> </tr> <tr> <td> '.nl2br($row['comments']).' <hr /> </td> </tr> </table>'; } }else{ echo "No Entries... <a href='guestbook.php?page=sign'>Be the first!</a>"; } } ?> and my edited data processing script to put form input into the database (separate file): <?php if($_POST['submit']) { $connect = mysql_connect('127.0.0.1','patben_admin','pepsi_1990') or die('Error connecting'); $db = mysql_select_db('patben_db') or die('Error selecting db'); $date = date("Y-m-d"); $name = strip_tags($_POST['name']); $email = strip_tags($_POST['email']); $song = strip_tags($_POST['song']); $part = strip_tags($_POST['part']); private='$private' $comments = nl2br($_POST['comments']); if (empty($error)) { mysql_query("insert into guestbook values('','$date','$name','$email','$song','$part','$comments')"); header("location: guestbook.php");} if (isset($_POST['private'])){ $private = "yes"; } else { $private = "no"; } else { echo implode($error); }else{ header("location: guestbook.php"); } ?> any help will be greatly appriciated! Quote Link to comment https://forums.phpfreaks.com/topic/233205-form-validation/#findComment-1200094 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.