Jump to content

in contact form the empty fields (for example name) is required is not functioning ?


johannes999

Recommended Posts

Hello,

I have this php code for contact form which I  had copied   and sudied from w3school  then I have   incorporated  in my website.

everything was working very wel .

but now after a while  I see some error .

  when I try to fill the contact form fields with name email etc. and I let 1 field expressly empty and click on submit botton the  email is sent.

and  I don't get the error   for example name is requierd, email is required , etc.

what it can be the problem?

thanks

johannes

<?php

/*
* Template Name: Contact
* Description: Template voor contactpagina
* Template Post Type: page
*/
get_header('new4');
the_content();
// define variables and set to empty values
$nameErr = $emailErr = $phoneErr = $contentErr = "";
$name_sender = $email_sender= $phone = $content =$success= "";

  if ($_SERVER["REQUEST_METHOD"] == "POST") {

	
  

	// post
	$name_sender = strip_tags($_POST['name_sender']);
	$email_sender = strip_tags($_POST['email_sender']);
	$phone = strip_tags($_POST['phone']);
	$content = strip_tags($_POST['content']);
		
	// validate name
	 if (empty($_POST["name_sender"])) {
    $nameErr = "Name is required";
  } else {
    $name_sender = test_input($_POST["name_sender"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z-' ]*$/",$name_sender)) {
      $nameErr = "Only letters and white space allowed";
    }
	 }
	
	
 
	
	
	if (empty($_POST["email_sender"])) {
    $emailErr = "Email is required";
  } else {
    $email_sender = test_input($_POST["email_sender"]);
    // check if e-mail address is well-formed
    if (!filter_var($email_sender, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format";
    }
  }

	
	

	// validate phone
	if (empty($_POST["phone"])) {
		$phoneErr = "Phone is required";
	} else {
		 $phone = test_input($_POST["phone"]);
		// check if phone is well-formed
		if(!preg_match("/^[0-9\s\-]+$/", $phone)) {
			$phoneErr = "Invalid phone number";
		}
	}


	// validate content
	if (empty($_POST["content"] )) {
		$contentErr = "Content is required";
	} else {
    $content = test_input($_POST["content"]);
  }

	// send
		if ( empty($nameErr) && empty($emailErr) && empty($phoneErr) && empty($contentErr) ) {
		// Variables
		$to = 'johannes@webdesignleren.com';
		$subject = 'Contact form';
			
			echo '<script type="text/javascript">window.location.href="https://webdesignleren.com/bedankt/"</script>';

			
    



		// Message content		
		 $content .= ''; 	

		// Headers
		$headers	 = 'From: ' .$name_sender. ' <' .$email_sender. '>' . PHP_EOL;
		$headers	.= 'Reply-To: <' .$email_sender. '>' . PHP_EOL;
		$headers	.= 'Content-Transfer-Encoding: 8bit' . PHP_EOL;
		$headers	.= 'Content-Type: text/plain; charset=UTF-8' . PHP_EOL;

		// Send email
		mail($to, $subject,$content, $headers); 
		
		
		
    
	
}
	  
	 

	  
  }




function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
	


// form


?>
<div class="container-form">
<form id="contact" action= "https://webdesignleren.com/bedankt/"  autocomplete="off"  method="post" action=""> 
<h3>Quick Contact</h3>
<h4>Contact us today, and get reply with in 24 hours!</h4>
<fieldset>
<input placeholder="Your name" type="text" name="name_sender" id="name_sender" value="<?php echo $name_sender;?>">

<span class="error">* <?php echo $nameErr; ?></span>
</fieldset>
<fieldset>
<input placeholder="Your Email Address" type="text" name="email_sender" id="email_sender" value="<?php echo $email_sender;?>">
 
<span class="error">* <?php echo $emailErr; ?></span>
</fieldset>
<fieldset>
<input placeholder="Your phone number" type="text" name="phone" id="phone" value="<?php echo $phone;?>">

<span class="error">* <?php echo $phoneErr; ?></span>
</fieldset>
<fieldset>
<textarea placeholder="Type your message here.." type="text" name="content"  id="content"><?php echo $content;?></textarea>
<span class="error">* <?php echo $contentErr; ?></span>
</fieldset>
<fieldset>

	<button name="submit" type="submit" id="btnsubmit" class="submit">Submit</button></fieldset>
	
</form></div>






								   
								   

								 
								 














		
	

https://webdesignleren.com/contact/

Link to comment
Share on other sites

I prefer to set a varable $IsValid value as true, if a field is empty or not valid, produce error message and set $IsValid to false. If $IsValid is still true at end of validation, set the: to email, subject, content and header values for mail(), this way you only check against a single boolean variable during validation.

Edited by dsonesuk
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...