dqdpz60048 2013-12-13 08:19
浏览 24
已采纳

验证消息未显示php

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.

";

  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)."
";
$email_message .= "<b>Subject :</b> ".clean_string($subject)."
";
$email_message .= "<b>Email   :</b> ".clean_string($email_from)."
";
$email_message .= "<b>Email   :</b> ".clean_string($email_from)."
";
$email_message .= "<b>Message :</b> ".clean_string($message)."
";



// create email headers
$headers = 'From: '.$email_from."
".
'Reply-To: '.$email_from."
" .
'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条回答 默认 最新

  • doujiurong7210 2013-12-13 08:22
    关注

    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">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写