douxianji6104 2019-01-31 12:20
浏览 86
已采纳

重命名输入使用while循环创建的表单中的字段ID

I am trying to POST a form that is generated through a while loop that iterates on some elements of a table and they can vary in number according to the different companies. Because the IDs of the input/edit fields are created by a while-loop they all have the same id and this is creating a problem when I try to submit the form, is there a way to change automatically the IDs? I tried to use Javascript with no success. Is there a way to name the IDs according to the loop cycle?

Also is there a better way to create a number of input/edit fields according to the number of items to be edited in an SQL table?

echo '<form id="myForm" action="edit.php" method="POST">';
echo '<div class="leftcontact">';

  $i=0;
  while($query_data = mysqli_fetch_row($result)) {
   $_SESSION["List"]=$query_data[2];
   $_SESSION["List_company_id"]=$query_data[0];

   $result2 = mysqli_query($connection, "SELECT * FROM `calling_lists` WHERE `calling_list_id`='".$_SESSION["List"]."'"); 
   $query_data2 = mysqli_fetch_row($result2);
   $_SESSION["list_name"]=$query_data2[1];

   echo '<div class="form-group">';
   echo '<p>List number</p>';
   echo '<select class="dropdown" name="ID">';
   echo "<option selected = 'selected' value=\"".$_SESSION["List_company_id"]."\">".$_SESSION["list_name"]."</option>";

        $query1="SELECT * FROM `calling_lists`";
        $result1=mysqli_query($connection,$query1) or die ("Query to get data from list table failed: ".mysql_error());

        while ($row1=mysqli_fetch_array($result1)) {
           $list_name=$row1["calling_list_name"];
           $list_description=$row1["calling_list_description"];
           $list_id=$row1["calling_list_id"];
   echo "<option value=\"$list_id\">"
          . $list_name . 
         "</option>";
        };
   $i=$i+1;
 };
  • 写回答

2条回答 默认 最新

  • dongru2019 2019-02-01 10:04
    关注

    it was late and I could not figure out the answer, but with a fresh look I found it. In the code there are actually 2 problems, one is that the operator is missing and there was a problem with " ' ".

    The working code is here below, in case anybody needs it. It creates a number of dropdown menu, as many as my $results rows.

    Hope it helps anybody.

    $i=0;
    
      while($query_data = mysqli_fetch_row($result)) {
    
    $_SESSION["List_company_id"]=$query_data[3];
    $_SESSION["List"]=$query_data[1];
    $_SESSION["list_name"]=$query_data2[2];
    
    echo '<div class="form-group">';
    echo "<p>List number $i</p>";
    echo '<span class="icon-case hidden-xs"><i class="fa fa-home"></i></span>';
    echo "<select class='dropdown' name=\"".$_SESSION['List_company_id']."\">";
    echo "<option selected = 'selected'  value=\"".$_SESSION["List_company_id"]."\">".$query_data[2]."</option>";
    
      $query1="SELECT * FROM `calling_lists`";
      $result1=mysqli_query($connection,$query1) or die ("Query to get data from list table failed: ".mysql_error());
    
            while ($row1=mysqli_fetch_array($result1)) {
    
              $list_name=$row1["calling_list_name"];
              $list_description=$row1["calling_list_description"];
              $list_id=$row1["calling_list_id"];
              echo "<option value=\"$list_id\">
              $list_name
              </option>";
        };
    
    echo "</select>";
    echo "</div>"; 
    $i=$i+1;
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)