Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Jquery&;在DIV中包含PHP文件;AJAX_Php_Jquery_Html_Ajax - Fatal编程技术网

使用Jquery&;在DIV中包含PHP文件;AJAX

使用Jquery&;在DIV中包含PHP文件;AJAX,php,jquery,html,ajax,Php,Jquery,Html,Ajax,我的问题是,当按下按钮而不重新加载页面时,我需要在DIV中包含一个PHP文件 “Jsfiddle”文件中有更多的解释 下面是附带的JSFIDLE文档 谢谢你抽出时间。我非常乐意应要求提供任何信息 尝试以下操作: <button name="Change" id="Change">Change Div</button> 更改Div 您正在为id指定单击功能,但按钮上未设置id。请尝试以下操作: <button name="Change" id="Change"&

我的问题是,当按下按钮而不重新加载页面时,我需要在DIV中包含一个PHP文件

“Jsfiddle”文件中有更多的解释

下面是附带的JSFIDLE文档

谢谢你抽出时间。我非常乐意应要求提供任何信息

尝试以下操作:

<button name="Change" id="Change">Change Div</button>
更改Div
您正在为id指定单击功能,但按钮上未设置id。

请尝试以下操作:

<button name="Change" id="Change">Change Div</button>
更改Div
您正在为id指定单击功能,但按钮上未设置id。

请参阅

您已将更改按钮的名称标记为
change
,但尝试使用id为
change
来选择它。另外,您还没有告诉JSFIDLE包含jQuery。

请参见

您已将更改按钮的名称标记为
change
,但尝试使用id为
change
来选择它。另外,您还没有告诉JSFIDLE包含jQuery。

您可以尝试在jQuery中使用load()函数

您可以在jquery中尝试使用load()函数


不能将PHP文件包含在AJAX中,而是包含AJAX服务器端脚本的响应,即PHP(具有相同的效果)

加载。。。

JS文件(代码):

下面是PHP文件:

<?php
    $id = (int) @$_GET['id']; // same as in data part of ajax query request
    $action = @$_GET['action']; // same as in data part of ajax query request

    $text = '<a href="/index.php?id=' . $id . '&action=' . $action . '">click me</a>';

    // Note this is short example you may want to echo instead of die
    // You may use not JSON, but raw text. However, JSON is more human-friendy (readable)
    // and easy to maintain.
    // Note also the array keys are used in the onAjax function form res (response).
    die(json_encode(array('text' => $text /* and anything else you want */)));
?>

您不能将PHP文件包含在AJAX中,而是包含AJAX服务器端脚本的响应,即PHP(具有相同的效果)

加载。。。

JS文件(代码):

下面是PHP文件:

<?php
    $id = (int) @$_GET['id']; // same as in data part of ajax query request
    $action = @$_GET['action']; // same as in data part of ajax query request

    $text = '<a href="/index.php?id=' . $id . '&action=' . $action . '">click me</a>';

    // Note this is short example you may want to echo instead of die
    // You may use not JSON, but raw text. However, JSON is more human-friendy (readable)
    // and easy to maintain.
    // Note also the array keys are used in the onAjax function form res (response).
    die(json_encode(array('text' => $text /* and anything else you want */)));
?>

PHP是一种服务器端脚本语言,将在JavaScript脚本执行之前执行

因此,您不能使用
.load()
来执行PHP代码,但是,您可以尝试
.ajax()
来创建对服务器的ajax请求,该服务器可以实现PHP代码

请查看您在使用
.ajax()
时是否遇到问题

注意:在
.ajax()
方法中,有一个名为
beforeSend
的设置,该设置“可用于在发送jqXHR(在jQuery 1.4.x中,XMLHTTPRequest)对象之前修改它”。希望这个方法对你有所帮助

然后,您的JavaScript代码如下所示:

$(document).ready(function(){
  $("#Change").click(function(){
    //doing AJAX request
    $.ajax({
      url:"include/start10.php",
      beforeSend:function(){
        $('#myDiv').fadeOut('slow');
      },
      success:function(data){
        // do something with the return data if you have
        // the return data could be a plain-text, or HTML, or JSON, or JSONP, depends on your needs, if you do ha

        $('#myDiv').fadeIn('slow');
      }
    });    
  });
});

PHP是一种服务器端脚本语言,它将在JavaScript脚本执行之前执行

因此,您不能使用
.load()
来执行PHP代码,但是,您可以尝试
.ajax()
来创建对服务器的ajax请求,该服务器可以实现PHP代码

请查看您在使用
.ajax()
时是否遇到问题

注意:在
.ajax()
方法中,有一个名为
beforeSend
的设置,该设置“可用于在发送jqXHR(在jQuery 1.4.x中,XMLHTTPRequest)对象之前修改它”。希望这个方法对你有所帮助

然后,您的JavaScript代码如下所示:

$(document).ready(function(){
  $("#Change").click(function(){
    //doing AJAX request
    $.ajax({
      url:"include/start10.php",
      beforeSend:function(){
        $('#myDiv').fadeOut('slow');
      },
      success:function(data){
        // do something with the return data if you have
        // the return data could be a plain-text, or HTML, or JSON, or JSONP, depends on your needs, if you do ha

        $('#myDiv').fadeIn('slow');
      }
    });    
  });
});

这是否适用于完整的PHP页面?我想让这个函数像“PHP包含”一样工作。@tushar747很抱歉这么晚才响应。与PHP相比,Javascript是一种完全不同的语言,但它们可以在HTML文档中协同工作,如:`alert('here is the JS code')`这是否适用于完整的PHP页面?我想让这个函数像“PHP包含”一样工作。@tushar747很抱歉这么晚才响应。与PHP相比,Javascript是一种完全不同的语言,但它们可以在HTML文档中协同工作,如:`alert('here is the JS code')`对不起,这是JSFIDLE上的一个错误,而不是我的实际代码。抱歉,这是JSFIDLE上的一个错误,不是我的实际代码。很抱歉。