-1

I'm beginner in php. I've designed below code to run php form.

if i click on submit button. it does not dispaly message of required field. please tell me where is error in code writing.

Thanks

<?php include('header.php'); ?>
<?php
if (isset($_POST['submit'])) {  
if (empty($_POST['co_name']) || isset($_POST['co_name'])){
echo 'Field is empty';
exit();
}
}
?>
<h1>Inquery </h1>
<!--<div class="box effect3">-->
<div id="frmsubmit">
<form class="email" name="form1" action="inquery.php" method="post">
<p>Contact Person <span>*</span></p>
<input type="text" name="co_name" />
<p>Company Name <span>*</span></p>
<input type="text" name="cname" />
<p>Address <span>*</span></p>
<textarea name="address"></textarea></p>

<p>Mobile Number <span>*</span></p>
<input type="text" name="mobile" />

<p>E-mail <span>*</span></p>
<input type="text" name="email" />
<p>Phone Number</p>
<input type="text" name="ph" />
<p>Subject <span>*</span></p>
<input type="text" name="subject" />
<p>Message <span>*</span></p>
<textarea name="message"></textarea></p>
<br />
<input class="send" type="submit" value="submit">
</form>
<br /><br /><br /><br /><br />
</div><!--frmsubmit codes-->
<!--</div>--><!--effect2 div-->



<?php include('footer.php'); ?>

Code of inq_mail.php

<?php
if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "myemail@domain.com";
$email_subject = $_POST['subject'];


function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you    
submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}

// validation expected data exists
if(!isset($_POST['name']) ||
    !isset($_POST['cname']) ||  
    !isset($_POST['address']) ||    
    !isset($_POST['subject']) ||
    !isset($_POST['email']) ||
    !isset($_POST['mobile']) ||
    !isset($_POST['ph']) ||
    !isset($_POST['message'])) {
    died('We are sorry, but there appears to be a problem with the form you  
submitted.');      
}

$name = $_POST['name']; // required
$cname = $_POST['cname']; // required
$address = $_POST['address']; // required
$subject = $_POST['subject']; // required
$email_from = $_POST['email']; // required
$mobile = $_POST['mobile']; // required
$ph = $_POST['ph']; // required
$message = $_POST['message']; // not required

$error_message = "";




   $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
 $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
 }


//  $string_exp = "/^[A-Za-z .'-]+$/";
//  if(!preg_match($string_exp,$name)) {
//    $error_message .= 'The Name you entered does not appear to be valid.<br />';
//  }
//   if(!preg_match($string_exp,$cname)) {
//    $error_message .= 'The Name you entered does not appear to be valid.<br />';
//  }
//   if(!preg_match($string_exp,$address)) {
//    $error_message .= 'The Name you entered does not appear to be valid.<br />';
//  }
//   if(!preg_match($string_exp,$mobile)) {
//    $error_message .= 'The Name you entered does not appear to be valid.<br />';
//  }
//   if(!preg_match ('/[^0-9]/', $element) || $mobile=="") {
//    $error_message .= 'Please enter Numeric Value Or Field required.<br />';
//  }
//  if(!preg_match($string_exp,$subject)) {
//    $error_message .= 'Please fill the subject field.<br />';
//  }
 //  if(strlen($message) < 2) {
//    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
//  }
 //  if(strlen($error_message) > 0) {
//    died($error_message);
//  }

if ($name == "")
{
echo "Field is empty";
}
   $email_message = "Form details below.\n\n";

  function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "<b>Name    :</b> ".clean_string($name)."\n";
$email_message .= "<b>Subject :</b> ".clean_string($subject)."\n";
$email_message .= "<b>Email   :</b> ".clean_string($email_from)."\n";
$email_message .= "<b>Email   :</b> ".clean_string($email_from)."\n";
$email_message .= "<b>Message :</b> ".clean_string($message)."\n";



// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
 @mail($email_to, $email_subject, $email_message, $headers); 
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>

3 Answers3

2

change

<form class="email" name="form1" action="inquery.php" method="post">

to

<form class="email" name="form1" action="" method="post">

and

<input class="send" type="submit" name="submit" value="submit">
user7789076
  • 798
  • 2
  • 12
  • 25
0

You can do it with HTML5, adding required attribute to your fields as:

<!DOCTYPE html>
<html>
<body>
<form action="http://google.com">
 <input type="text" name="q" required/>
 <input type="submit" />
</form>
</body>
</html>

enter image description here

Remember that PHP process data in server side, if you need a more complex validation in client side, you should use JavaScript (JQuery).

Ignacio Ocampo
  • 2,693
  • 1
  • 20
  • 31
  • 1
    Validation on the client-side is for it to be user-friendly. You **MUST** validate on the server-side no matter what. – Ali Dec 13 '13 at 08:32
  • thanks Alitrixx... i appriciate what you are asking about... i also want do it same way – user3060235 Dec 13 '13 at 08:47
0

@user3060235 - A couple of expansion points ...

  1. Expanding on alok's answer - your submit button did not have a name attribute on it. This is critical to insure the form field gets bound to the PHP $_POST variable.
  2. Expanding on Ignacio's answer - The HTML5 attribute is very useful but this is only done on the client side. While this is nice from a user experience perspective, you still need to validate form parameters on the server-side. JavaScript: client-side vs. server-side validation
  3. On form submission and completion of previous suggestions, the exit() statement will prevent the user from seeing the form again to resubmit their input. You might actually want this depending on the client ask, but if you don't want that you need to remove the exit statement.
Community
  • 1
  • 1
user2910265
  • 844
  • 1
  • 9
  • 15
  • thanks user2910265.. but i want solve my actually problem. i appriciate Lgnacio which tell me ans with update ver of html5. – user3060235 Dec 13 '13 at 08:45