doudiemei2013 2013-12-18 15:46
浏览 51

POST下拉列表值到下一个php页面

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++; 
    } 
?>
  • 写回答

1条回答 默认 最新

  • duancoubeng5909 2013-12-18 16:03
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭