使用Ajax帖子

时间:2014-12-15 14:26:17

标签: javascript php ajax

修改 这是我设法建立的...但它仍然无法工作..

    $(document).ready(function(){
  $("tr").click(function(){
    txt=$("input[name=path]").val();
   $.ajax({
   type: "POST",
   url: contract.php, // the same page where i have the table
   data: txt,         // the variable containing the dynamic id from the clicked row
   success: success,  // i have no idea what is this parameter for....
   dataType: dataType  // i have no idea what is this parameter for....
});
  });
});

PHP:

$row=mysql_fetch_array($query){
echo '<tr id="'.$row['id'].'">';
 echo '<td></td>';
echo '</tr>';
}

我想要的是,当用户点击一行时,必须采用行ID(动态),并使用ajax帖子返回,因此我可以在另一个查询中使用它。我必须这样做而不重新加载页面,这就是我尝试用ajax做的原因。

2 个答案:

答案 0 :(得分:1)

如果可以使用jQuery,我会使用这样的东西来构建我的表(在mysql _ *旁边):

<?php
    $row=mysql_fetch_array($query){
        echo '<tr class="listContractRow" data-path="'.$row['id'].'">';
        echo '</tr>';
    }
?>

然后使用jQuery监听器捕获click事件:

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>

    $(document).on('click','tr.listContractRow', function(e){

        var path = $(this).data('path');

        //Use the variable path here in your AJAX call 
        //Assuming you want a GET request, you can also use $.ajax or $.post here
        $.get('YOUR_AJAX_URL_HERE?path='+path, function(){

            //Do something after the ajax call

        });

    });

</script>

编辑

在PHP中,您可以执行以下操作:

<?php

    if(isset($_GET['path']))
    {
        //Query here with the variable $_GET['path'];
        //Echo the results you want

        //Perform an exit here, since it's an AJAX call. You probably don't want to echo the code below (if there is)
        exit();
    }

?>

答案 1 :(得分:-2)

jquery ajax get example

$.ajax({
    type: "POST",
    url: "index.php", 
  data: "{param: "param"}",
    success: function(msg) {

    },
    error: function(err) {

    }
});