douzhongju8780 2013-03-02 15:11
浏览 11
已采纳

在表AJAX,PHP中每行显示独立的多个ajax查询结果

Objective:
To show a new, unique set of options in a drop-down box, for each row in the table, by using AJAX and PHP.

Details:
I have a table written in PHP, where, I create the rows of the table in for loop:

Code:

<table>
foreach ($mylist as $i)
{
<tr>
  <td>
       echo '<select name="product[]" onchange="showItems(this.value,'.$i.')" >';
       echo "<option value='APPLE'>APPLE</option>";
       echo "<option value='ORANGE'>ORANGE</option>";
       echo "<option value='PLUM'>PLUM</option>";
       echo '</select>';
  </td>
  <td>
       echo '<select name="sales[]" id="txtHint">';
       echo '</select>';
  </td>
</tr>
}
</table>

This creates a table that looks like this:

No.   Product                sales mode
---   -------                -------
 1    (Drop-down list here)  (drop-down list here)
 2    (Drop-down list here)  (drop-down list here)

There are N no. of table rows.

When user selects a particular option, say, "APPLE" from the drop-down list, in the Product column, for row 1, then the corresponding sales mode column for the same row 1 is updated with options using AJAX, relevant to the APPLE product selected in the first column.

That is, Based on option in Product column, the options in sales mode column will change.(And not vice-versa).

The problem I am facing is, whenever, I select a new different product,in the second row of the table, the first row's sales mode column also changes, based on the second row's Product selection.

In the first step:

No.  Product   sales mode
---  ----      ----------
 1   Apple     EXPRESS
 2   (Select)  (Select)

When I do this next[Choosing Orange option in the product column]:

No.  Product   sales mode
---  ----      ----------
 1   Apple     EXPRESS//This is the default option for Apple
 2   Orange    (select)

It becomes this:

No.  Product   sales mode
---  ----      ----------
 1   Apple     REGULAR//This is the default option for Orange 
 2   Orange    (select)

AJAX code:

function showItems(str,val)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str+"&val="+val,true);
xmlhttp.send();
}

How do I achieve this? [Fairly new to AJAX]

Appreciate your help, suggestions. Let me know, if you need further information.

  • 写回答

1条回答 默认 最新

  • dragam0217 2013-03-02 15:29
    关注

    The problem is that in your html you're setting the id of all of the sales mode dropdowns to txtHint. Aside from being invalid html, it's causing your ajax call back to always update the first element it finds with that id, which happens to be the one for Apple in your example. What you want to do is give each of them a unique id, and use that in your callback.

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

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致