I have a php page which is reading a text file and printing the file line by line. and also I have given 'yes' 'no' option with every line.Now I want to print those lines in the 2nd page which is selected as yes.If user select yes then I want to print those line to next page.I have the below code.Can any one check this code?
<?php
$i=0;
$lines = file("text.txt");
foreach($lines as $line) {
$select_val=$line.'-'.$i;
print " <br> ";
print " <br> ";
echo($line);
echo '<select name=".$select_val.">';
echo '<option value="Yes">Yes</option>';
echo '<option value="No">No</option>';
echo '</select>';
print " <br> ";
print " <br> ";
$i++;
}
?>
<input type="hidden" name="ival" value="$i" /> <?
print "<input type=submit value=Submit><input type=reset>";
?>
2nd page:
<?php
$i=0;
$lines = file("text.txt");
foreach($lines as $line) {
$select_val=$line.'-'.$i;
$a=$_POST[$select_val];
if ($a == 'Yes') {
echo($line);
}
$i++;
}
?>
The code would almost achieve what you're trying to do, but a 2-value selection may be better achieved with a radio selection instead of a drop-down. This makes better usability since a radio click is achieved with one action vs. a drop-down select which requires two actions.
echo '<input type="radio" name="' . $select_val . '" id="'
. $select_val . 'yes" value="Yes" /><label for="'
. $select_val . '1"> Yes</label> ';
echo '<input type="radio" name="' . $select_val . '" id="'
. $select_val . 'no" value="No" /><label for="'
. $select_val .'no"> No</label> ';
Your original code has some formatting error. If you're going to use single quotes for your echo statements, you have to concatenate the variable with the single quotations to return to PHP parsing like this (recommended method):
echo '<select name="' . $select_val . '">';
or use double quotations like this (while escaping the double quotations inside of the select tag):
echo "<select name=\"$select_val\">";
Otherwise the select tag will show up as (using your original code)
<select name=".$select_val.">
instead of
<select name="line-0">
Examine the HTML output of your PHP script and you'll see this.