使用php在多个页面和电子邮件中传递复选框值
Passing checkbox values through multiple pages and email using php
嗨,我正在创建一个简单的宣传册申请表,以获取用户的品牌和价格信息以及他们的电子邮件id。我创建了三个页面的代码(Page1.php用于品牌选择,Page2.php用于价格范围和电子邮件地址。我在下一个页面中获得了Page1.php的用户选择值,但在邮件页面中没有获得值(我需要在这里收集这两个页面值并提交到电子邮件)。请在下面找到页面代码。请帮忙。
page1.php
<html>
<body bgcolor="#fff" text="#000" >
<form method="post" action="page2.php">
<p>Which car you want to buy?<P>
<TABLE border="1" width="500">
<TR ><TD><input type="checkbox" value ="ferrari" name="brand[]"><TD>Ferrari</td></TR>
<TR><TD><input type="checkbox" value ="mercedes" name="brand[]"><TD>Mercedes</td></TR>
<TR><TD><input type="checkbox" value ="bugatti" name="brand[]"><TD>Bugatti</td></TR>
</TABLE>
<input type="hidden" value="1" name="car_brand" >
<p>
<input type="submit" value="Continue" >
</form>
</body>
</html>
第2页
<html>
<body bgcolor="#fff" text="#000" >
<div id="firstpage-value" style="border:1px solid #000;">
<h4>Selected Brand</h4>
<?php
if(isset($_POST['car_brand']))
{
$count=count($_POST['brand']);
for($i=0;$i<$count;$i++)
{
echo $_POST['brand'][$i]." ";
}
}
?>
</div>
<form method="post" action="mail.php">
<p>Choose a Price Range<P>
<TABLE border="1" width="500">
<TR ><TD><input type="checkbox" value ="under20k" name="price[]"><TD>Under $20K</td></TR>
<TR><TD><input type="checkbox" value ="35-85k" name="price[]"><TD>$35K-$85K</td></TR>
<TR><TD><input type="checkbox" value ="over85k" name="price[]"><TD>Over $85K</td></TR>
</TABLE>
<input type="text" name="email" value="email" />
<input type="hidden" value="1" name="car_price" >
<p>
<input type="submit" value="Submit" >
<input type="hidden" name="brand" value="<?php echo $_POST['brand']?>">
</form>
</body>
</html>
mail.php
<?php
/* Subject and email variables */
$emailsSubject = 'Brochure Request';
$thanks = 'Thank you for requesting Brochure';
$webMaster = 'mailsample@gmail.com';
$mailheader .= "Reply-To: ".$_POST["email"]."'r'n";
//Get the input.
$email = $_POST['email'];
$brand = $_POST['brand'];
$price = $_POST['price'];
$body = '
<p>A user has been submitted "Request Brochure" form with the following info.</p>
<table width="600" border="1" >
<tr>
<th width="556" height="37" align="left"><h2>Which car you want to buy?</h2></th>
<th width="268" style="border-bottom:1px solid #ccc; ">'.$brand.'</th>
</tr>
<tr>
<th width="556" height="37" align="left"><h2>Choose price range</h2></th>
<th width="268" style="border-bottom:1px solid #ccc; ">'.$price.'</th>
</tr>
<tr>
<th width="556" height="37" align="left"><h2>Email Address</h2></th>
<th width="268" style="border-bottom:1px solid #ccc; ">'.$emaild.'</th>
</tr>
';
$headers = "From: $email'r'n";
$headers .= "Content-type: text/html'r'n";
$headers .= 'Cc:'. $email . "'r'n";
$reply = "";
/* This is what sends the email */
$success = mail($webMaster, $body, $headers, "-f $email");
/* Results Rendered as Html */
header('location:thankyou.php');exit;
?>
检查每页的以下代码
第1页
<html>
<body bgcolor="#fff" text="#000" >
<form method="post" action="page2.php">
<p>Which car you want to buy?<P>
<TABLE border="1" width="500">
<TR ><TD><input type="checkbox" value ="ferrari" name="brand[]"><TD>Ferrari</td></TR>
<TR><TD><input type="checkbox" value ="mercedes" name="brand[]"><TD>Mercedes</td></TR>
<TR><TD><input type="checkbox" value ="bugatti" name="brand[]"><TD>Bugatti</td></TR>
</TABLE>
<input type="hidden" value="1" name="car_brand" >
<p>
<input type="submit" value="Continue" >
</form>
</body>
第2页
在这个页面中,我使用内爆函数将数组转换为字符串
<html>
<body bgcolor="#fff" text="#000" >
<div id="firstpage-value" style="border:1px solid #000;">
<h4>Selected Brand</h4>
<?php
if(isset($_POST['car_brand']))
{
$count=count($_POST['brand']);
for($i=0;$i<$count;$i++)
{
echo $_POST['brand'][$i]." ";
}
$brand = implode("," ,$_POST['brand']);
}
?>
</div>
<form method="post" action="mail.php">
<p>Choose a Price Range<P>
<TABLE border="1" width="500">
<TR ><TD><input type="checkbox" value ="under20k" name="price[]"><TD>Under $20K</td></TR>
<TR><TD><input type="checkbox" value ="35-85k" name="price[]"><TD>$35K-$85K</td></TR>
<TR><TD><input type="checkbox" value ="over85k" name="price[]"><TD>Over $85K</td></TR>
</TABLE>
<input type="text" name="email" value="email" />
<input type="hidden" value="1" name="car_price" >
<p>
<input type="submit" value="Submit" >
<input type="hidden" name="brand" value="<?php echo $brand; ?>">
</form>
</body>
</html>
邮件页面
<?php
/* Subject and email variables */
$emailsSubject = 'Brochure Request';
$thanks = 'Thank you for requesting Brochure';
$webMaster = 'mailsample@gmail.com';
$mailheader .= "Reply-To: ".$_POST["email"]."'r'n";
//Get the input.
$email = $_POST['email'];
$brand = $_POST['brand'];
$price = implode(",",$_POST['price']);
$body = '
<p>A user has been submitted "Request Brochure" form with the following info.</p>
<table width="600" border="1" >
<tr>
<th width="556" height="37" align="left"><h2>Which car you want to buy?</h2></th>
<th width="268" style="border-bottom:1px solid #ccc; ">'.$brand.'</th>
</tr>
<tr>
<th width="556" height="37" align="left"><h2>Choose price range</h2></th>
<th width="268" style="border-bottom:1px solid #ccc; ">'.$price.'</th>
</tr>
<tr>
<th width="556" height="37" align="left"><h2>Email Address</h2></th>
<th width="268" style="border-bottom:1px solid #ccc; ">'.$email.'</th>
</tr>
';
$headers = "From: $email'r'n";
$headers .= "Content-type: text/html'r'n";
$headers .= 'Cc:'. $email . "'r'n";
$reply = "";
/* This is what sends the email */
$success = mail($webMaster, $body, $headers, "-f $email");
/* Results Rendered as Html */
header('location:thankyou.php');exit;
?>
这里的信息太多,无法发表评论。有几件事你可以记在心里。首先:
检查POST请求
仔细检查处理请求的页面上是否有有效的POST请求,现在您只假设页面2上有任何POST请求。通过这种方式,您可以确保有您需要的数据。例如,在page1.php
上将您的按钮命名为:<input type="submit" name="step1" value="Continue" />
,现在您可以在第2页验证此按钮是否被按下:
if( isset( $_POST['step1'] ))
{
// Process your code, otherwise return to page1
}
验证发送到下一页的数据
通过检查发送的数据,确保从上一页收到正确的数据。您可以使用一个简单的var_dump()
来完成此操作。如果将var_dump()封装在<pre>
标记中,则会得到更好的可读结果。例如:
if( isset( $_POST['step1'] ))
{
echo '<pre>'; var_dump($_POST); echo '</pre>';
}
使用内爆和爆炸
您在步骤2中将$_POST['brand']回声到一个文本字段中,这是不可能的,因为$_POSD['brand']是一个PHP数组。你可以做几个选项,最简单的是使用implode()
,这个函数内爆一个数组并返回一个字符串:
if( count($_POST['brand']) > 0 ) {
$brands = implode(",", $_POST['brand']); // returns: ferrari,mercedes,bugatti
}
要将此字符串返回到数组,可以使用explode(",", $brands);
全部应用并调试
应用所有这些东西,在每个帖子中验证您的请求,并调试哪里出了问题。这些简单的步骤会很快让你的脚本工作起来。你的剧本还有很多需要学习和改进的地方。但是,让我们暂时不谈这些技巧。
问题出现在page2.php上在这里,您正在<input type="hidden" name="brand" value="<?php echo $_POST['brand']?>">
行将数组转换为字符串
解决这个问题的一个简单方法是,您可以使用内爆函数将所有选定的品牌转换为逗号分隔的字符串,然后将该字符串分配给隐藏输入的值,这样您就可以在mail.php页面上访问它。
$brand = '';
if(isset($_POST['brand']))
{
$brand = implode("," ,$_POST['brand']);
}
然后
<input type="hidden" name="brand" value="<?php echo $brand; ?>">
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- 正在验证8个真/假复选框或复选框中的2个
- 为复选框javascript指定两个值
- 如何在单击复选框后调用控制器方法
- 从复选框和Selects-KnockoutJS中获取值的总和
- 选中多个具有相同名称的复选框
- 根据签入的表单,在html电子邮件模板中将复选框标记为true
- 如何通过 PHP 将多个复选框值发送到电子邮件
- 不能发送超过 12 个单选按钮和表单操作邮件中的复选框选项
- PHP 复选框邮件程序为每个错误
- 我想根据复选框从 PDF 发送邮件
- 使用 JavaScript 生成的复选框防止垃圾邮件和机器人
- 使用php在多个页面和电子邮件中传递复选框值
- 动态单击复选框并在列表中添加电子邮件地址,并使用javascript删除重复的电子邮件地址
- 如何获得所有的电子邮件地址为每个复选框,是用jQuery检查
- 当从html, javascript和aspx发送电子邮件时,复选框不转换
- 如何在客户端gridview中获得选定复选框的电子邮件
- 如何通过ajax将值从表单复选框传递到邮件
- 电子邮件发送表单,复选框选择,复选框限制
- 使复选框值追加到文本框,以分号分隔(复制电子邮件地址)