karl_009 Posted February 28, 2009 Share Posted February 28, 2009 Hello again, I have been working on a form, the form is finished that it enters data into the database, but am looking to include some basic validation. So in nothing is entered a message is shown on screen saying that it was missed off, I was able to put a very basic validation on but no message informing the user. Am hoping you are able to see a easy solution or could point me in the right direction. Here is my PHP code; <?php require ($_SERVER['DOCUMENT_ROOT']."cmstesting/config/db_config.php"); $connection = @mysql_connect ($db_host, $db_user, $db_pass) or die ("Error with Connection"); mysql_select_db ($db_name, $connection); $first1 = $_POST["first"]; $len = strlen($first1); if ($len > 0) { $title1 = $_POST["title"]; $last1 = $_POST["last"]; $suffix1 = $_POST["suffix"]; $company1 = $_POST["company"]; $address11 = $_POST["address1"]; $address21 = $_POST["address2"]; $city1 = $_POST["city"]; $town1 = $_POST["town"]; $country1 = $_POST["country"]; $postcode1 = $_POST["postcode"]; $website1 = $_POST["website"]; $email1 = $_POST["email"]; $hp11 = $_POST["hp1"]; $hp21 = $_POST["hp2"]; $hp31 = $_POST["hp3"]; $mp11 = $_POST["mp1"]; $mp21 = $_POST["mp2"]; $mp31 = $_POST["mp3"]; $notes1 = $_POST["notes"]; $query = "INSERT INTO contacts (conid, title, first, last, suffix, company, address1, address2, city, town, country, postcode, website, email, hp1, hp2, hp3, mp1, mp2, mp3, notes) VALUES (NULL, '$title1', '$first1', '$last1', '$suffix1', '$company1', '$address11', '$address21', '$city1', '$town1', '$country1', '$postcode1', '$website1', '$email1', '$hp11', '$hp21', '$hp31', '$mp11', '$mp21', '$mp31', '$notes1')"; mysql_query($query, $connection) or die("MySQL Query Error"); header ('Location: contact_view.php'); exit; } ?> Here is my Form, this is only part of it; <html> <body> <form id="contact" name="contact" class="" autocomplete="off" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <div class="info"> <h2>New Contact</h2> <div></div> </div> <ul> <li id="foli0" class=" "> <label class="desc" id="title0" for="Field0"> Name </label> <span> <input id="title" name="title" type="text" class="field text" value="" size="2" tabindex="1" /> <label for="Field0">Title</label> </span> <span> <input id="first" name="first" type="text" class="field text" value="" size="8" tabindex="2" /> <label for="Field1">First</label> </span> <span> <input id="last" name="last" type="text" class="field text" value="" size="12" tabindex="3" /> <label for="Field2">Last</label> </span> <li class="buttons"> <input id="saveForm" class="btTxt" type="submit" value="Submit" /> </li> </body> </html> I have tryed to do something simple like this, but before I even submit the page the error is displayed; <?php if (! strlen($_POST['flavor'])) { print 'You must enter your favorite ice cream flavor.'; } ?> Thanks for any help Karl Quote Link to comment https://forums.phpfreaks.com/topic/147355-solved-form-validation-display-error-message/ Share on other sites More sharing options...
dt192 Posted February 28, 2009 Share Posted February 28, 2009 $errorMessage=''; if (!isset($_POST['title1'])){$errorMessage.='<br/>Title Missing';} if (!isset($_POST['last1'])){$errorMessage.='<br/>Last Name Missing';} if($errorMessage!=''){echo 'Please rectify the following error(s):'.$errorMessage;} you also need to look into sanitising the data before you put it into the database Quote Link to comment https://forums.phpfreaks.com/topic/147355-solved-form-validation-display-error-message/#findComment-773499 Share on other sites More sharing options...
karl_009 Posted February 28, 2009 Author Share Posted February 28, 2009 Thank you for the response. I tried the code, what happens is because of the; header ('Location: contact_view.php'); exit; The code is redirected straight away, and if i take out the header then the form is not displayed, but if I take out the exit the form is displayed but all the error messages are also displayed, this is with the code above. All the PHP and HTML code in on the same page. Thanks Karl Quote Link to comment https://forums.phpfreaks.com/topic/147355-solved-form-validation-display-error-message/#findComment-773512 Share on other sites More sharing options...
dt192 Posted March 1, 2009 Share Posted March 1, 2009 Hi i would just do it all a bit diff, i have not error checked this but you get my drift, but you would want to do more sanitisation before outputting to value part of the form and possibly before entering into the database, your best bet is probably to get a drop in script from one of the many script sites if your not too sure <?php $errorMessage=''; if(isset($_POST['submitted'])) { $errorMessage=''; if (!isset($_POST['title'])){$errorMessage.='<br/>Title Missing';} if (!isset($_POST['first'])){$errorMessage.='<br/>First Name Missing';} if (!isset($_POST['last'])){$errorMessage.='<br/>Last Name Missing';} if($errorMessage=='') { require ($_SERVER['DOCUMENT_ROOT']."cmstesting/config/db_config.php"); $connection = @mysql_connect ($db_host, $db_user, $db_pass) or die ("Error with Connection"); mysql_select_db ($db_name, $connection); $query = "INSERT INTO contacts (conid, title, first, last) VALUES (NULL, '".mysql_real_escape_string($_POST['title'])."', '".mysql_real_escape_string($_POST['first'])."', '".mysql_real_escape_string($_POST['last'])."')"; mysql_query($query, $connection) or die("MySQL Query Error"); } } else { echo $errorMessage; ?> <form id="contact" name="contact" class="" autocomplete="off" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <div class="info"> <h2>New Contact</h2> <div></div> </div> <ul> <li id="foli0" class=" "> <label class="desc" id="title0" for="Field0"> Name </label> <span> <input id="title" name="title" type="text" class="field text" value="<?php if(isset($_GET['title'])){echo $_GET['title'];} ?>" size="2" tabindex="1" /> <label for="Field0">Title</label> </span> <span> <input id="first" name="first" type="text" class="field text" value="<?php if(isset($_GET['first'])){echo $_GET['first'];} ?>" size="8" tabindex="2" /> <label for="Field1">First</label> </span> <span> <input id="last" name="last" type="text" class="field text" value="<?php if(isset($_GET['last'])){echo $_GET['last'];} ?>" size="12" tabindex="3" /> <label for="Field2">Last</label> </span> <li class="buttons"> <input id="saveForm" class="btTxt" type="submit" value="Submit" name="submitted"/> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/147355-solved-form-validation-display-error-message/#findComment-773537 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.