简体   繁体   English

与echo一起使用时的PHP选择选项值

[英]PHP select option values when use with echo

I am trying to show details in a table and show list of values in drop down list. 我试图在表中显示详细信息,并在下拉列表中显示值列表。 the values are shown in table but drop down list is not showing the values although the values exists and when i save it in session and print it, it appears at the top . 值显示在表中,但下拉列表未显示值,尽管这些值存在,并且当我将其保存在会话中并打印时,它显示在顶部。 Also I have problem in using name of select. 我也有使用选择名称的问题。 when i click on button and wants to read the post value of the option, it gives me error as shown in figure 当我单击按钮并想读取该选项的后值时,它给我错误,如图所示

在此处输入图片说明

here is the code: 这是代码:

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'];
    }

Here is my full page code. 这是我的完整页面代码。

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

Remove name='perr' from <options> and change foreach() like below:- <options>删除name='perr'并更改如下所示的foreach()

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

Do change in your code like this:- 像这样更改代码:-

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

You have to echo the names like this: 您必须回显这样的名称:

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

And put the table inside the form: 并将表格放在表格中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM