使用ajax post类型在同一页面js上使用php中的js变量

时间:2017-02-08 10:46:25

标签: javascript php ajax

我有一个PHP文件名作为report.php,其中我还使用post类型为id创建了一个脚本,我有一个post类型的ajax用于传递id。     我想在report.php查询中使用该帖子类型id来比较id。     那么如何在我的$ str查询中获取该帖子ID进行比较呢?

<script type="text/javascript">
    $(document).ready(function(){
      $('#project').on('click',function(){
        var pID = $(this).val();
        alert(pID);
        if(pID){
          $("#developer").show();
          $.ajax({
          type:'POST',
          url:'report1.php',
          data:'id='+pID,
          success:function(html){
            $('#developer').html(html);
          }
          }); 
        }
      });
    });
</script>

在下面的查询中,我想比较帖子类型ID和给定值。

$str = "SELECT GROUP_CONCAT(CONCAT((u.firstname),' ',(u.lastname))) as dname, p.name, GROUP_CONCAT(t.hours)as spent_hrs, SUM(t.hours) as tot_spent,t.spent_on FROM time_entries as t 
    INNER Join projects as p on p.id = t.project_id 
    INNER Join users as u on u.id = t.user_id 
    where p.id=$_POST[id] and u.id=6 
    GROUP By p.id,t.spent_on ";

3 个答案:

答案 0 :(得分:1)

更改您的ajax请求数据:

$.ajax({
    type:'POST',
    url:'report1.php',
    data:'id:'+pID,
    success:function(html_data){
      $('#developer').html(html_data);
    }
}); 

然后在 report1.php 页面中,您可以使用$_POST['id'];获取ID

答案 1 :(得分:0)

$('#developer').on('click',function(){
        var dID = $(this).val();
        if(dID){
            $.ajax({
                type:'POST',
                url:'report1.php',
                data: 'pid='+pID+'did='+dID,
                success:function(html_data){
                    // console.log(html);
                  $('#developer').html(html_data);
                 }  
            }); 
        }
    });

答案 2 :(得分:-1)

你可以打击想法看到这个答案进一步查询你可以问一个社区 jQuery Ajax POST example with PHP

$('#devloper').on('click',function(){
        var dID = $(this).val();


      });