我的问题:我正在尝试使此表单通过电子邮件发送表单数据并使用PHP脚本重定向到下载页面(即:一键= 2个操作)。我搜查了电路板,并没有发现任何类似于我想做的事情。我曾尝试过几种代码选项,但它根本不会发送电子邮件。我做错了什么?
代码: 形式:
<form id="myform">
<form method="get" action="action/php">
<fieldset><center>
<h3>DOWNLOAD DVD</h3>
<p> Enter your full name and email and then press Download DVD. </p>
<p><br>
<label>Enter Your Name *</label>
<input type="text" name="name" pattern="[a-zA-Z ]{5,}" maxlength="30" />
</p>
<p>
<label>Enter Your Email *</label>
<input type="email" name="email" required />
</p>
<button type="submit" id="submit-myform"; class="submit" value="Submit" name="myform_submit">Download DVD</button>
<button type="reset">Reset</button>
</fieldset>
</form>
PHP:
<?PHP
if(isset($_POST['myform_submit']) && $_POST['myform_submit'] == "Submit"){
echo "http://www.website.com";
}else {
mail( "info@website.com", "Landing Page Download",
$name, "From: $email" );
}
?>
再次......下载内容很好。但是电子邮件不会发送。
答案 0 :(得分:1)
我认为你的if语句已经混淆了。目前它正在说if the form is submitted, then print a URL to the screen, otherwise send an email
,但根据您所说的,您想要重定向和发送电子邮件。试试这个:
if(isset($_POST['myform_submit'])) {
$send = mail( "info@website.com", "Landing Page Download", $_POST['name'], "From: " . $_POST['email'] );
if($send) {
header("Location: http://www.website.com");
} else {
echo 'Error sending email!';
}
}
Problem number 2 is you have nested forms。不确定为什么要这样做,but it's against HTML spec并且可能会导致您的表单数据无法按原样发送。删除外部表格。这是HTML3(旧!)规范的第3行:
请注意,您不能嵌套FORM元素!
问题3,您将表单方法设置为GET,然后尝试访问POST变量。问题3.5,你的动作是action/php
- 那不是文件名(除非你在名为php的文件夹里面有一个index.php文件,在一个名为action的文件夹中)。将所有这些改为:
<form method="post" id="myform" action="action.php">
注意: header("Location: [url]")
会向您的浏览器发送重定向标头,因此您将被重定向到目标网址。如果您只想显示该网址(如问题中所示),请继续echo
。