weixin_33744854 2015-02-18 19:35 采纳率: 0%
浏览 40

jQuery.ajax替换PHP代码

I'm trying to update a REST search result with ajax to not reload a new page.

right now search result is shown within:

<div id="searchtable"><?php include 'hotels/hotelList.php';?></div>

On click on button I want to reload this div element so it includes a new php file.

Button:

<button onclick="myFunction()">LOAD</button><br /><br />

jQuery

<script>
function myFunction() {
  $.get( "/hotels/hotelSortBy.php" );
  $('#searchtable').replaceWith('<?php include 'hotels/hotelSortBy.php';?  >');
 }
</script>

I'm not able to replace the div element "searchable" - with replaceWith - Am I doing this right ?

  • 写回答

3条回答 默认 最新

  • weixin_33719619 2015-02-18 19:38
    关注

    There's no need for inline PHP if you're using AJAX. Try using jQuery's .load AJAX method:

    function myFunction() {
      $('#searchtable').load('/hotels/hotelSortBy.php');
    }
    

    Note that this is slightly different than .replaceWith -- using .load will preserve #searchtable and put the new content inside of it, rather than replacing it.

    评论

报告相同问题?