Jump to content

Form submits to data base and i am trying to stop it when there is an error


benjaminmorgan

Recommended Posts

I have a form and whenever I am entering a invalid email it still goes through.

 

I have the form set where if it isn't valid it will pop up an error... BUT when I put in the php section it is this.

 

if (isset($_REQUEST['Submit'])) {

$FName=$_POST['fullname'];

$CName=$_POST['comname'];

$Cwebsite=$_POST['cwebsite'];

$Email=$_POST['blemail'];

$comabout=$_POST['comabout'];

$prequirements=$_POST['prequirements'];

$pagenumber=$_POST['pagenum'];

 

 

if(

$_POST['fullname']!="" ||

$_POST['blemail']!="" ||

$_POST['comabout']!="" ||

$_POST['prequirements']!="" ||

$_POST['pagenum']!="" ||

$_POST['blemail']==preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['blemail']))

{

 

$query="INSERT INTO quote (full_name, company_name, website, email, company_about, project_requirements, page_numbers)VALUES('".$FName."', '".$CName."', '".$Cwebsite."', '".$Email."', '".$comabout."', '".$prequirements."', '".$pagenumber."')";

mysql_query($query) or die("Error updating database.". mysql_error());

 

}

}

 

**************************************************************************

table name is quote and it is inserting fine. I just want to stop it if it does not meet any of the following. I can just submit the form blank (except for the pages because it is a dropdown option with one preselected. ) I want to stop the form if a required field is blank OR the email doesn't match the reg ex

**************************************************************************

Link to comment
Share on other sites

The way you have it, the code checks if a field is NOT empty, try reversing it so it checks if any field IS empty:-

// Code to check for empty input boxes (edit to suit your form)
if (empty($email) || empty($name) || empty($subject) || empty($component) || empty($size)) {
echo "Please complete all boxes";
}
else .........

 

Separate the email check into a separate if statement.

 

It's often best to download a form script that has many security features.

Edited by Wickham
Link to comment
Share on other sites

http://pastebin.com/SpDarS8J is my exact code. I don't see any non closed if statements. there is an if within an if though.

Can you point out which two lines have the closing "}" for the two if statements?

 

You need

 

if ($_POST) {
   ... your code...

   if(
       $FName!="" &&
       $Email!="" &&
       $comabout!="" &&
       $prequirements!="" &&
       $Email==preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $Email)
   ) {

   ... more code

   } // closing bracket
} // closing bracket

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...