javascript - 如何使用ajax将检索到的数据分配给不同的td

标签 javascript php ajax jquery

当用户按下按钮时,数据将被保存到数据库中。成功后我想从数据库中检索数据并将其放在正确的 td 中(在单击的 td 的同一行中)我成功检索数据但没有将检索到的数据分配给不同的td

ajax

$(document).ready(function(){

        $('.edit2').on('click', function(){


         arr = $(this).attr('class').split( " " );
        var clientid=document.getElementById("client").value;

        $.ajax({    type: "POST",
        url:"clientnetworkpricelist/updateprice.php", 
        data: "value="+$('.ajax input').val()+"&rowid="+arr[2]+"&field="+arr[1]+"&clientid="+clientid,
        success: function(data){
                            $('#CPH_GridView1_clientprice'+arr[2]).empty();
                            $('#CPH_GridView1_clientprice'+arr[2]).append(data);                                                             
                            $('.ajax').html($(this).val());
                            $('.ajax').removeClass('ajax');
                                }});

                                  }  
                             );
                        });

HTML

<td  id="CPH_GridView1_clientprice'.$rows['net_id'].'" class="edit clientprice '.$rows["net_id"].'">'.$rows["clientprice"].'</td>
 <td  id="CPH_GridView1_Status'.$rows['net_id'].'" class="edit2 status '.$rows["net_id"].' "><img  src="image/'.$rows["status"].'f.png" /></td>

在我的 updateprice.php 中,我连接到数据库并从数据库中检索值,只需像这样打印检索值

print $newclientprice;

   print $status;

我的结果

这两个值现在都显示在同一个 td 中,但我希望它在 clientprice 和 increase in status 中单独的 td 0/01 中显示

Client Price      status
 0.01increase    |
                 |
                 |

谁能帮帮我,谢谢。

最佳答案

更新价格.php

让我们改变一下:

<?php
print $newclientprice;
print $status;

我不确定您要做什么。因为您使用的是 jQuery,所以为了我们的利益,让我们使用 JSON。 重要:下面的代码依赖于没有输出在它执行之前发送,并且它会立即完成php的执行

<?php
// set type so client can understand what was sent
header('Content-Type: application/json');
// transform our 2 values into a JSON array,
// this will be transparently transformed into an array for your ajax handler
echo json_encode(array($newclientprice, $status));
// end the script, this is what the client ajax request wanted
exit;

javascript.js

我冒昧地重写了你的代码,让它看起来更清晰,至少还有一些注释

$(document).ready(function(){
  var onClick, ajaxSuccessHandleMaker;
  onClick = function() {
    var
      url = 'clientnetworkpricelist/updateprice.php',
      clientid = $('#client')[0].value,
      classesArray = $(this).attr('class').split(" "),
      // send data as object, jQuery will transparently transform for the server
      data = {
        value : $('.ajax input').val,
        rowid : classesArray[2],
        field : classesArray[1],
        clientid : clientid
      };
    // send POST request and expect JSON
    $.post(url,data,ajaxSuccessHandleMaker(classesArray),'json');
  };
  // success returns the ajax handler with a closure on classesArray
  ajaxSuccessHandleMaker = function (arr) {
    // the handler EXPECTS an array, which is why we have to protect output in updateprice.php
    return function (arrayOf2vals) {
      $('#CPH_GridView1_clientprice'+arr[2]).html(arrayOf2vals[0]);
      $('#CPH_GridView1_Status'+arr[2]).html(arrayOf2vals[1]);
      // I am not sure what you want with the following 2 lines of code
      $('.ajax').html($(this).val());// what is this?
      $('.ajax').removeClass('ajax');// what is this?
    };
  };
  // set the onClick handler
  $('.edit2').click(onClick);
});

最后

  • 欢迎提问
  • 请测试它是否有效,如果无效,请回复,我会尽力提供帮助

关于javascript - 如何使用ajax将检索到的数据分配给不同的td,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20262515/

相关文章:

php - 如何使用 PHP 的 GD 库对图像执行缝线雕刻?

javascript - AngularJS + $q,在多个 ajax 调用完成后做一些事情

jquery - 当表单包含文件时,Ajax 请求在 IE10 中不起作用

javascript - 在 ReactJS 中切换 View 中的组件

javascript - 如何预解析 JSON 字符串?

javascript - 为什么 .not() 在这种情况下不起作用?

javascript - 如何在 Fullcalendar 中举办不同类型的事件?

php - 如何在Azure Web应用程序上发送PHP邮件功能?

jQuery ajax错误函数即使查询成功也会执行

c# - 如何在url中将特殊字符作为查询字符串传递