BrotherBear Posted April 8, 2007 Share Posted April 8, 2007 hi all I still have problems with my form what can I do? I want the PHP to check if the space has words or something and if doesn't I want the PHP script to tell people they need to fill every space I have 2 pages that do this work of sending the info to the database the first one is contactus.html wich has just the form here's the code <form action='processing.php' method='post'> <div class='name'>*Name:</div> <div class='namebox'><input type='text' name='Name' style='font-family:monotype corsiva; font-size:15px;' /><br><?echo "$error" ?></div> <div class='age'>*Age:</div> <div class='agebox'><input type='text' name='Age' style='font-family:monotype corsiva; font-size:15px;' /><br></div> <div class='email'>*Email:</div> <div class='emailbox'><input type='text' name='Email' style='font-family:monotype corsiva; font-size:15px;' /><br></div> <div class='commentss'>*Comments:</div> <div class='commentssbox'><textarea name='Comments'>Write Your Message</textarea></div> <div class='submit'><input type='submit' value='Send!' /></div> </form> and the next code is the one that prcess the data and inserts it in the database <?php $con = mysql_connect("mysql1.100ws.com","johncoda_downloa","******"); if (!$con) { die('Could not connect: ' . mysql_error()); } $Name = $_POST['Name']; $Age = $_POST['Age']; $Email = $_POST['Email']; $Comments = $_POST['Comments']; mysql_select_db("johncoda_downloa", $con); $sql = mysql_query("INSERT INTO Customers (Name, Age, Email, Comments) VALUES('$Name', '$Age', '$Email', '$Comments')") or die (mysql_error()); ?> <html> <head> <meta http-equiv="Refresh" content="5;url=http://johncoda.freestarthost.com/thankyou.html"> </head> <body> </body> </html> if you need to see the error by yourself and the form problem go here http://bbanddisney.freeprohost.com/contactus.html any advices to make a space checking with PHP? BB Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted April 8, 2007 Share Posted April 8, 2007 um, from memory, look at the strip_char() function, althpough thats prolly nt its name. thers a function that finds special characters ie "<>?,./;':"[]\{}| etc and the whitecpase, the replaces then with their something or rather name ie which is the whitespace from memory goo dluck i hope thats what you were looking for Quote Link to comment Share on other sites More sharing options...
BrotherBear Posted April 8, 2007 Author Share Posted April 8, 2007 sorry but I didn't get quite right what you said I want like if (name has words) continue with script if (name doesn't have anything) error please fill spaces go to form again that way the php checks all spaces to make sure they have something but they let you send the info when everything is ok BB Quote Link to comment Share on other sites More sharing options...
curtis_b Posted April 8, 2007 Share Posted April 8, 2007 The quick and easy way, with no email validation: if(!($Name && $Age && $Email && $Comments)){ //form is not complete echo "You did not complete the form"; exit; } Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted April 8, 2007 Share Posted April 8, 2007 $nbsp; is a non-breaking space, anyways if(empty($name)) { echo "Name is not set, please fill in all required fields"; } Quote Link to comment Share on other sites More sharing options...
esukf Posted April 8, 2007 Share Posted April 8, 2007 <?php $error = array(); //holds any errors from the form //check name is entered if(!isset($_POST['Name']) || strlen(trim($_POST['Name']))==0){ $error[] = "Please enter a you name"; } //check age is entered if(!isset($_POST['Age']) || strlen(trim($_POST['Age'])==0){ $error[] = "Please enter your age"; } //check email is entered if(!isset($_POST['Email']) || strlen(trim($_POST['Email'])==0){ $error[] = "Please enter you email address"; } else { //check email is valid $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*" ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*" ."\.([a-z]{2,}){1}$"; if(!eregi($regex, $_POST['Email'])){ $error[] = "Please enter a valid email"; } } //check comment is entered if(!isset($_POST['Comments']) || strlen(trim($_POST['Comments'])==0){ $error[] = "please enter a comment"; } //display the errors found if(count($error) > 0){ echo "Errors(s) found" echo "<ul>" foreach($error as $error_item){ echo '<li>'.$error_item.'</li>'; } echo '</ul>'; } else { //do your db stuff $con = mysql_connect("mysql1.100ws.com","johncoda_downloa","******"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("johncoda_downloa", $con); $sql = sprintf("INSERT INTO Customers (Name, Age, Email, Comments) VALUES ('%s', '%s', '%s', '%s')", mysql_real_escape_string($_POST['Name']), mysql_real_escape_string($_POST['Age']), mysql_real_escape_string($_POST['Email']), mysql_real_escape_string($_POST['Comments']) ); $result = mysql_query($sql) or die(mysql_error(); if($result){ header("Location: http://johncoda.freestarthost.com/thankyou.html"); } ?> Quote Link to comment Share on other sites More sharing options...
BrotherBear Posted April 8, 2007 Author Share Posted April 8, 2007 thanks everyone this helps BB 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.