douliaodan2738 2019-02-01 10:43
浏览 117
已采纳

如何找到我正在改变的输入的id,以便在Ajax中发布

I have created an array of DropDown menus on a while-loop and I would like to post the value on change of the menu. The problem is that since I have created the menus with a loop and the id $_SESSION['List_company_id'] is being overwritten so I cannot get the id of the menu to post.

$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;
};

echo '</form>';
echo "</div>"; 
?>

The Ajax I would like to use looks like:

<script type="text/javascript">
$(document).ready(function(){
    $("#.$_SESSION['List_company_id'].").change(function(){ 
        var company = $(this).val();
        var dataString = "list="+list; 
        if($_SESSION['List_company_id']){
            $.ajax({
                type:'POST',
                url:"get-data1.php",
                data:dataString,
                success:function(html){
                $('#contact').html(html);

            }
        }); 
    }
 });
});

Finally my get-data1.php looks like:

<?php

  /* Connect to MySQL and select the database. */
  $connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);

  if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();

  $database = mysqli_select_db($connection, DB_DATABASE);
  $c=$_POST['$_SESSION['List_company_id'];
  $query="UPDATE `calling_list_company` SET `calling_list_id`='$c' WHERE `calling_list_company_id`='".$_SESSION["company_id"]."' ;";
  $result=mysqli_query($connection,$query) or die ("Query to get data from contact_company table failed: ".mysqli_connect_error());
?>
  • 写回答

1条回答 默认 最新

  • douhuanqiao5290 2019-02-05 13:22
    关注

    I have found the answer after a lot of trial and error, "using this.id" in case anybody needs it the working code is here below:

    <script type="text/javascript">
    $(document).ready(function(){
    $(".dropdown").change(function(){ 
      var id=(this.id);
      var value=(this.value);
    
        if(value){
            $.ajax({
                type:'POST',
                url:"get_dropdown.php",
                data:{"list_company_id":id,"calling_list_id": value},
                success:function(html){
                    $('#risu').html(html);
    
                  }
              }); 
          }
      });
    });
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存