I want to let my visitors check some checkboxes. When they submit the form I would like to check which checkboxes are checked and then send an email to the visitor. My problem is that I don't want to show the value but the label.
我想让我的访问者查看一些复选框。当他们提交表单时,我想检查选中的复选框,然后向访问者发送电子邮件。我的问题是我不想显示价值而是标签。
So if someone checks for example the "bike" checkbox I want to send an email with text containing the $bike
but I want to show the label instead of the value.
因此,如果有人检查例如“自行车”复选框,我想发送包含$ bike的文本的电子邮件,但我想显示标签而不是值。
<input type="checkbox" name="vehicle" value="bike">
<label for="bike">This is my bike</label>
<input type="checkbox" name="vehicle" value="car">
<label for="car">This is my car</label>
<input type="checkbox" name="vehicle" value="bus">
<label for="bus">This is my bus</label>
<input type="checkbox" name="vehicle" value="plane">
<label for="plane">This is my plane</label>
<input type="checkbox" name="vehicle" value="train">
<label for="train">This is my train</label>
<input type="submit" value="Submit">
3 个解决方案
#1
0
Labels are not passed to PHP. You'll need logic on the backend to turn train
into This is my train
.
标签不会传递给PHP。你需要后端的逻辑把火车变成这是我的火车。
#2
0
$translate = array (
'bike'=>'This is my bike',
...
);
// Assuming vehicle has been declared vehicle[] and is passed as an array:
foreach ($_POST ['vehicle'] as $v)
{
$label = $translate [$v];
....
}
Labels are not input fields and so are not sent to the server. Why would they? They don't change and the server knows what they are!
标签不是输入字段,因此不会发送到服务器。他们为什么?它们不会改变,服务器知道它们是什么!
In all probability, you would have used the translate array to generate the fields in the first place:
很可能,你会使用translate数组首先生成字段:
foreach ($translate as $value=>$label)
{
echo "<input type='checkbox' name='vehicle[]' value='$value'>\n"
echo "<label for='$value'>$label</label>\n"
}
#3
0
I would just build a one-page execution/form class. The __construct()
is just taking hard coded values, but you could feed a database connection and return an array there as well.
我只想构建一个单页的执行/表单类。 __construct()只是获取硬编码值,但您也可以提供数据库连接并返回一个数组。
class Vehicles
{
protected $array;
public function __construct()
{
$this->array['vehicle']['bike'] = 'This is my bike';
$this->array['vehicle']['car'] = 'This is my car';
$this->array['vehicle']['bus'] = 'This is my bus';
$this->array['vehicle']['plane'] = 'This is my plane';
$this->array['vehicle']['train'] = 'This is my train';
}
public function Form()
{ ?>
<form method="post" action="">
<input type="hidden" name="command" value="true" /><?php
foreach($this->array as $type => $kind) {
foreach($kind as $key => $value) {?>
<input type="checkbox" name="<?php echo $type; ?>[]" value="<?php echo $key; ?>">
<label for="<?php echo $key; ?>"><?php echo $value; ?></label>
<?php }
} ?>
<input type="submit" value="Submit">
</form>
<?php }
public function SendMail($email)
{
if(isset($_POST['command']) && $_POST['command'] == 'true') {
foreach($_POST['vehicle'] as $value) {
if(isset($this->array['vehicle'][$value]))
$selection[] = $this->array['vehicle'][$value];
}
$subject = "Subject: Vehicle Selected";
$message = implode("\r\n",$selection);
$header = "From: Automator Send<email@from.addr>";
if(mail($email,$subject,$message,$header))
echo 'Sent';
}
else
$this->Form();
}
}
// Initiate Class
$Automaker = new Vehicles();
// Run main function
$Automaker->SendMail('email@address.com'); ?>
#1
0
Labels are not passed to PHP. You'll need logic on the backend to turn train
into This is my train
.
标签不会传递给PHP。你需要后端的逻辑把火车变成这是我的火车。
#2
0
$translate = array (
'bike'=>'This is my bike',
...
);
// Assuming vehicle has been declared vehicle[] and is passed as an array:
foreach ($_POST ['vehicle'] as $v)
{
$label = $translate [$v];
....
}
Labels are not input fields and so are not sent to the server. Why would they? They don't change and the server knows what they are!
标签不是输入字段,因此不会发送到服务器。他们为什么?它们不会改变,服务器知道它们是什么!
In all probability, you would have used the translate array to generate the fields in the first place:
很可能,你会使用translate数组首先生成字段:
foreach ($translate as $value=>$label)
{
echo "<input type='checkbox' name='vehicle[]' value='$value'>\n"
echo "<label for='$value'>$label</label>\n"
}
#3
0
I would just build a one-page execution/form class. The __construct()
is just taking hard coded values, but you could feed a database connection and return an array there as well.
我只想构建一个单页的执行/表单类。 __construct()只是获取硬编码值,但您也可以提供数据库连接并返回一个数组。
class Vehicles
{
protected $array;
public function __construct()
{
$this->array['vehicle']['bike'] = 'This is my bike';
$this->array['vehicle']['car'] = 'This is my car';
$this->array['vehicle']['bus'] = 'This is my bus';
$this->array['vehicle']['plane'] = 'This is my plane';
$this->array['vehicle']['train'] = 'This is my train';
}
public function Form()
{ ?>
<form method="post" action="">
<input type="hidden" name="command" value="true" /><?php
foreach($this->array as $type => $kind) {
foreach($kind as $key => $value) {?>
<input type="checkbox" name="<?php echo $type; ?>[]" value="<?php echo $key; ?>">
<label for="<?php echo $key; ?>"><?php echo $value; ?></label>
<?php }
} ?>
<input type="submit" value="Submit">
</form>
<?php }
public function SendMail($email)
{
if(isset($_POST['command']) && $_POST['command'] == 'true') {
foreach($_POST['vehicle'] as $value) {
if(isset($this->array['vehicle'][$value]))
$selection[] = $this->array['vehicle'][$value];
}
$subject = "Subject: Vehicle Selected";
$message = implode("\r\n",$selection);
$header = "From: Automator Send<email@from.addr>";
if(mail($email,$subject,$message,$header))
echo 'Sent';
}
else
$this->Form();
}
}
// Initiate Class
$Automaker = new Vehicles();
// Run main function
$Automaker->SendMail('email@address.com'); ?>