PHP联系表格不会提交 [英] PHP contact form will not submit
问题描述
我有一个简单的php联系表单,我从一个web教程中获得。它昨天有效,但今天不行。我喜欢一些帮助,因为我不知道这些php。
php:
<?php
//如果表单被提交
if(isset($ _ POST ['submit'])){
//检查以确保名称字段不为空
if(trim($ _ POST ['contactname'])==''){
$ hasError = true;
} else {
$ name = trim($ _ POST ['contactname']);
//检查以确保主题字段不为空
if(trim($ _ POST ['subject'])=='' ){
$ hasError = true;
} else {
$ subject = trim($ _ POST ['subject']);
}
//检查以确保提交了有效的电子邮件地址
if(trim($ _ POST ['email'])==''){
$ hasError = true;
} else if(!filter_var(trim($ _ POST ['email'],FILTER_VALIDATE_EMAIL))){
$ hasError = true;
} else {
$ email = trim($ _ POST ['email']);
}
//检查并确认输入
if(trim($ _ POST ['message'])==''){
$ hasError = true;
} else {
if(function_exists('stripslashes')){
$ comments = stripslashes(trim($ _ POST ['message']));
} else {
$ comments = trim($ _ POST ['message']);
//如果没有错误,发送邮件
if(!isset($ hasError)){
$ emailTo = 'person@domain.com'; //将您自己的电子邮件地址放在这里
$ body =名称:$ name \\\
\\\
Email:$ email \\\
\\\
Subject:$ subject \\\
\\\
Comments:\\\
$ comments ;
$ headers ='From:My Site<'。$ emailTo。'>'。 \r\\\
。 '回复: ' 。 $电子邮件;
邮件($ emailTo,$ subject,$ body,$ headers);
$ emailSent = true;
}
}
?>
HTML:
< form role =formmethod =postaction =<?php echo $ _SERVER ['PHP_SELF'];?> ID = 联系形式 >
<?php if(isset($ hasError)){//如果发现错误?>
请检查您是否填写了所有包含有效信息的字段,然后重试。谢谢。< / p>
<?php}?>
<?php if(isset($ emailSent)&& $ emailSent == true){//如果发送电子邮件?>
< div class =alert alert-success>
< p>< strong>讯息已成功发送!< / strong>< / p>
< p>感谢您使用我们的联系表单< strong><?php echo $ name;?>< / strong>!您的电子邮件已成功发送,我们会尽快与您联系。< / p>
< / div>
<?php}?>
< div class =form-group>
< label for =name>您的名称< span class =help-required> *< / span>< / label>
< input type =textname =contactnameid =contactnamevalue =class =form-control requiredrole =inputaria-required =true/>
< / div>
< div class =form-group>
< label for =email>您的电子邮件< span class =help-required> *< / span>< / label>
< input type =textname =emailid =emailvalue =class =form-control required emailrole =inputaria-required =true/>
< / div>
< div class =form-group>
< label for =subject> Subject< span class =help-required> *< / span>< / label>
< input type =textname =emailid =subjectclass =form-control requiredrole =inputaria-required =true>
< / div>
< div class =form-group>
< label for =message> Message< span class =help-required> *< / span>< / label>
< / div>
< div class =actions>
< input type =submitvalue =发送消息name =submitid =submitButtonclass =btn btn-greytitle =点击这里提交您的消息! />
< input type =resetvalue =清除表格class =btn btn-gray pull-righttitle =从表格中删除所有数据。 />
< / div>
< / form>
它在验证中被挂起。不知道为什么。
$ _ POST [subject]未在您的表单中定义。您的SUBJECT字段被称为EMAIL:
更改:
< div class =form-group >
< label for =subject> Subject< span class =help-required> *< / span>< / label>
< input type =text name =emailid =subjectclass =form-control requiredrole =inputaria-required =true>
< / div>
$ c
< div class =form-group>
< label for =subject> Subject< span class =help-required> *< / span>< / label>
< input type =textname =subjectid =subjectclass =form-control requiredrole =inputaria-required =true>
< / div>
I have a simple php contact form i got from a web tutorial. It worked yesterday, but will not work today. I'd love some help, as I don;t know much php.
php:
<?php
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!filter_var( trim($_POST['email'], FILTER_VALIDATE_EMAIL ))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'person@domain.com'; // Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
HTML:
<form role="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
<?php if(isset($hasError)) { //If errors are found ?>
<p class="alert alert-danger">Please check if you've filled all the fields with valid information and try again. Thank you.</p>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<div class="alert alert-success">
<p><strong>Message Successfully Sent!</strong></p>
<p>Thank you for using our contact form, <strong><?php echo $name;?></strong>! Your email was successfully sent and we’ll be in touch with you soon.</p>
</div>
<?php } ?>
<div class="form-group">
<label for="name">Your Name<span class="help-required">*</span></label>
<input type="text" name="contactname" id="contactname" value="" class="form-control required" role="input" aria-required="true" />
</div>
<div class="form-group">
<label for="email">Your Email<span class="help-required">*</span></label>
<input type="text" name="email" id="email" value="" class="form-control required email" role="input" aria-required="true" />
</div>
<div class="form-group">
<label for="subject">Subject<span class="help-required">*</span></label>
<input type="text" name="email" id="subject" class="form-control required" role="input" aria-required="true">
</div>
<div class="form-group">
<label for="message">Message<span class="help-required">*</span></label>
<textarea rows="8" name="message" id="message" class="form-control required" role="textbox" aria-required="true"></textarea>
</div>
<div class="actions">
<input type="submit" value="Send Your Message" name="submit" id="submitButton" class="btn btn-grey" title="Click here to submit your message!" />
<input type="reset" value="Clear Form" class="btn btn-grey pull-right" title="Remove all the data from the form." />
</div>
</form>
It gets hung up on the validation. Not sure why.
解决方案 $_POST["subject] is not defined in your form. Your SUBJECT field is called EMAIL:
Change:
<div class="form-group">
<label for="subject">Subject<span class="help-required">*</span></label>
<input type="text" name="email" id="subject" class="form-control required" role="input" aria-required="true">
</div>
With:
<div class="form-group">
<label for="subject">Subject<span class="help-required">*</span></label>
<input type="text" name="subject" id="subject" class="form-control required" role="input" aria-required="true">
</div>
这篇关于PHP联系表格不会提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!