如何将复选框与数组一起使用


How to use check boxes with an array

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

复选框会出现,但当用户输入搜索词时,只有相关数据才会出现复选框,我的问题是如何显示在不同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>

根据需要修改以下代码。这是在show.php中显示index.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>";
}

如果要允许选择多个复选框,则需要将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>