2014-07-09 30 views
0

因此,我有2個文件file1.php與所有的PHP和AJAX,和file2.php只有PHP。如何在發送ajax數據後加載php文件

$_POST["there_id"]將通過ajax從file1.php頁面發送到file2.php。

file1.php代碼:

<div class="list_items"> 
    <a href="" class="box" id="56545"><li><p>content here ...</p></a> 
</div> 

<div class="content_area"> 
    //load ajax content here 
</div> 

$(".box").on("click", function(e) { 
    e.preventDefault(); 
    var there_id = $(this).attr("id"); 
    $.ajax({ 
     url: "indexnew.php", 
     type: "POST", 
     data: { there_id : there_id} 
    }); 
}); 

file2.php代碼:

<div id="file2content> 
<?php 
    $there_id = $_POST['there_id']; 
    $user_id = 5; 
    $fetch_data = regular_query("SELECT * FROM contents WHERE 
    (user_by = :me or user_by + :them) AND (user_to = :me or user_to = :them)", 
    ["me" => $user_id, "them" = $there_id], $conn); 

    foreach ($fetch_data as $data) : ?> 

    <a href=""><li><p>database data here</p></a> 

<?php 
    endforeach; 
?> 

</div> 

現在我想做的是當file2.php與PHP和列表項做都可以,我想在file1.php上加載file2contentscontent_area

回答

2
$(".box").on("click", function(e) { 
    e.preventDefault(); 
    var there_id = $(this).attr("id"); 
    $.ajax({ 
     url: "indexnew.php", 
     type: "POST", 
     data: { there_id : there_id}, 
     success: function(data) { 
      $('.content_area').html(data); 
     } 
    }); 
}); 

但是,content_area應該是一個id,而不是一個類,因此它是唯一的。

由於您剛剛返回html,因此可以使用​​函數來簡化它。

$('.box').on('click', function(e) { 
    e.preventDefault(); 
    var there_id = $(this).attr('id'); 
    $('.content_area').load('indexnew.php', {there_id: there_id}); 
}); 
+0

我們倆都忘了指出Javascript真的需要在標籤中,如果它做任何事情:') – t3chguy

+0

所以函數(數據)就是file2.php中的所有東西? –

+0

@ this.Tony返回的數據將是Ajax調用返回的原始數據.--我說RAW除非JSON和Ajax調用自動解析它。 – t3chguy

0
success: function(data) { $(".content_area").html(data); } 

添加上述到您的AJAX調用應該做你想要什麼,如果我理解正確。