选中框的 PHP 循环返回错误
我目前正在尝试设计一个 for 循环来迭代任何“选中”复选框,然后使用“值”槽中的值来运行一些查询。不幸的是,我正在努力解决这个问题,因为复选框列表不是预先定义的,它们是来自数据库的动态,等待用户之前的选择。
该循环实际上用于呈现要检查的项目:
?>
<input type="checkbox" name="option[]" value="$listing_id">
<font size="+1" color="green"><?php echo"$list_name"; ?>:</font><br />
<?php
值中的列表 ID 是我在运行更新查询之前需要在 mysql 查询中使用的内容。 for 循环的作用是:
foreach($_POST['option'] as $option) //loop through the checkboxes
{
...
}
更新查询将在其中工作,因为它只是从其他地方复制,我只需要选中的复选框中的“Listing_ID”。
我运行此代码希望进行一些调试:
if(empty($_POST['option'])){
echo "no checkboxes checked.";
} else {
if(!isset($_POST['option'])){
echo "no set.";
}
}
它返回“未检查复选框”。
我现在已经陷入了一个灰色地带,为什么这个 for 循环不起作用(这是取自互联网上的另一个例子)。
I am currently trying to design a for
loop to iterate through any 'checked' check boxes, and from that, use the value within the 'value' slot to run some queries. I am unfortunately struggling with this as the list of checkboxes are not pre-defined, they are dynamic from the database pending the users previous selection.
The loop actually works to present the items to be checked:
?>
<input type="checkbox" name="option[]" value="$listing_id">
<font size="+1" color="green"><?php echo"$list_name"; ?>:</font><br />
<?php
The listing ID within the value is what I need to work with in a mysql query before I run an update query. The for
loop that's meant to work is:
foreach($_POST['option'] as $option) //loop through the checkboxes
{
...
}
The update query will work within this as its simply copied from somewhere else, I just need the 'Listing_ID' from the check boxes that are checked.
I ran this code to hopefully do some debugging:
if(empty($_POST['option'])){
echo "no checkboxes checked.";
} else {
if(!isset($_POST['option'])){
echo "no set.";
}
}
and it returns "no checkboxes checked."
I have now hit a grey area as to why this for loop isn't working (this was taken from another example on the internet).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果未设置
$_POST['option']
,empty($_POST['option'])
将返回 true(与!isset($ _POST['option'])
(!)) 或空数组。如果您需要调试正在发生的情况,请使用
var_dump($_POST['option']);
找出已为option
复选框提交的内容。我还建议您执行var_dump($_POST);
这样您就可以看到已提交的总体内容 - 例如,如果 post 操作不是post
您会立即注意到) 。对于 HTML 输出:这应该会为您提供所需的信息。对于每个单独的复选框,您可以执行以下操作:
empty($_POST['option'])
will return true, if either$_POST['option']
is not set (same as!isset($_POST['option'])
(!)) or an empty array.If you need to debug what's going on, use
var_dump($_POST['option']);
to find out what has been submitted for theoption
checkboxes. I also suggest you do avar_dump($_POST);
so you can see what has been submitted overall - e.g. in case the post action is notpost
you will immediatly notice). For HTML output:That should give you the information you're looking for. For each individual checkbox, you can do:
首先,你的代码似乎对我来说是有问题的。也许只是一个错字,但
应该是
此外,在数组上使用空根本不好。
First of all your code seems to be bugged to me. Maybe is just a typo but
should be
Moreover using empty over an array is not good at all.
尝试在循环中回显
$option
来查看该值是什么,然后您就可以看到那里是否有东西。还要确保表单的方法设置为
POST
或者它的操作指向正确的位置。您的输入也有错误:我假设您的意思是:
更新:
错误最终不在代码已发布。在 if 语句中发现错误,在上面发布的代码中总是返回 false。
Try echoing out the
$option
in the loop to see what the value is and there you can see if there is something there.Also make sure your form's method is set to
POST
or that it's action is pointed to the correct place. You also have an error in your input:<input type="checkbox" name="option[]" value="$listing_id">
I assume you meant:
<input type="checkbox" name="option[]" value="<?php echo $listing_id;?>">
UPDATE:
The error ended up not being in the code posted. Error was discovered in an if statement that always returned false that in-cased the code posted above.