你好,我正在为一位客户设计一个网站。我已经设计了这个表单。
<form action="inc/email.php" method="POST" onSubmit="alert('Email send successfully!');">
<div class="col-md-6">
<input type="text" id="contact-name" name="contact-name" value="" placeholder="Your name" required/>
<input type="email" id="contact-email" name="contact-email" value="" placeholder="Your email" required/>
<input type="number" id="contact-phone" name="contact-phone" value="" placeholder="Your phone" required/>
</div>
<div class="col-md-6">
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="audi">Audi</option>
</select>
</div>
<div class="col-md-12">
<input type="submit" value="Send message"/></td>
</div>
</form>
它有三个选项可供选择,第一个是Volvo,第二个是Saab,第三个是Audi。
现在我想根据用户的选择将表单提交重定向到不同的网站
例如,如果用户选择了Volvo,则重定向到facebook.com;如果他选择了Saab,则重定向到Twiiter.com。
我已经为此设计了以下PHP代码。
<?php
/*Getting submitted values from html form*/
$name = $_POST['contact-name'];
$email = $_POST['contact-email'];
$phone = $_POST['contact-phone'];
$cars= $_POST['cars'];
/* Generating Output HTML */
$output = '<html><body><div>';
$output .= '<h2 style="color: #49A020;">Newsletter Email</h2>';
$output .= '<p style="line-height: 28px;font-size: 16px; text-align: justify; color: #333;">Name: '.$name.'</p>';
$output .= 'Phone: <a style="line-height: 28px;font-size: 16px; text-align: justify; color: #333;" href="tel: '.$phone.'">'.$phone.'</a><br><br>';
$output .= 'Email: <a style="line-height: 28px;font-size: 16px; text-align: justify; color: #333;" href="mailto:'.$email.'">'.$email.'</a>';
$output .= '</div><div>';
/*Signature */
$output .= '<hr style="border:1px solid #49A020;">';
$output .= '<p style="margin-top: 0px; margin-bottom: 0px;">Thanks & Regards,</p>';
$output .= '<p style="margin-top: 0px; margin-bottom: 0px;">South Post Fitness,</p>';
$output .= 'Phone: <a href="tel:416843 5925">(416) 843 5925</a><br>';
$output .='Email: <a href="maito: info@xx.com">info@xx.com</a>';
$output .= '</div></body></html>';
$to = 'my email address here'; /* Receivers Email*/
$headers = "From: $email \r\n"; /*Headers*/
$headers .= 'MIME-Version: 1.0' . "\n"; /*Headers*/
$headers .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n"; /*Headers*/
$subject = 'my subject here'; /* Subject */
mail($to, $subject, $output, $headers); /*Sending Mail*/
if(mail) {
if($cars=="Volvo"){
header("Location: http://facebook.com"); /*Redirect to home after sucessfull submission*/
}
elseif($cars=="Saab"){
header("Location: http://twitter.com"); /*Redirect to home after sucessfull submission*/
}
elseif($cars=="Audi"){
header("Location: http://pintrest.com"); /*Redirect to home after sucessfull submission*/
}
}
else {
echo 'Error sending email'; /*If failed display error message */
}
?>
但是这段代码只会将页面重定向到第一个链接,即facebook.com,无论选择哪个选项。