如何使用带有数组的复选框

时间:2012-02-23 19:07:19

标签: php arrays checkbox

我正在尝试使用数组显示文本文件中的一些数据。

复选框显示但是,当用户输入搜索词时,只有相关数据会出现复选框,我的问题是如何显示已在其他PHP文件上检查过的数据?

<html> 
<body bgcolor="#99FF00">
<table border="1">
<FORM ACTION="available.php" METHOD="POST">
Enter maximum price <input type="text" name="maximumprice"/> 
<p><input type="submit" value="Go" name="Go"/>
</form>
<FORM action="visit.php" METHOD="Post"> 
<p><input type="Submit" value="Visit" name="visit">
</form>
<?

$mPrice = $_POST['maximumprice'];
$file1 = "properties.txt";
$filedata = fopen ($file1, "r");
$array1 = file ($file1); 

for ($counter1 = 0; $counter1 < count($array1); $counter1++) 
{
$arrLine = $array1[$counter1];

$pCode = getvalue ($arrLine, 0);
$price = getvalue ($arrLine, 1);
$picture = getvalue ($arrLine, 2);
$visit = getvalue ($arrLine, 3);



if ($price < $mPrice)
{
print "<tr>";
print "<td>";

print $pCode. "<br>";
print $price. "<br>"; 
//print $picture. "<br>";
print $visit. "<br>";

print "<form action=\"available.php\">";
print "$arrLine<input type=\"checkbox\" name=\"box[]\" value=\"$arrLine\" />";
print "</form>";
print "</td>";

print "<td>";
printf("<img src='$picture' width='200' height='150'>");
print "</td>";
print "</tr>";



}
} 

fclose ($filedata); 

function getvalue ($text, $arrNo) 
{ 
$intoarray = explode (",", $text); 
return $intoarray[$arrNo]; 
} 

?> 

</table>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

根据您的需要修改以下代码。这是显示index.phpshow.php页面中选中复选框的更简单方法。我为此示例选择了POST方法。

<强>的index.php

echo "<form method='post' action='show.php'>";
echo "<input type='checkbox' name='box[]' value='$arrLine' /> $arrLine";
echo "</form>"

<强> show.php

$options = $_POST['box'];
foreach($options as $option) {
  echo "<p>$option<p>";
}

答案 1 :(得分:1)

如果您要允许选择多个复选框,则需要将FORM标记放在for循环之外。

这应该是您的代码的工作修改:

<html> 
<body bgcolor="#99FF00">
<table border="1">
<FORM ACTION="available.php" METHOD="POST">
Enter maximum price <input type="text" name="maximumprice"/> 
<p><input type="submit" value="Go" name="Go"/>
</form>
<FORM action="visit.php" METHOD="Post"> 
<p><input type="Submit" value="Visit" name="visit">
</form>
<?

$mPrice = $_POST['maximumprice'];
$file1 = "properties.txt";
$filedata = fopen ($file1, "r");
$array1 = file ($file1); 

// Move the form outside the for loop
print "<form action=\"available.php\" method=\"POST\">";

//SHOULD I DISPLAY SELECTED VALUES?
if (isset($_POST['box'])) {
    $array1 = $_POST['box'];
    $mPrice = 10000000; // Since what I wanna see is already selected, set maxprice to ALOT.

} else { // read from file
//  $mPrice = $_POST['maximumprice'];
//  $file1 = "properties.txt";
//  $filedata = fopen ($file1, "r");
//  $array1 = file ($file1); 
}

for ($counter1 = 0; $counter1 < count($array1); $counter1++) 
{
$arrLine = $array1[$counter1];

$pCode = getvalue ($arrLine, 0);
$price = getvalue ($arrLine, 1);
$picture = getvalue ($arrLine, 2);
$visit = getvalue ($arrLine, 3);



if ($price < $mPrice)
{
print "<tr>";
print "<td>";

print $pCode. "<br>";
print $price. "<br>"; 
//print $picture. "<br>";
print $visit. "<br>";


print "$arrLine<input type=\"checkbox\" name=\"box[]\" value=\"$arrLine\" />";

print "</td>";

print "<td>";
printf("<img src='$picture' width='200' height='150'>");
print "</td>";
print "</tr>";


}
} 
// Add a view selected button
print '<input type="submit" name="selectedList" value="View selected"/>';

// Move the form outside the for loop
print "</form>";


fclose ($filedata); 

function getvalue ($text, $arrNo) 
{ 
$intoarray = explode (",", $text); 
return $intoarray[$arrNo]; 
} 

?> 

</table>
</body>
</html>