使用jquery ajax将php页面输出加载到html div中

时间:2013-01-29 06:07:26

标签: php ajax jquery

我想将php页面的输出呈现为html div。这意味着在php页面中有很多jquery stuff.suppose我用当前的jquery ajax来加载一个php页面。它错过了加载jquery文件准备好的东西。

我该怎么做?

3 个答案:

答案 0 :(得分:4)

如果你想使用jquery和ajax将php页面的输出放到html页面。你可以这样做。

$(document).ready(function(){
    $("#div1").load("demo_test.php");
});

div1是要用php内容更新的div。 这个链接可以帮到你。

http://www.w3schools.com/jquery/jquery_ajax_intro.asp

答案 1 :(得分:0)

这是简短的例子。 jQuery API Doc

<强> HTML

 <div id="content"></div>
 <input type="button" id="btnLoad" value="Load" />

<强> jquery的

$("#btnLoad").click(function(){

    $.ajax({
        type: 'POST',
        url: 'page1.php',
        success: function(data){
                 if(data != null) $("#content").text(data)
         }
     });
});

<强> page1.php中

<?php

  echo "This is the sample data to be printed for request from AJAX";

 ?>

答案 2 :(得分:0)

您可以使用ajax observe方法

<强> cakephp ajax helper

<?php echo $form->create( 'Post' ); ?>
<?php $titles = array( 1 => 'Tom', 2 => 'Dick', 3 => 'Harry' ); ?>  
<?php echo $form->input( 'title', array( 'options' => $titles ) ) ?>
<label for="something">This checkbox will be send (and it will trigger Ajax reqest)  </label>
<input id="something" type="checkbox" name='data[Post][something]' value='1' />
</form>

<?php
echo $ajax->observeForm( 'PostAddForm', 
    array(
        'url' => array( 'action' => 'edit' ),
        'complete' => 'alert(request.responseText)'
    ) 
); ?>
相关问题