简体   繁体   English

努力处理PHP表单处理

[英]Struggling with PHP form processing

Can anyone help with a form problem that I just can't fathom? 有人可以解决我无法理解的表格问题吗? I have a group of checkboxes whose values I can't get to show up in the resulting mail. 我有一组复选框,其值无法显示在结果邮件中。

This is the method I'm using to set the variables: 这是我用来设置变量的方法:

    $benefit01  = ($_POST['benefit_01']);
$benefit02  = ($_POST['benefit_02']);
$benefit03  = ($_POST['benefit_03']);
$benefit04  = ($_POST['benefit_04']);
$benefit05  = ($_POST['benefit_05']);
$benefit06  = ($_POST['benefit_06']);

$property   = addslashes($_POST['property']);
$owner      = addslashes($_POST['owner']);
$mainHeating    = addslashes($_POST['Heating_mainHeating_answer']);
$boiler_working = addslashes($_POST['boiler_working']);
$boiler_age = addslashes($_POST['boiler_age']);

$postcode   = addslashes($_POST['postcode']);
$address1   = addslashes($_POST['address1']);
$address2   = addslashes($_POST['address2']);
$address3   = addslashes($_POST['address3']);
$city       = addslashes($_POST['town']);
$county     = addslashes($_POST['county']);

$name       = addslashes($_POST['firstName']);
$surname    = addslashes($_POST['surname']);
$email      = addslashes($_POST['email']);
$phone      = addslashes($_POST['phonenum']);


$type   = addslashes($_POST['type']);
$time   =   $_POST['time'];
$src = $_POST['urlsrc'] ;

$benefits   = $benefit01." ".$benefit02." ".$benefit03." ".$benefit04." ".$benefit05." ".$benefit06;
$address = $address1." ".$address2;
$note = "Benefits: ".$benefits.". Property Type: ".$property.". Home Owner: ".$owner.". Type of Heating: ".$mainHeating.". Boiler Working: ".$boiler_working.". Boiler Age: ".$boiler_age.". Timescales: ".$time.". Address Line 3: ".$address3;

So the "benefits" are checkboxes. 因此,“好处”是复选框。 I had tried naming them all the same to create an array then imploding the values but that didn't work either. 我曾尝试将它们全部命名为相同的名称,以创建一个数组,然后内插值,但这也不起作用。 Ultimately this data is sent to a dbase which is why most of the info is dumped into a $note variable. 最终,这些数据被发送到数据库,这就是为什么大多数信息都被转储到$ note变量中的原因。

I don't know much about this and have taken this example from a working (albeit simplified) form. 我对此了解不多,并从一个有效的形式(尽管简化了)中举了这个例子。 Any assistance appreciated. 任何帮助表示赞赏。

** Small sample of the form added below: **以下表格中的小样:

    <input name="ctl00_chkBenefit" type="checkbox" id="benefit_01" class="checkbox-benefit" value="Child Tax Credit" />

Child Tax Credit (where the relevant income is £15,860 or less) 儿童税收抵免(相关收入为£15,860或以下)

     <input name="ctl01_chkBenefit" type="checkbox" id="benefit_02" class="checkbox-benefit" value="State Pension Credit" />

State Pension Credit 国家养老金信贷

Is your checkbox's HTML like this? 您的复选框的HTML是这样的吗?

<input type="checkbox" name="benefit_01" value="Benefit 01" /> Benefit 01

Then if that checkbox will be checked by user, $_POST['benefit_01'] will be the value of that checkbox - Benefit 01 . 然后,如果该复选框将由用户选中,则$_POST['benefit_01']将是该复选框的值$_POST['benefit_01'] 01

If it will be not checked, $_POST['benefit_01'] will be not set. 如果未选中,则不会设置$_POST['benefit_01']

EDITED 1: 编辑1:

And that all you should have in <form> , with setted method and action: 并且您应该在<form>拥有所有内容,并具有设置的方法和操作:

<form action="index.php" method="post">
     <input name="ctl00_chkBenefit" type="checkbox" id="benefit_01" class="checkbox-benefit" value="Child Tax Credit" />
</form>

EDITED 2: 编辑2:

You have different name for that checkboxes. 您为该复选框使用其他name You used in your $_POST theirs id , not name . 您在$_POST使用了他们的id ,而不是name

Change your code to this: 将代码更改为此:

$benefit01  = ($_POST['ctl00_chkBenefit']);
$benefit02  = ($_POST['ctl01_chkBenefit']);
$benefit03  = ($_POST['ctl02_chkBenefit']);
$benefit04  = ($_POST['ctl03_chkBenefit']);
$benefit05  = ($_POST['ctl04_chkBenefit']);
$benefit06  = ($_POST['ctl05_chkBenefit']);

Now it will work for 100% :) 现在它将适用于100%:)

First of all, 首先,

<input type="checkbox" name="benefits[]" value="one">
<input type="checkbox" name="benefits[]" value="2">
<input type="checkbox" name="benefits[]" value="III">

would create an array $_POST['benefits'] in PHP. 将在PHP中创建$ _POST ['benefits']数组。

Second, the variable will only contain the value if the checkbox is checked. 其次,如果选中此复选框,则变量将仅包含值。

Input checkboxes aren't set in $_POST if not checked by the user. 如果用户未选中输入复选框,则不会在$ _POST中设置输入复选框。 You can use empty() to determine if they're set and/or empty, and then set the value to the one you want to insert into the database. 您可以使用empty()来确定它们是否已设置和/或为空,然后将值设置为要插入数据库的值。

crate the input field name like this 

<input type="checkbox" name="c[]" value="1">
<input type="checkbox" name="c[]" value="2">
<input type="checkbox" name="c[]" value="3">
<input type="submit" >

And PHP you can write like this- 而PHP您可以这样写:

<?php 
$_POST['benefits'] 
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM