0

I am trying to display some data from text file with an array.

The check boxes show up but, when a user enters a search term only the relevant data will come up with check boxes, my question is how do I display the data that has been checked on a different PHP file?

<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>
1

2 Answers 2

2

Modify the below code to your needs. This is the simpler way to show the choosen checkboxes from index.php page in show.php. I choose a POST method for this example.

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>";
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you're gonna allow for multiple checkboxes to be selected, you need to put your FORM tag outside the for loop.

This should be a working modification for your code:

<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>

2 Comments

please note that I open the properties.txt file even though I only use the posted data to present the table. You wouldn't do that in a production environment.
I actually wanted that to be displayed on another .php file but you did point out my mistake, having the form inside the for loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.