首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用php通过多个页面和电子邮件传递复选框值

使用php通过多个页面和电子邮件传递复选框值
EN

Stack Overflow用户
提问于 2015-10-27 15:24:50
回答 3查看 551关注 0票数 0

嗨,我正在创建一个简单的小册子请求表,以捕获用户的品牌和价格信息,以及他们的电子邮件id。我创建了三个页面的代码(Page1.php用于品牌选择,Page2.php用于价格范围和电子邮件地址。我在下一页获得了page1.php的用户选定值,但在邮件页面中没有得到值(我需要在这里收集这两个页面值并提交到电子邮件)。请在下面找到页面代码。请帮帮忙。

page1.php

代码语言:javascript
运行
复制
<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>

page2

代码语言:javascript
运行
复制
<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

代码语言:javascript
运行
复制
<?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;
?>

EN

Stack Overflow用户

发布于 2015-10-27 16:25:47

问题出在page2.php上,这里您在<input type="hidden" name="brand" value="<?php echo $_POST['brand']?>">行将数组转换为字符串

这个问题的一个简单解决方法是,您可以使用implode函数将所有选定的品牌转换为逗号分隔的字符串,然后将该字符串分配给隐藏输入的值,以便您可以在mail.php页面上访问它。

代码语言:javascript
运行
复制
$brand = '';
if(isset($_POST['brand']))
{   
    $brand = implode("," ,$_POST['brand']);
}

然后

代码语言:javascript
运行
复制
<input type="hidden" name="brand" value="<?php echo $brand; ?>">
票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33362050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档