Jump to content

[SOLVED] form submitting information even though check is in place


dennismonsewicz

Recommended Posts

<?php require_once "includes/header.php"; ?> 

<?php if(isset($_POST['submit'])) {
	if(!isset($_POST['name']) || $_POST['name'] == '') {
		$e_name = '<span class="error">You must enter a name to continue</span>';
	} else {
		$name = mysql_real_escape_string($_POST['name']);
	}

	if($_POST['email']) {
		$email = mysql_real_escape_string($_POST['email']);
	}

	if(!isset($_POST['message']) || $_POST['message'] == '') {
		$e_message = '<span class="error">You must enter a message to continue</span>';
	} else {
		$message = mysql_real_escape_string($_POST['message']);
	}		

	$query = mysql_query("INSERT INTO contact (name, email, message) VALUES ('$name', '$email', '$message')")or die(mysql_error());

	}
?>
    		
    		<div class="container">
    			
    			<div class="left_weight_two">
    		
				<?php include "includes/nav.php"; ?>
    			
    			<div class="content-left">

				<?php if($query) {
							$to = 'dennismonsewicz@gmail.com';
							$subject = 'Contact from dennismonsewicz.com';
							$body = '<p>Dennis, <strong>' . $name . '</strong> has filled out your contact form and has sent you the following information.';
							$body .= '<p>Email: ' . $email . '</p>';
							$body .= '<p>Message: ' . $message . '</p>';
							$headers  = 'MIME-Version: 1.0' . "\r\n";
							$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
							if(mail($to, $subject, $body, $headers)) {
								echo '<div class="alert"><img src="images/action_check.gif" alt="image" class="alertimg"/> Contact Successful! An email has been sent to Dennis Monsewicz</div>';
							}
						}
				?>
                    
                    <form action="contact.php" method="post">

					<p>

						Name: <b style="color:#ff0000;">*</b> <?php echo $e_name; ?><br/>

						<input type="text" name="name" id="name" class="forms" style="width:250px;"/>

					</p>

					<p>

						Email <em>(optional)</em>:<br/>

						<input type="text" name="email" id="email" class="forms" style="width:250px;"/>

					</p>

					<p>

						Message: <b style="color:#ff0000;">*</b> <?php echo $e_message; ?><br/>

						<textarea rows="6" cols="50" class="forms" style="width:440px;" name="message" id="message"></textarea>

					</p>

						<input type="submit" id="contact" class="buttons" value="Submit" name="submit" />

				</form>

					<div style="clear:both;"></div>
						    			
    			</div>
    			
    		</div>
    		
    		<?php include "includes/right.php"; ?>
    		
    		<div style="clear:both;"></div>
    			
    		</div>
    		
<?php require_once "includes/footer.php"; ?>

 

If you submit the form blank the form still submits and shows the contact successful message... how do i fix this so that the form does not submit if a blank form is submitted

Link to comment
Share on other sites

I'm not incorporating anything into that code because to be quite frank, its pretty hard to follow. You should use spaces to indent, tabs or whatever it is your using create far too much whitespace.

 

Anyway, most of the logic seems to be already there, your just not stopping execution If you have error messages. I would use an array to store your error messages then stop the script form executing if the array has values. eg;

 

$errors = array();
if (!isset($_POST['foo'] || $_POST['foo'] == '') {
  $errors[] = "foo is empty";
} else {
  $foo = $_POST['foo'];
}

// more checking.

// process.
if (count($errors)) {
  // display errors.
  foreach ($errors as $error) {
    echo $error;
  }
  exit();
} else {
  // process form.
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.