Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我可以为选中和未选中状态的复选框赋值吗?_Php_Checkbox - Fatal编程技术网

Php 我可以为选中和未选中状态的复选框赋值吗?

Php 我可以为选中和未选中状态的复选框赋值吗?,php,checkbox,Php,Checkbox,我在表单中有一个复选框,我想为选中和未选中状态分配值。 这些值保存在MySQL数据库中。这是我用来检索值的php <?php // Value = 10.00 // This should be the checked value (initial state) function getDel1() { global $con; $get_del1 = "select * from delivery"; $run_del1 = mysqli_query($con, $get_del1);

我在表单中有一个复选框,我想为选中和未选中状态分配值。 这些值保存在MySQL数据库中。这是我用来检索值的php

<?php
// Value = 10.00
// This should be the checked value (initial state)
function getDel1()
{
global $con;
$get_del1 = "select * from delivery";
$run_del1 = mysqli_query($con, $get_del1);
while($row_del1=mysqli_fetch_array($run_del1)){
$leeds = $row_del1['delivery_leeds'];
echo "$leeds";
}
}
?>

<?php
// Value = 20.00
// This should be the unchecked value (user interaction)
function getDel2()
{
global $con;
$get_del2 = "select * from delivery";
$run_del2 = mysqli_query($con, $get_del2);
while($row_del2=mysqli_fetch_array($run_del2)){
$leeds = $row_del2['delivery_other'];
echo "$other";
}
}
?>


我可以通过调用与表单复选框相邻的div中的任一函数来独立显示这些值,但我不知道如何将这些值分配给表单复选框,然后根据用户交互自动更新div。有什么建议吗?

三元运算符可以同时处理赋值和未赋值的默认值

例如:

$checkbox = isset($_POST['checkbox']) ? $_POST['checkbox'] : "Default value if unchecked";
这相当于做:(您也可以使用)

您还可以删除默认文本,并在三元文本中执行
,使其为空

参考:

脚注:

确保该复选框包含name属性

例如,
name=“checkbox”

您的表单有一个POST方法。相应地使用适当的方法

  • 因此,在您的情况下,如您留下的代码注释所示,将数组中的
    复选框
    更改为
    交付
您的复选框值也可以使用以下内容作为示例:

<input type="checkbox" name="deliver" value="<?php echo isset($_POST['deliver']) ? $_POST['deliver'] : "Default value if unchecked"; ?>" />

使用三元运算符或我不确定是否安静地跟随,如果复选框未“选中”,则不会发送该复选框?-如果我做错了什么,那就照弗雷德说的做弗雷德,你是说如果陈述?我可以这样做,但它仍然留下了差距,因为我不知道如何针对复选框的两个状态。你能提供一个html示例吗?另外,你标记为javascript;相关性是什么?您也没有为您的复选框发布相关代码,这使得为您提供解决方案变得更加困难。无需使用
if
语句,三元运算符一次就完成了。好的,我已经搞乱了这一点,感谢您的建议,我得到了部分修复;但是检查和未检查的状态没有受到影响,它只是在提交后更新,而不是在运行中。@Steven不客气Steven,很高兴能提供帮助,啦啦队员,我过早地按了return@史蒂文:“在飞”将是一个JS/Ajax的问题,我将无法帮助你。我对JS不是很在行,你可以用php来定位复选框,还是我需要在表单输入区为这个复选框使用onChange事件并使用javascript?
<input type="checkbox" name="deliver" value="<?php echo isset($_POST['deliver']) ? $_POST['deliver'] : "Default value if unchecked"; ?>" />