我是php的初学者。我设计了下面的代码来运行php表单。
如果我点击提交按钮。它没有显示必填字段的信息。 请告诉我代码编写的错误在哪里。
由于
<?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'); ?>
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
}
?>
答案 0 :(得分:2)
更改
<form class="email" name="form1" action="inquery.php" method="post">
到
<form class="email" name="form1" action="" method="post">
和
<input class="send" type="submit" name="submit" value="submit">
答案 1 :(得分:0)
您可以使用HTML5,将必需属性添加到您的字段中:
<!DOCTYPE html>
<html>
<body>
<form action="http://google.com">
<input type="text" name="q" required/>
<input type="submit" />
</form>
</body>
</html>
请记住,服务器端的PHP流程数据,如果您需要在客户端进行更复杂的验证,则应使用JavaScript(JQuery)。
答案 2 :(得分:0)
@ user3060235 - 几个扩展点......