【问题标题】:PHP select option values when use with echo与 echo 一起使用时 PHP 选择选项值
【发布时间】:2017-12-18 17:26:47
【问题描述】:

我正在尝试在表格中显示详细信息并在下拉列表中显示值列表。值显示在表中,但下拉列表未显示值,尽管值存在,当我将其保存在会话中并打印时,它出现在顶部。我也有使用选择名称的问题。当我点击按钮并想要读取选项的帖子值时,它给了我错误,如图所示

代码如下:

for($i=0;$i<count($dr_ide);$i=$i+5)
    {
        echo "<tr>";
        echo "<td> Dr. " . $dr_namee[$i] . "</td>";
        echo "<td>" . $sub_namee[$i] . "</td>";
        echo "<td>" . $sub_namee[$i+1] . "</td>";
        echo "<td>" . $sub_namee[$i+2] . "</td>";
        echo "<td>" . $sub_namee[$i+3] . "</td>";
        echo "<td>" . $sub_namee[$i+4] . "</td>";
        echo "<td>  <select  name='perr' >
                    <option name='perr' selected=selected>Choose one</option>";
                    foreach($Names as $name) {
                        echo"<option name='perr' value=";
                             $name ;
                            echo "> ";
                            $_SESSION["tt"]=$name;
                            $name;
                            echo"</option>";


                    } 
                    echo "</select></td>";
        //echo "<td>" . $dayse[$i] . "</td>";
        //echo "<td>" . $timing[$i] . "</td>";
        echo "</tr>";
    }
}

    echo $_SESSION["tt"];

    if(isset($_POST['confirm'])){ 
        echo $_POST['perr'];
    }

这是我的完整页面代码。

<html>
<head>
<title> Expert System </title>

<link rel="stylesheet" type="text/css" href="style.css">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:700,600' rel='stylesheet' type='text/css'>
 </head>
<body>
<div >
  <table id="cells2" border="0" cellpadding="15" cellspacing="5"  font size="6">

<tr>
<th><img id="header2"  src="images/Kuwait_University_Logo.jpg" > </th>
<th>KUWAIT UNIVERSITY</th>
</tr>
</table>
<div class='back1'>

<table border='1' align='center' id='customers' >
<?php
 require "init.php";
session_start();

    global $con,$users,$dr_id,$sub_id,$dr_name,$sub_name,$days,$fav,$timing;
    global $dr_ide,$dr_namee,$sub_namee,$dayse,$fave,$timinge;


        $query= "SELECT * FROM subjects_current where subjects_current.sub_ID NOT IN (SELECT test.subject_id from test)";
    $result=mysqli_query($con,$query);
    if ( $result->num_rows == 0 ) // User doesn't exist
        echo "Subjects doesn't exist!";
    else { 
    while($row = mysqli_fetch_array($result))
        {

            $IDs[]=$row['sub_ID'];
            $Names[]=$row['Name'];
            //echo $row['Name'];
        }
    }


    $query= "SELECT * FROM test order by dr_id";
    $result=mysqli_query($con,$query);
    if ( $result->num_rows == 0 ) // User doesn't exist
        echo "Subjects doesn't exist!";
    else { echo "
        <tr>
            <th>Professor Name</th>
            <th>First Choice</th>
            <th>Second Choice</th>
            <th>Third Choice</th>
            <th>Fourth Choice</th>
            <th>Fifth Choice</th>
            <th>Update Subject</th>

        </tr>";

       $r=0;
       $f=0;
      while($row = mysqli_fetch_array($result))
        {
            $dr_ide[$f]=$row['dr_id'];
            $dr_namee[$f]=$row['dr_name'];
            $sub_namee[$f]=$row['sub_name'];
            $dayse[$f]=$row['days'];
            $timinge[$f]=$row['timing'];
            $fave[$f]=$row['fav'];

            //echo "<tr>";
            //echo "<td> Dr. " . $dr_namee[$f] . "</td>";
            //echo "<td>" . $sub_namee[$f] . "</td>";
            //echo "<td>" . $fave[$f] . "</td>";
            //echo "<td>" . $dayse[$f] . "</td>";
            //echo "</tr>";
            //$r++;
            $f++;
        }
        //for($i=0;$i<count($Names);$i=$i+5)
        //{
        for($i=0;$i<count($dr_ide);$i=$i+5)
        {
            echo "<tr>";
            echo "<td> Dr. " . $dr_namee[$i] . "</td>";
            echo "<td>" . $sub_namee[$i] . "</td>";
            echo "<td>" . $sub_namee[$i+1] . "</td>";
            echo "<td>" . $sub_namee[$i+2] . "</td>";
            echo "<td>" . $sub_namee[$i+3] . "</td>";
            echo "<td>" . $sub_namee[$i+4] . "</td>";

            echo "<td>  <select  name='perr' >
                        <option name='perr' selected=selected>Choose one</option>";
                        foreach($Names as $name) {
                             echo"<option name='perr' value='$name'>$name</option>";

                        }                            
                        echo "</select></td>";
            //echo "<td>" . $dayse[$i] . "</td>";
            //echo "<td>" . $timing[$i] . "</td>";
            echo "</tr>";
        }
    }

    echo $_SESSION["tt"];

    if(isset($_POST['confirm'])){ 
        echo $_POST['perr'];
    }

?>
</table>
<form method='post' action='edit_subjects.php'>
            <input ID="btn2" name="confirm"  type="submit" value="Home">

            </form>
</div>
</body>
</html>

【问题讨论】:

  • 注意:mysqli 的面向对象接口明显不那么冗长,使代码更易于阅读和审核,并且不容易与过时的mysql_query 接口混淆。在您对程序风格投入过多之前,值得转换一下。示例:$db = new mysqli(…)$db-&gt;prepare("…") 过程化接口是 PHP 4 时代引入 mysqli API 时的产物,不应在新代码中使用。
  • 你是对的,但我只是以这种方式构建了我的所有项目,我有两天的交付时间,所以我无法以其他方式重建它,所以如果你能帮助我完成这项工作,我会的开心

标签: php mysql mysqli


【解决方案1】:

&lt;options&gt; 中删除name='perr' 并更改foreach(),如下所示:-

foreach($Names as $name) {
    echo"<option value='$name'>$name</option>";
} 

像这样修改你的代码:-

echo "<form method='post' action='edit_subjects.php'>"; //add before while
        while($row = mysqli_fetch_array($result))
        {
            $dr_ide[$f]=$row['dr_id'];
            $dr_namee[$f]=$row['dr_name'];
            $sub_namee[$f]=$row['sub_name'];
            $dayse[$f]=$row['days'];
            $timinge[$f]=$row['timing'];
            $fave[$f]=$row['fav'];

            $f++;
        }
        for($i=0;$i<count($dr_ide);$i=$i+5)
        {
            $dr_nam = $dr_namee[$i];
            echo "<tr>";
            echo "<td> Dr. " . $dr_namee[$i] . "</td>";
            echo "<td>" . $sub_namee[$i] . "</td>";
            echo "<td>" . $sub_namee[$i+1] . "</td>";
            echo "<td>" . $sub_namee[$i+2] . "</td>";
            echo "<td>" . $sub_namee[$i+3] . "</td>";
            echo "<td>" . $sub_namee[$i+4] . "</td>";

            echo "<td><select  name='perr[$dr_nam]' >
                        <option name='perr' selected=selected>Choose one</option>";
                        foreach($Names as $name) {
                             echo"<option name='perr' value='$name'>$name</option>";

                        }                            
                        echo "</select></td>";
            echo "</tr>";
        }
        echo"<input ID='btn2' name='confirm'  type='submit' value='Home'></form>"; //add just after while code ended

【讨论】:

  • 这解决了第一个问题并出现了值,但是当我想读取后值时,它给了我错误“未定义索引”
  • 你是 PH​​P 之王,感谢你的帮助,它现在可以工作了
  • @SalehRefaai 很高兴为您提供帮助:):)
【解决方案2】:
echo "<td>  <select  name='perr' >
      <option name='perr' selected='selected'> Choose one </option>";
      foreach($Names as $name) {
         echo"<option name='perr' value='". $name ."'> ". $name ." </option>";
      } 
echo "</select></td>";

【讨论】:

    【解决方案3】:

    你必须像这样呼应名字:

    foreach($Names as $name) {
        echo "<option value=";
        echo $name ;
        echo "> ";
        $_SESSION["tt"]=$name;
        echo $name;
        echo"</option>";
    } 
    

    并将表格放入表格中:

    <html>
    <head>
    <title> Expert System </title>
    
    <link rel="stylesheet" type="text/css" href="style.css">
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:700,600' rel='stylesheet' type='text/css'>
     </head>
    <body>
    <div >
      <table id="cells2" border="0" cellpadding="15" cellspacing="5"  font size="6">
    
    <tr>
    <th><img id="header2"  src="images/Kuwait_University_Logo.jpg" > </th>
    <th>KUWAIT UNIVERSITY</th>
    </tr>
    </table>
    <div class='back1'>
    <form method='post' action='edit_subjects.php'>
    <table border='1' align='center' id='customers' >
    <?php
     require "init.php";
    session_start();
    
        global $con,$users,$dr_id,$sub_id,$dr_name,$sub_name,$days,$fav,$timing;
        global $dr_ide,$dr_namee,$sub_namee,$dayse,$fave,$timinge;
    
    
            $query= "SELECT * FROM subjects_current where subjects_current.sub_ID NOT IN (SELECT test.subject_id from test)";
        $result=mysqli_query($con,$query);
        if ( $result->num_rows == 0 ) // User doesn't exist
            echo "Subjects doesn't exist!";
        else { 
        while($row = mysqli_fetch_array($result))
            {
    
                $IDs[]=$row['sub_ID'];
                $Names[]=$row['Name'];
                //echo $row['Name'];
            }
        }
    
    
        $query= "SELECT * FROM test order by dr_id";
        $result=mysqli_query($con,$query);
        if ( $result->num_rows == 0 ) // User doesn't exist
            echo "Subjects doesn't exist!";
        else { echo "
            <tr>
                <th>Professor Name</th>
                <th>First Choice</th>
                <th>Second Choice</th>
                <th>Third Choice</th>
                <th>Fourth Choice</th>
                <th>Fifth Choice</th>
                <th>Update Subject</th>
    
            </tr>";
    
           $r=0;
           $f=0;
          while($row = mysqli_fetch_array($result))
            {
                $dr_ide[$f]=$row['dr_id'];
                $dr_namee[$f]=$row['dr_name'];
                $sub_namee[$f]=$row['sub_name'];
                $dayse[$f]=$row['days'];
                $timinge[$f]=$row['timing'];
                $fave[$f]=$row['fav'];
    
                //echo "<tr>";
                //echo "<td> Dr. " . $dr_namee[$f] . "</td>";
                //echo "<td>" . $sub_namee[$f] . "</td>";
                //echo "<td>" . $fave[$f] . "</td>";
                //echo "<td>" . $dayse[$f] . "</td>";
                //echo "</tr>";
                //$r++;
                $f++;
            }
            //for($i=0;$i<count($Names);$i=$i+5)
            //{
            for($i=0;$i<count($dr_ide);$i=$i+5)
            {
                echo "<tr>";
                echo "<td> Dr. " . $dr_namee[$i] . "</td>";
                echo "<td>" . $sub_namee[$i] . "</td>";
                echo "<td>" . $sub_namee[$i+1] . "</td>";
                echo "<td>" . $sub_namee[$i+2] . "</td>";
                echo "<td>" . $sub_namee[$i+3] . "</td>";
                echo "<td>" . $sub_namee[$i+4] . "</td>";
    
                echo "<td>  <select  name='perr' >
                            <option  selected=selected>Choose one</option>";
                            foreach($Names as $name) {
                                 echo"<option value='$name'>$name</option>";
    
                            }                            
                            echo "</select></td>";
                //echo "<td>" . $dayse[$i] . "</td>";
                //echo "<td>" . $timing[$i] . "</td>";
                echo "</tr>";
            }
        }
    
        echo $_SESSION["tt"];
    
        if(isset($_POST['confirm'])){ 
            echo $_POST['perr'];
        }
    
    ?>
    </table>
    
                <input ID="btn2" name="confirm"  type="submit" value="Home">
    
                </form>
    </div>
    </body>
    </html>
    

    【讨论】:

    • 这解决了第一个问题并出现了值,但是当我想读取 post 值时,它给了我错误“未定义索引”
    • @SalehRefaai 你在哪里定义名为“confirm”的字段?
    • 还有其他代码,我在其中放置了带有名称确认的按钮,当我删除 echo $_POST['perr'];并输入 echo 'hi';它打印你好
    • 我添加了我的完整代码以查看它并查看提交按钮。 @克劳迪奥
    • @SalehRefaai 你必须在表格中插入表格
    猜你喜欢
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    相关资源
    最近更新 更多