问题:我已经为表单和php编写了html代码来通过电子邮件发送表单的内容,但是只要页面加载就会运行php代码,导致发送一封空白的电子邮件,这是尽管我签入了表单已使用if(isset($_POST['submit']))
另外,由于某些奇怪的原因,如果我删除行'echo'你好“;” php代码根本不起作用!
这是php:
<?php
if (isset($_POST['submit']))
echo"hello";
/* Set e-mail recipient */
$myemail = "contact@birminghamvegans.com";
/* Check all form inputs using check_input function */
$name = $_POST['inputName'];
$email = $_POST['inputEmail'];
$subject = $_POST['inputSubject'];
$message = $_POST['inputMessage'];
/* Let's prepare the message for the e-mail */
$subject2 = "Message from Birmingham Vegans website";
$message = "
Someone has sent you a message using your contact form:
Name: $name
Email: $email
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject2, $message);
?>
这是HTML
<form name="contactform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="form-group">
<label for="inputName" class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputName" name="inputName" placeholder="Your Name" required>
</div>
</div>
<div class="form-group">
<label for="inputEmail1" class="col-lg-2 control-label">Email</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputEmail" name="inputEmail" placeholder="Your Email" required>
</div>
</div>
<div class="form-group">
<label for="inputSubject" class="col-lg-2 control-label">Subject</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputSubject" name="inputSubject" placeholder="Subject Message" required>
</div>
</div>
<div class="form-group">
<label for="inputPassword1" class="col-lg-2 control-label">Message</label>
<div class="col-lg-10">
<textarea class="form-control" rows="4" id="inputMessage" name="inputMessage" placeholder="Your message..." required></textarea>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button type="submit" class="btn btn-default">
Send Message
</button>
</div>
</div>
</form>
答案 0 :(得分:2)
删除echo"hello";
时代码中断的原因是因为您缺少IF
语句的括号,这是语法错误。如果在IF
之类的控制结构之后有一行,则假定该块中的所有行都是有效代码。一旦你有两行以上的花括号必需。
if (isset($_POST['submit'])) {
// mail code goes here
}
您的电子邮件在页面加载时发送的原因是因为您在每次加载页面时都会调用代码的电子邮件部分。如果修复if语句以包含花括号,则此错误消失。
答案 1 :(得分:1)
您的问题在开场if
这是一个完整的陈述:
if (isset($_POST['submit']))
echo"hello";
它与此相同:
if (isset($_POST['submit']))
{
echo"hello";
}
为了解决您的问题,请在if
声明中使用括号:
if (isset($_POST['submit']))
{
echo"hello";
/* Set e-mail recipient */
$myemail = "contact@birminghamvegans.com";
/* Check all form inputs using check_input function */
$name = $_POST['inputName'];
$email = $_POST['inputEmail'];
$subject = $_POST['inputSubject'];
$message = $_POST['inputMessage'];
/* Let's prepare the message for the e-mail */
$subject2 = "Message from Birmingham Vegans website";
$message = "
Someone has sent you a message using your contact form:
Name: $name
Email: $email
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject2, $message);
}
答案 2 :(得分:0)
该行
if (isset($_POST['submit']))
echo"hello";
的if语句仅适用于没有花括号({...}
)的下一行。因此,无论表单提交如何,都会执行mail()
函数。
答案 3 :(得分:0)
您需要在花括号中包装条件代码块。如果没有大括号,则只将条件应用于echo语句(条件之后的下一个语句)并每次运行其余代码。
<?php
if (isset($_POST['submit'])) {
// Inside opening curly brace
echo"hello";
/* Set e-mail recipient */
$myemail = "contact@birminghamvegans.com";
//
// More conditional code here
//
/* Send the message using mail() function */
mail($myemail, $subject2, $message);
// Closing brace
}
答案 4 :(得分:0)
谢谢你们,花括号是一个小学生的错误。完成了错误,需要todo if (isset($_POST['inputName']))