我创建了一个简单的HTML页面index.html
,用户可以在其中提交消息。创建了另一个PHP文件email_form.php
,以捕获表单提交并将表单内容发送到我的电子邮件地址。
index.html:
<form name="contactform" method="POST" action="email_form.php">
<div class="form-group">
<div class="form-row">
<div class="col" id="prenom">
<input type="text" class="form-control" placeholder="Prénom" required="True" name="prenom">
</div>
<div class="col" id="nom">
<input type="text" class="form-control" placeholder="Nom" required="True" name="nom">
</div>
</div>
<div class="col" id="age">
<input type="number" class="form-control" placeholder="Age" required="True" name="age">
</div>
<div class="form-row">
<div class="col" id="tel">
<input type="tel" class="form-control" placeholder="Téléphone" name="tel">
</div>
<div class="col" id="mail">
<input type="email" class="form-control" placeholder="Email" name="email">
</div>
</div>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3" placeholder="Your message" name="message"></textarea>
<div class="row">
<div class="col-xs-6" id="button">
<button type="submit" name="submit" class="btn btn-primary">Send</button>
</div>
</div>
</div>
</form>
email_form.php:
<?php
if(isset($_POST['submit'])) {
$email_to = "info@mysite.com";
$email_subject = "Email subject";
$prenom = $_POST['prenom'];
$nom = $_POST['nom'];
$age = $_POST['age'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$message = $_POST['message'];
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Prénom: ".clean_string($prenom)."\n";
$email_message .= "Nom: ".clean_string($nom)."\n";
$email_message .= "Age: ".clean_string($age)."\n";
$email_message .= "Telephone: ".clean_string($tel)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = "From: ".$email."\n\n";
mail($email_to, $email_subject, $email_message, $headers);
header("Location: index.html");
}
?>
在网站上,当我尝试提交消息时,它会将我重定向到如下所示的链接:mysite.com/email_form.php
,并显示错误消息“找不到页面”。我对PHP不太了解,也不知道我在做什么错。
答案 0 :(得分:0)
首先,您应该有一个结束表单标签</form>
然后:
将php代码粘贴到HTML文件的顶部,然后
将文件扩展名更改为.php
将表单的操作更改为index.php
您的index.php应该看起来像这样:
<?php
if(isset($_POST['submit'])) {
$email_to = "info@mysite.com";
$email_subject = "Email subject";
$prenom = $_POST['prenom'];
$nom = $_POST['nom'];
$age = $_POST['age'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$message = $_POST['message'];
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Prénom: ".clean_string($prenom)."\n";
$email_message .= "Nom: ".clean_string($nom)."\n";
$email_message .= "Age: ".clean_string($age)."\n";
$email_message .= "Telephone: ".clean_string($tel)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = "From: ".$email."\n\n";
mail($email_to, $email_subject, $email_message, $headers);
header("Location: index.html");
}
?>
<form name="contactform" method="POST" action="index.php">
<div class="form-group">
<div class="form-row">
<div class="col" id="prenom">
<input type="text" class="form-control" placeholder="Prénom" required="True" name="prenom">
</div>
<div class="col" id="nom">
<input type="text" class="form-control" placeholder="Nom" required="True" name="nom">
</div>
</div>
<div class="col" id="age">
<input type="number" class="form-control" placeholder="Age" required="True" name="age">
</div>
<div class="form-row">
<div class="col" id="tel">
<input type="tel" class="form-control" placeholder="Téléphone" name="tel">
</div>
<div class="col" id="mail">
<input type="email" class="form-control" placeholder="Email" name="email">
</div>
</div>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3" placeholder="Your message" name="message"></textarea>
<div class="row">
<div class="col-xs-6" id="button">
<button type="submit" name="submit" class="btn btn-primary">Send</button>
</div>
</div>
</div>
</form>
答案 1 :(得分:0)
我已通过更改email_form.php中的header
解决了该问题。现在看起来像这样:
header("Location: https://example.com");
它工作正常。