我正在尝试创建一个php web应用程序,该应用程序可以选择使用复选框表单,然后将复选框提交到其他文件。但如果使用查询值定义复选框的值,我该怎么做呢? 请帮助我,我找不到解决方案:/
<form action="addPratosEnc4.php" method="POST">
*Pratos a Adicionar:
<select name="nomeA">
<?php
try
{
$host = "xxxx";
$user ="xxx";
$password = "xxx";
$dbname = $user;
$db = new PDO("mysql:host=$host;dbname=$dbname", $user, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT nomeA FROM Disponivel;";
$result = $db->query($sql);
foreach($result as $row)
{
$nomeA = $row['nomeA'];
/*should be here?*/
}
$db = null;
}
catch (PDOException $e)
{
echo("<p>ERROR: {$e->getMessage()}</p>");
}
?>
</select>
<br>
<br>
<input type="submit" value="Disponibilizar">
</form>
答案 0 :(得分:1)
要渲染选项列表,请执行以下操作:
.......
$nomeA = $row['nomeA'];
/*should be here?*/
?> <option value="<?php echo $nomeA; ?>"><?php echo $nomeA; ?></option><?php
}
$db = null;
}
........
这应该在select中创建一个填充了查询结果的选项列表。我假设你的选项列表不是复选框 - 选择框为您提供每个选项,您只能选择1.您也可以使用单选按钮执行此操作。另一方面,您可能打算使用Checkbox,用户可以选择多个框 在这种情况下,你不应该在select中包装任何东西,比如
<input type="checkbox" name="group" value="<?php echo $nomeA; ?>">I have a <?php echo $nomeA; ?><br>
在for循环中。
祝你好运 Narimm答案 1 :(得分:1)
好的,让我们直截了当地
<?php
try {
$host = "xxxx";
$user ="xxx";
$password = "xxx";
$dbname = $user;
$db = new PDO("mysql:host=$host;dbname=$dbname",$user,$password);
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$q = "SELECT nomeA FROM Disponivel";
$options = array();
$count = 0;
foreach($db->query($q) as $row) {
$count++;
$options[$row['nomeA']] = '<input type="checkbox" name="dispo[]" value="'.$row['nomeA'].'" />';
}
echo '<p>Rows processed: '.$count.'</p>';
$db = null;
} catch (PDOException $e) {
echo '<p>ERROR: '.$e->getMessage().'</p>';
}
?>
<form action="addPratosEnc4.php" method="POST">
*Pratos a Adicionar:
<?php
if (!$options) echo ' agora no hay pratos';
else foreach ($options as $name => $input) echo '<label>'.$input.' '.$name.'</label><br />';
?>
<br />
<br />
<button type="submit">Disponibilizar</button>
</form>
尝试此操作,如果您有任何问题,请提出任何问题,如果有任何解决方案适合您,请将其标记为答案
答案 2 :(得分:0)
echo'<input type ="checkbox" name = "'.$row['nomeA'].'"value = "'.$row['nomeA'].'" />'.$row['nomeA'].'<br />;
而不是:$ noumeA = $ row ['noumeA']; 删除选择标记,这应该有用。
答案 3 :(得分:0)
只是为了完成我的任务,我将发布需要获取所选值的内容:
<?php
session_start();
$email = $_SESSION['email'];
$nEnc = $_SESSION['nEnc'];
$options = $_REQUEST['dispo'];
echo("<p>$email</p>");
echo("<p>$nEnc</p>");
try
{
$host = "xxxx";
$user ="xxxx";
$password = "xxxx";
$dbname = $user;
$db = new PDO("mysql:host=$host;dbname=$dbname", $user, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
foreach($options as $name){
$nomeA = $name;
$sql = "INSERT INTO RegistoEnc VALUES ('$email',$nEnc,'$nomeA');";
echo("<p>$sql</p>");
$db->query($sql);
};
$db = null;
}
catch (PDOException $e)
{
echo("<p>ERROR: {$e->getMessage()}</p>");
}
echo "<br>Encomenda Criada!!<br>";
?>