无法处理php表单
我是一个新手,对php既老又新。 我正在努力学习php(以及其他一切),以便将其整合到web站点中。 我已经撕下了一个带有php控件的示例表单,以帮助我学习。 将HTML页面和php文件上传到我的web主机上,我发现当我按下submit“按钮”时什么也没有发生——网页上有短暂的停顿,然后是空白页面。 请有人看一下精简的php代码,并告诉我我犯了什么错误? 非常感谢你 HTML代码:无法处理php表单,php,email,Php,Email,我是一个新手,对php既老又新。 我正在努力学习php(以及其他一切),以便将其整合到web站点中。 我已经撕下了一个带有php控件的示例表单,以帮助我学习。 将HTML页面和php文件上传到我的web主机上,我发现当我按下submit“按钮”时什么也没有发生——网页上有短暂的停顿,然后是空白页面。 请有人看一下精简的php代码,并告诉我我犯了什么错误? 非常感谢你 HTML代码: <!DOCTYPE html> <head> <meta c
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Try</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.contact-form {
margin-top: 40px;
}
.contact-form input, .contact-form textarea {
width: 50%;
outline: 0;
border: 1px solid #aaaaaa;
padding: 12px;
color: #999999;
margin-bottom: 30px;
}
.contact-form input:focus, .contact-form textarea:focus {
outline: 0;
}
.contact-form .button {
width: auto;
background: #5f6d7e;
color: white;
text-transform: uppercase;
border: 0;
}
</style>
</head>
<body>
<!-- CONTACT -->
<div id="contact">
<div class="row">
<div>
<h4>Contact Us</h4>
<p>We welcome any enquiries.</p>
</div>
</div>
<div>
<form action="send_form_email.php" method="post" class="contact-form">
<fieldset>
<input type="text" name="first_name" placeholder="Your First name...">
</fieldset>
<fieldset>
<input type="submit" class="button big default" value="Send Message">
</fieldset>
</form>
<!-- .contact-form -->
</div>
</div>
</body>
</html>
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "me@myself.xxx";
$email_subject = "Test php";
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['first_name']) ||
{
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // 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,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$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 .= "First Name: ".clean_string($first_name)."\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
}
?>
尝试
.联络表格{
边缘顶端:40px;
}
.联系人表单输入,.联系人表单文本区域{
宽度:50%;
大纲:0;
边框:1px实心#AAAAA;
填充:12px;
颜色:#999999;
边缘底部:30px;
}
.联系人表单输入:焦点,.联系人表单文本区域:焦点{
大纲:0;
}
.联系方式.按钮{
宽度:自动;
背景:#5f6d7e;
颜色:白色;
文本转换:大写;
边界:0;
}
联系我们
欢迎查询
php代码:
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Try</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.contact-form {
margin-top: 40px;
}
.contact-form input, .contact-form textarea {
width: 50%;
outline: 0;
border: 1px solid #aaaaaa;
padding: 12px;
color: #999999;
margin-bottom: 30px;
}
.contact-form input:focus, .contact-form textarea:focus {
outline: 0;
}
.contact-form .button {
width: auto;
background: #5f6d7e;
color: white;
text-transform: uppercase;
border: 0;
}
</style>
</head>
<body>
<!-- CONTACT -->
<div id="contact">
<div class="row">
<div>
<h4>Contact Us</h4>
<p>We welcome any enquiries.</p>
</div>
</div>
<div>
<form action="send_form_email.php" method="post" class="contact-form">
<fieldset>
<input type="text" name="first_name" placeholder="Your First name...">
</fieldset>
<fieldset>
<input type="submit" class="button big default" value="Send Message">
</fieldset>
</form>
<!-- .contact-form -->
</div>
</div>
</body>
</html>
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "me@myself.xxx";
$email_subject = "Test php";
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['first_name']) ||
{
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // 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,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$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 .= "First Name: ".clean_string($first_name)."\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
}
?>
整个PHP文件被一个大的包围,如果
:
if(isset($_POST['email'])) {
但是您的表单不包含名为email
的字段。因此,要么包含名为email
的文本字段,要么删除if
,要么将if
更改为
if(isset($_POST['first_name'])) {
由于代码似乎会生成电子邮件,因此第一个选项可能是最佳选择。非常感谢您的回复。我所做的是将html和php剥离到我认为最小的程度(以节省空间)。如果我能够发布完整(原始)的HTML和php代码,那会有帮助吗?原始文件中有电子邮件字段。。。。。我是罪魁祸首。