douyun3631
2017-04-06 13:58
浏览 54
已采纳

为什么我的php脚本没有动态填充html表单上的输入字段?

I am building an html form that will need to dynamically fill in the first input field with the "facility" column values in my "doctors" table. The facility column contains the names of our 7 offices. However, when I run the code below, my input field is blank and I have verified there is data in my "doctors" table. After this is working, I need to be able to dynamically fill in the second input field (which I haven't coded for in the code below because I'm stuck with the issue of first input) with the "provider" column values, also from my "doctors" table. The "provider" column contains all the provider names in our practice. However, the providers should be filtered, so that only the providers at the facility from the first input field is showing.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <?php
     $link = mysqli_connect("localhost","USERNAME","PASSWORD");
     mysqli_select_db($link,"DB");
  ?>
  <head>
    <title> Untitled Doc</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
  </head>
  <body>
    <form name "form1" action="" method="post">
      <select>
        <?php
        $res=mysqli_query($link,"select facility from doctors");
        while($row=mysqli_fetch_array($res)){?>
          <option> <?php echo $row ["facility"]; ?></option>
        <?php }?>
      </select>
    </form>
  </body>
</html>

图片转代码服务由CSDN问答提供 功能建议

我正在构建一个html表单,需要使用“facility”列值动态填充第一个输入字段 在我的“医生”表中。 设施栏包含我们7个办事处的名称。 但是,当我运行下面的代码时,我的输入字段是空白的,我已经验证了我的“医生”表中有数据。 在此工作之后,我需要能够使用“provider”列值动态填写第二个输入字段(我在下面的代码中没有编码,因为我遇到了第一个输入的问题), 也来自我的“医生”表。 “提供者”列包含我们实践中的所有提供者名称。 但是,应该过滤提供者,以便只显示第一个输入字段中设施的提供者。

 &lt;!DOCTYPE html PUBLIC“ -  // W3C // DTD HTML 4.01 Transitional // EN”“http://www.w3.org/TR/html4/  loose.dtd“&gt; 
&lt; html&gt; 
&lt;?php 
 $ link = mysqli_connect(”localhost“,”USERNAME“,”PASSWORD“); 
 mysqli_select_db($ link,”DB“); \  n?&gt; 
&lt; head&gt; 
&lt; title&gt;  Untitled Doc&lt; / title&gt; 
&lt; meta http-equiv =“content-type”content =“text / html; charset = iso-8859-1”&gt; 
&lt; / head&gt; 
&lt; body&gt;  
&lt; form name“form1”action =“”method =“post”&gt; 
&lt; select&gt; 
&lt;?php 
 $ res = mysqli_query($ link,“select facility from doctors”);  
 while($ row = mysqli_fetch_array($ res)){?&gt; 
&lt; option&gt;  &lt;?php echo $ row [“facility”];  ?&gt;&lt; / option&gt; 
&lt;?php}?&gt; 
&lt; / select&gt; 
&lt; / form&gt; 
&lt; / body&gt; 
&lt; / html&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

  • dtuzjzs3853 2017-04-06 14:49
    最佳回答

    It is important that you check if the connection have been established, as I have copied your code as it is and use it on my side and was working fine, therefore made me suspect that the problem might be connection related. also check how sensitive your server is maybe your server see this as an error : $row ["facility"] that space might be the problem as well, but it didn't on my side.

    Check your server error log and also enable error reporting at the top of your page add

    <?php 
    ini_set('display_errors', 1); 
    error_reporting(E_ALL);?>
    

    That will enable error reporting, but use that on local server only

    Then on live site send them to error log

    error_reporting(E_ALL);
    ini_set('display_errors',0);
    ini_set('log_errors',1);
    

    also to get the mysqli errors, before your connection

    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

    Important to check if your query does indeed return results before trying to display them.

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <?php
         $link = mysqli_connect("localhost", "root", "", "DB");
            if (mysqli_connect_errno()) {
                printf("Connect failed: %s
    ", mysqli_connect_error());
                exit();
            }
        ?>
        <head>
        <title> Untitled Doc</title>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
        </head>
        <body>
        <form name ="form1" action="" method="post">
        <?php
           $query = "SELECT facility FROM doctors";
    
            if ($res = mysqli_query($link, $query)) {
                echo "<select name=\"myselect\">";
                while ($row = mysqli_fetch_assoc($res)) {
            ?>
    
            <option value="<?php echo $row['facility'];?>"><?php echo $row['facility'];?></option>
               <?php
                }
    
                echo "</select>";
                mysqli_free_result($res);
            } else {
    
                printf("Error : %s
    ", mysqli_error($link));
            }
    
            /* close connection */
            mysqli_close($link);
            ?>
    
    
    
        </form>
        </body>
        </html>
    

    NB: For your own benefit, if you haven't used prepared statements, would suggest that you learn them as well, though they are not needed in this case

    评论
    解决 无用
    打赏 举报
查看更多回答(3条)

相关推荐 更多相似问题