所以整个场景如下:
我有一个窗体在我的网站上有多个值的复选框。例如,"price"有四个复选框。用户可以选择从无到所有四个复选框的任意位置。
我无法将"SELECTED"复选框值插入数据库的表中。我已经尝试了所有我能在Google和Stackoverflow上找到的方法。
我对PHP比较陌生,所以我对它知之甚少。但是为了解决这个问题,我查阅了各种文档,实际上有很多。
无论如何,这是我现在正在做的,它是有效的。但这是一个糟糕的方法,我知道会有更有效的方法。
<div class="panel-body">
<label class="checkbox-inline"><input type="checkbox" name="price1" value="Less than 10,000, "> Less than 10,000</label>
<label class="checkbox-inline"><input type="checkbox" name="price2" value="10,001 to 15000, "> 10,001 - 15000</label>
<label class="checkbox-inline"><input type="checkbox" name="price3" value="15,001 to 25000, "> 15,001 - 25000 </label>
<label class="checkbox-inline"><input type="checkbox" name="price4" value="25,001 to 35000, "> 25,001 - 35000 </label>
<label class="checkbox-inline"><input type="checkbox" name="price5" value="35,001 and more, "> 35,001 and more </label>
</div>
所以基本上价格的每个复选框都有自己独立的名称。
对应的PHP如下
<?php
session_start();
$_SESSION["f1"]=$_REQUEST["price1"];
$_SESSION["f2"]=$_REQUEST["price2"];
$_SESSION["f3"]=$_REQUEST["price3"];
$_SESSION["f4"]=$_REQUEST["price4"];
$_SESSION["f5"]=$_REQUEST["price5"];
$_SESSION["f"]=$_SESSION["f1"].$_SESSION["f2"].$_SESSION["f3"].$_SESSION["f4"].$_SESSION["f5"];
$z6=$_SESSION["f"];
require_once('ConnectDB.php');
$conn=mysql_connect(SERVER,USER,PASSWORD);
mysql_select_db(DATABASE,$conn);
query="insert into mobile_frm values('".$z6."')";
mysql_query($query) or die(mysql_error());
echo"Thanks for using us";
header('Location: gadget.html');
mysql_close($conn);
?>
这是在数据库中写入表单值的PHP脚本。我只包含了它的一部分,以保持它的简单明了。
这里我只是取了所有单独的值,并从中得到一个字符串。
请给我指点一下。
您可以发送一个表单元素数组,只需将标记更改为:
<input type="checkbox" name="prices[]" value="Less than 10,000, ">
<input type="checkbox" name="prices[]" value="10,001 to 15000, ">
// etc...
然后,PHP端:
$prices = $_POST['prices']; // this is now an array, work on it
首先你使用的是已弃用的mysql库。应该切换到mysql或更好的PDO。然后这里有个错别字:
query="insert into mobile_frm values('".$z6."')";
应该是:
$query="insert into mobile_frm values('$z6')";
假设您的表只有一列。然后,如前所述,每个复选框应该具有相同的名称prices[](即数组表示法)。在价格数组中,您将只有选中的复选框。旁注:您正在使用$_REQUEST
数组,但您应该使用$_POST or $_GET
取决于您在表单中定义的方法。我还假设您需要使用$_SESSION
来存储发布的值(在其他地方使用它们)。在任何其他情况下,应该修改代码,不要使用$_SESSION
只是将存储的值分配给变量。
<div class="panel-body">
<label class="checkbox-inline"><input type="checkbox" name="price[]" value="Less than 10,000, "> Less than 10,000</label>
<label class="checkbox-inline"><input type="checkbox" name="price[]" value="10,001 to 15000, "> 10,001 - 15000</label>
<label class="checkbox-inline"><input type="checkbox" name="price[]" value="15,001 to 25000, "> 15,001 - 25000 </label>
<label class="checkbox-inline"><input type="checkbox" name="price[]" value="25,001 to 35000, "> 25,001 - 35000 </label>
<label class="checkbox-inline"><input type="checkbox" name="price[]" value="35,001 and more, "> 35,001 and more </label>
</div>
<?php
$price = $_REQUEST['price'];
//for single cell value separte by ,
$p = implode(',',$price);
// for each value
for($i = 0;count($price)<$i;$i++){
// put insert query and value of selected price
$p = $price[$i];
}
?>
在复选框名称后使用方括号([])。
<label class="checkbox-inline"><input type="checkbox" name="price1[]" value="Less than 10,000, "> Less than 10,000</label>
在PHP中可以使用
$price1 = $_POST['price1'];
下面的代码展示了如何在PHP中使用多个复选框字段。
首先将复选框名称改为price[]。
<div class="panel-body">
<label class="checkbox-inline"><input type="checkbox" name="price[]" value="Less than 10,000, "> Less than 10,000</label>
<label class="checkbox-inline"><input type="checkbox" name="price[]" value="10,001 to 15000, "> 10,001 - 15000</label>
<label class="checkbox-inline"><input type="checkbox" name="price[]" value="15,001 to 25000, "> 15,001 - 25000 </label>
<label class="checkbox-inline"><input type="checkbox" name="price[]" value="25,001 to 35000, "> 25,001 - 35000 </label>
<label class="checkbox-inline"><input type="checkbox" name="price[]" value="35,001 and more, "> 35,001 and more </label>
</div>
如何在PHP中获取值?
Ans:你可以直接取value: $prices = $_POST['prices'];
在这种情况下,你可以得到你的值作为一个数组。如果需要分隔符,请使用$price = implode(', ', $_POST['price']);
。