Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.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,我有个问题,我需要在选中他的复选框时获取输入(文本)的值 <form method=GET> <input type =checkbox name = checkbox[]> <input type = 'text' name=? > <input type =checkbox name = checkbox[]> <input type ='text' name=? > <input type =checkbox name

我有个问题,我需要在选中他的复选框时获取输入(文本)的值

<form method=GET>

<input type =checkbox name = checkbox[]>
<input type = 'text' name=? >

<input type =checkbox name = checkbox[]>
<input type ='text' name=? >

<input type =checkbox name = checkbox[]>
<input type = 'text' name=? >

<input type = 'submit' name='Submit' >

</form> 

感谢您的帮助。

复选框和单选类型字段未选中或选中时,不会被提交

<form method=GET>

<input type =checkbox name = checkbox[]>
<input type = 'text' name=? >

<input type =checkbox name = checkbox[]>
<input type ='text' name=? >

<input type =checkbox name = checkbox[]>
<input type = 'text' name=? >

<input type = 'submit' name='Submit' >

</form> 
因此,您需要检查get/request对象中是否存在复选框名称


复选框的值仅返回布尔值(即true或false)。 你应该使用

if(isset($_GET["checkbox"]))
而不是

if(checkbox == checked)

当您选中
复选框时
可以获得下一个输入框的值

$("input:checkbox").change(function(){
    if(this.checked){
       alert($(this).next("input").val());
    }
});

我猜您的意思是在提交到PHP脚本之后。
我只是把它放在一起,还没有测试过,但看起来相当直截了当。

它也可以使用
,但只是有点担心文本和复选框与数组的同步

HTML 总结: 提交的复选框值存储在
$chk['c']

提交的文本分别位于
$txt['t'][0]、$txt['t'][2]、$txt['t'][3]
中。

因此,文本可以通过
$txt['t'][$chk['c']


循环和回显在0.000054秒内执行。54微秒,还不错。

谢谢你的提问,但我的问题不是要获取复选框值,或者当它被选中时,我在那里使用foreach循环。我想用phpB获取复选框的下一个输入文本值,但我从数据库中获取复选框值?我可以't使用n1,n2将文本值放入$text。如果选中复选框“c1”,则$text将文本值输入为“t1”。我将发布测试结果。这就是您要查找的吗?
$("input:checkbox").change(function(){
    if(this.checked){
       alert($(this).next("input").val());
    }
});
<form action="./chkbx.php" method=GET>
<input type=checkbox name="c1" value="1"/>
<input type="text" name="t1" />
<input type=checkbox name="c2" value="2"/>
<input type="text" name="t2" />
<input type=checkbox name="c3" value="3"/>
<input type="text" name="t3" >
<input type="submit" name='Submit' />
</form>
foreach ($_GET as $key => $val){
  $chk[substr($key,0,1)] = intval(substr($key,1,1));
  $txt[substr($key,0,1)][intval(substr($key,1,1))] = $val;
}
echo '<h2>Text=' .  $txt['t'][$chk['c']] . '<h2>'; 
<?php
echo <<<EOT
<!DOCTYPE html>
<html lang="en"><head><title>Menu Test</title><meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style type="text/css">
</style></head><body>

<form action="./chkbx.php" method=GET>
<input type=checkbox name="c1" value="1"/>
<input type="text" name="t1" />
<input type=checkbox name="c2" value="2"/>
<input type="text" name="t2" />
<input type=checkbox name="c3" value="3"/>
<input type="text" name="t3" >
<input type="submit" name='Submit' />
</form>
EOT;
foreach ($_GET as $key => $val){
  $chk[substr($key,0,1)] = intval(substr($key,1,1));
  $txt[substr($key,0,1)][intval(substr($key,1,1))] = $val;
}
echo '<h2>' .  $txt['t'][$chk['c']] . '<h2>';
echo '<pre>';
var_export($chk);
echo"-------------------\n";
var_export($txt);
echo '</pre></body></html>';
$chk (
  't' => 3,
  'c' => 2,
  'S' => 0,
)-------------------
$txt (
  't' => 
  array (
    1 => 'Text One',
    2 => 'Text Two',
    3 => 'Text Three',
  ),
  'c' => 
  array (
    2 => '2',
  ),
  'S' => 
  array (
    0 => 'Submit',
  ),
)