[英]I want to send the details of this form as an email,
The mailing address depends on the city selected from the dropdown list.I have troubles with the php code.please help me.
邮寄地址取决于从下拉列表中选择的城市。我对php代码有疑问。请帮助我。
<form action="">
<ul>
<li><input name="name" type="text" placeholder="type name" /></li>
<li><input name="email" type="email" placeholder="type email"/></li>
<li><input name="num" type="number" placeholder="type contact number" /></li>
<div class="dropdown">
<button type="button" class="btn btn-lg dropdown-toggle btn-block" data-toggle="dropdown" id="city">
Select City
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a href="javascript:void(0);">Sydney</a></li>
<li><a href="javascript:void(0);">Brisbane</a></li>
<li><a href="javascript:void(0);">Melbourne</a></li>
<li><a href="javascript:void(0);">Canberra</a></li>
<li><a href="javascript:void(0);">Darwin</a></li>
<li><a href="javascript:void(0);">Perth</a></li>
</ul>
</div>
<li><input name="submit" type="submit" /></li>
</ul>
</form>
there is my php try
有我的PHP尝试
if(isset($_POST['submit'])) {
switch($_POST['city']) {
case 'Sydney': $to = 'to1@gmail.com'; break;
case 'Brisbane': $to = 'to2@hotmail.com'; break;
}
}
please see below. 请参阅下面。 I have tried and tested code below and it works.
我已经尝试并测试了下面的代码,它可以工作。 try it out.
试试看。
<html>
<head>
<title>Sending Email</title>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#Send").click(function(){
var Name=$('#Name').val();
var Email=$('#Email').val();
var Num=$('#Num').val();
var City= $("#City option:selected").text();
$.post('sendmail.php',{Name:Name,Email:Email,Num:Num, City:City},function(data){
alert(data);
});
});
});
</script>
</head>
<body>
<form onsubmit="return false;" id="UserReg">
<ul>
<li>Name:<input id="Name" type="text" placeholder="type name" /></li>
<li>Email:<input id="Email" type="email" placeholder="type email"/></li>
<li>Number:<input id="Num" type="text" placeholder="type contact number" /></li>
<li>City:<select id="City">
<option value=""></option>
<option value="Sydney">Sydney</option>
<option value="Brisbane">Brisbane</option>
<option value="Melbourne">Melbourne</option>
<option value="Canberra">Canberra</option>
<option value="Darwin">Darwin</option>
<option value="Perth">Perth</option>
</select>
</ul>
<input type="submit" value="Send" id="Send"/>
</form>
</body>
<html>
<?php
if($_POST)
{
$Name=$_POST["Name"];
$Email=$_POST["Email"];
$Num=$_POST["Num"];
$City=$_POST["City"];
$To;
switch($City) {
case 'Sydney': $To = 'to1@gmail.com'; break;
case 'Brisbane': $To = 'to2@hotmail.com'; break;
}
$Message="Name is".$Name." Email is:".$Email." Num is:".$Num;
$Send=mail($To,"Details of".$Name,$Message)
if($Send)
{
echo "Email Sent to".$City;
exit();
}
else
{
echo "Error encountered";
exit();
}
}
?>
because you are not submitting any input regarding city. 因为您没有提交有关城市的任何输入。 use an html input element for city to get it submitted to be available with $_POST here are the list of input elements: http://www.w3schools.com/html/html_forms.asp
使用城市的html输入元素将其提交以供$ _POST使用,以下是输入元素的列表: http : //www.w3schools.com/html/html_forms.asp
on submitting page will redirect to secondpage.php and from the second page mail will go as follows 在提交页面上将重定向到secondpage.php,从第二页发送的邮件将如下所示
and //secondpage.php 和//secondsecond.php
$body=$_POST['name']; $ body = $ _ POST ['name']; mail ($to, $subject, $body, $headers)
邮件($ to,$ subject,$ body,$ headers)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.