通过多个页面传递复选框值并使用php发送电子邮件

时间:2022-11-27 18:14:33

Hi I am creating a simple brochure request form to capture users brand and price information along with their email id. I created code for three pages (Page1.php is for brand selection, Page2.php is for price range and email address. I get user selected value for page1.php in the next page but I am not getting values in mail page (I need to collect those two page values here and submit to email). Please find the pages code below. Please help.

嗨,我正在创建一个简单的宣传册申请表,以捕获用户的品牌和价格信息以及他们的电子邮件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>

page2

<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;
?>

3 个解决方案

#1


0  

Check below code for each page

检查每页的下面代码

Page1

<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>

Page2

In this page I used implode function for convert array to string

在这个页面中,我使用了implode函数将数组转换为字符串

<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>

mail page

<?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;
?>

#2


0  

There's too much information here to place a comment. There are a few things you can keep in mind. First of all:

这里有太多的信息要发表评论。你可以记住一些事情。首先:

Check the POST request

Double check if there's a valid POST-request at the page that handles a request, now you are just assuming there was any on page2. This way you can be sure that there's data you need. For example, on page1.php name your button: <input type="submit" name="step1" value="Continue" />, now you can validate on page2 wether this button is pressed:

仔细检查处理请求的页面上是否有有效的POST请求,现在您只是假设第2页上有任何请求。这样您就可以确定您需要的数据。例如,在page1.php上命名你的按钮:,现在你可以在第2页上验证按下此按钮:

if( isset( $_POST['step1'] )) 
{
    // Process your code, otherwise return to page1
}

Validate the data that was send to the next page

Make sure you receive the correct data from the previous page by checking what data was send along. You can do this with a simple var_dump(). If you wrap var_dump() within <pre> tags it gives you a better readable result. For example:

通过检查发送的数据,确保从上一页收到正确的数据。您可以使用简单的var_dump()来完成此操作。如果在

标记内包装var_dump(),它会为您提供更好的可读结果。例如:

if( isset( $_POST['step1'] )) 
{
    echo '<pre>'; var_dump($_POST); echo '</pre>';
}

Use implode and explode

You are echo'ing $_POST['brand'] in step2 into a text field, this is impossible since $_POST['brand'] is a PHP array. There are a few options you can do, the most simple is to use implode(), this function implodes an array and returns a string:

你在第二步回到$ _POST ['brand']到文本字段,这是不可能的,因为$ _POST ['brand']是一个PHP数组。你可以做几个选项,最简单的就是使用implode(),这个函数会破坏一个数组并返回一个字符串:

if( count($_POST['brand']) > 0 ) {
    $brands = implode(",", $_POST['brand']);  // returns: ferrari,mercedes,bugatti
}

To return this string back to an array, you can use explode(",", $brands);

要将此字符串返回到数组,您可以使用explode(“,”,$ brands);

Apply all and debug

Apply al these things, validate your request each post and debug what's going wrong and where. These simple steps will get your script working soon enough. There's still a lot to learn and improve on your script. But let's leave it by these tips for now.

应用这些东西,验证每个帖子的请求并调试出错的地方和位置。这些简单的步骤将使您的脚本尽快运行。您的脚本还有很多需要学习和改进的地方。但是现在让我们通过这些提示离开它。

#3


0  

The problem is on page2.php Here you are converting array to string at line <input type="hidden" name="brand" value="<?php echo $_POST['brand']?>">

问题出在page2.php这里你将数组转换为字符串”>

One simple fix to this problem is you can make use of implode function to convert all the selected brands to comma separate string and then assign that string to value of hidden input so that you can access it on mail.php page.

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

$brand = '';
if(isset($_POST['brand']))
{   
    $brand = implode("," ,$_POST['brand']);
}

Then

<input type="hidden" name="brand" value="<?php echo $brand; ?>">

#1


0  

Check below code for each page

检查每页的下面代码

Page1

<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>

Page2

In this page I used implode function for convert array to string

在这个页面中,我使用了implode函数将数组转换为字符串

<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>

mail page

<?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;
?>

#2


0  

There's too much information here to place a comment. There are a few things you can keep in mind. First of all:

这里有太多的信息要发表评论。你可以记住一些事情。首先:

Check the POST request

Double check if there's a valid POST-request at the page that handles a request, now you are just assuming there was any on page2. This way you can be sure that there's data you need. For example, on page1.php name your button: <input type="submit" name="step1" value="Continue" />, now you can validate on page2 wether this button is pressed:

仔细检查处理请求的页面上是否有有效的POST请求,现在您只是假设第2页上有任何请求。这样您就可以确定您需要的数据。例如,在page1.php上命名你的按钮:,现在你可以在第2页上验证按下此按钮:

if( isset( $_POST['step1'] )) 
{
    // Process your code, otherwise return to page1
}

Validate the data that was send to the next page

Make sure you receive the correct data from the previous page by checking what data was send along. You can do this with a simple var_dump(). If you wrap var_dump() within <pre> tags it gives you a better readable result. For example:

通过检查发送的数据,确保从上一页收到正确的数据。您可以使用简单的var_dump()来完成此操作。如果在

标记内包装var_dump(),它会为您提供更好的可读结果。例如:

if( isset( $_POST['step1'] )) 
{
    echo '<pre>'; var_dump($_POST); echo '</pre>';
}

Use implode and explode

You are echo'ing $_POST['brand'] in step2 into a text field, this is impossible since $_POST['brand'] is a PHP array. There are a few options you can do, the most simple is to use implode(), this function implodes an array and returns a string:

你在第二步回到$ _POST ['brand']到文本字段,这是不可能的,因为$ _POST ['brand']是一个PHP数组。你可以做几个选项,最简单的就是使用implode(),这个函数会破坏一个数组并返回一个字符串:

if( count($_POST['brand']) > 0 ) {
    $brands = implode(",", $_POST['brand']);  // returns: ferrari,mercedes,bugatti
}

To return this string back to an array, you can use explode(",", $brands);

要将此字符串返回到数组,您可以使用explode(“,”,$ brands);

Apply all and debug

Apply al these things, validate your request each post and debug what's going wrong and where. These simple steps will get your script working soon enough. There's still a lot to learn and improve on your script. But let's leave it by these tips for now.

应用这些东西,验证每个帖子的请求并调试出错的地方和位置。这些简单的步骤将使您的脚本尽快运行。您的脚本还有很多需要学习和改进的地方。但是现在让我们通过这些提示离开它。

#3


0  

The problem is on page2.php Here you are converting array to string at line <input type="hidden" name="brand" value="<?php echo $_POST['brand']?>">

问题出在page2.php这里你将数组转换为字符串”>

One simple fix to this problem is you can make use of implode function to convert all the selected brands to comma separate string and then assign that string to value of hidden input so that you can access it on mail.php page.

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

$brand = '';
if(isset($_POST['brand']))
{   
    $brand = implode("," ,$_POST['brand']);
}

Then

<input type="hidden" name="brand" value="<?php echo $brand; ?>">