douxianji6104 2019-01-31 04: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 02: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部