简体   繁体   English

单击时更新具有PHP生成的表行ID的AJAX请求变量

[英]Updating AJAX request variable with ID of PHP generated table row on click

Im probably overlooking something very obvious but at this point i surrender and need help. 我可能忽略了一些非常明显的事情,但在这一点上我投降了,需要帮助。

Here is my situation. 这是我的情况。 When my page loads, in a while loops, PHP generates a table with data from my server. 当页面加载时,在while循环中,PHP会使用来自服务器的数据生成一个表。 Each table row has a name column, phone, etc. One of the columns is an icon that when clicked allows the user to view a popup with notes on this particular lead. 每个表格行都有一个名称列,电话等。其中一列是一个图标,单击该图标可使用户查看带有此特定线索的注释的弹出窗口。 Easy stuff. 简单的东西。

The icons in each row have the same class name and their ID's are unique. 每行中的图标具有相同的类名,并且其ID是唯一的。

I have an AJAX request that should be pulling the notes data from the server and displaying it in the popup when the user clicks on the relative icon. 我有一个AJAX请求,当用户单击相关图标时,该请求应从服务器中提取注释数据并在弹出窗口中显示它。 I am trying to use $('.class').click(this).attr('id'); 我正在尝试使用$('。class')。click(this).attr('id'); to set a variable in my AJAX request with the id that needs to be submitted to my PHP script. 在我的AJAX请求中设置一个变量,其ID需要提交给我的PHP脚本。

PROBLEM: The AJAX request and return seems to be working fine but no matter which row icon I click on it only displays the data that belongs to the first row, or the first instance with the class name 'homelead' Example: I click on row 1 icon and i get a popup with row 1's notes, GREAT!. 问题:AJAX请求和返回似乎工作正常,但是无论我单击哪一行图标,它都只显示属于第一行或类名“ homelead”的第一个实例的数据。示例:我单击该行1个图标,我弹出一个带有第1行注释的弹出式窗口! I click on any other row and it only shows the 1st rows data, :(. I have confirmed that the ID's associated with each row icon are correct by doing a simple click.(this).(id) and alerting the id belonging to the row icon. All is correct, just can't seem to get the JS variable to update with the correct ID. 我单击任何其他行,它仅显示第一行数据:(.。通过单击一次(this)。(id)并警告属于该ID的ID,我已确认与每个行图​​标关联的ID是正确的行图标,这是正确的,只是似乎无法使用正确的ID更新JS变量。

Im confused why this is. 林对此感到困惑。 Any help would be appreciated. 任何帮助,将不胜感激。 Here is my current code. 这是我当前的代码。

HTML: HTML:

 <td>
       <img class="homelead" id="<?php echo $leadsfetch['unit_id'];?>"  
           onclick="ajax_unitnotes();" src="images/list-view.png">
  </td>
  <?php echo "</tr>"; } ?>

AJAX request: AJAX请求:

function ajax_unitnotes(){

    var hr = new XMLHttpRequest();
    var url = "PHP/getnotes.php";

   // this variable should update with clicked rows id before      submitting to PHP script
    var unitidnotes = $('.homelead').click(this).attr('id');     

    var vars = "unitidnotes="+unitidnotes;
    hr.open("POST", url, true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var return_data = hr.responseText;
            document.getElementById("unitnotes").innerHTML = return_data;
        }
    }
    hr.send(vars); 
    document.getElementById("unitnotes").innerHTML = "processing...";
}

As you are using an onclick trigger in the tag itself - which is usually un common when using jQuery. 当您在标记本身中使用onclick触发器时-在使用jQuery时通常不常见。 You can do this: 你可以这样做:

 <img class="homelead" id="<?php echo $leadsfetch['unit_id'];?>"  
           onclick="ajax_unitnotes($(this));" src="images/list-view.png">

And the in your function 而在您的功能

function ajax_unitnotes(e){

    var unitidnotes = e.attr('id');
}

Your current code 您当前的代码

var unitidnotes = $('.homelead').click(this).attr('id'); 

Actually does not know what it the this object you are trying to access. 其实不知道它的this对象,您试图访问。

Better yet you can use a jQuery event, remove the onclick from the img tag and have an event like this: 更好的是,您可以使用jQuery事件,从img标签中删除onclick并产生一个类似这样的事件:

$('.homelead').click(function(){
    id = $(this).attr('id');
});

You could pass the clicked object this to the function trigged by "onclick": 你可以通过点击的对象而this由“onclick”事件触发的功能:

onclick="ajax_unitnotes(this);"

That will make the DOM object you clicked on available inside the JS function. 这将使您单击的DOM对象在JS函数中可用。

You need to change the function signature accordingly: 您需要相应地更改功能签名:

function ajax_unitnotes(clickedElement){

and then you can alter this 然后你可以改变这个

var unitidnotes = $('.homelead').click(this).attr('id');  

to

var unitidnotes = clickedElement.id;

This will give you the value of $leadsfetch['unit_id'] = img id. 这将为您提供$leadsfetch['unit_id'] = img id的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM