Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.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
从javascript(ajax/jquery)调用的PHP脚本作为警报对话框而不是原始页面进行响应_Javascript_Php_Jquery_Ajax - Fatal编程技术网

从javascript(ajax/jquery)调用的PHP脚本作为警报对话框而不是原始页面进行响应

从javascript(ajax/jquery)调用的PHP脚本作为警报对话框而不是原始页面进行响应,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我需要能够将数据从java脚本传递到php,并将新数据从php脚本发送回java脚本。但是,当我将数据发送到php并在新的php脚本中回显时,回显的内容通过警报发送 下面是我试图做的一个简化的不正确的版本 index.php <!DOCTYPE html> <html> <head> <script src="jquery.js"></script> <script src="script.js"> <

我需要能够将数据从java脚本传递到php,并将新数据从php脚本发送回java脚本。但是,当我将数据发送到php并在新的php脚本中回显时,回显的内容通过警报发送

下面是我试图做的一个简化的不正确的版本

index.php

<!DOCTYPE html>
<html>
<head>


   <script src="jquery.js"></script>
   <script src="script.js"> </script>
</head>
<body> </body>
</html>
loadContent.php

<?php
  if(isset($_POST['action']) && !empty($_POST['action'])) {
      $action = $_POST['action'];
      echo $action;
  }
 ?>

这段代码当前所做的是显示一个警报,其中包含通过script.js发送到loadContent.php的任何文本。我希望它做的是将内容回显到原始页面。

如果您只想将loadContent.php的输出放到页面中,您可以使用

$.ajax({ url: 'loadContent.php',
      data: {action: 'HEYO'},
      type: 'post',
      success: function(output) {
        $('body').html(output);
      }
    });

您需要将此内容粘贴到html页面上的任何div或其他html元素中。alert正在执行它所要执行的任务

因此,代码需要:-

HTML代码:-

<!DOCTYPE html>
<html>
<head>
   <script src="jquery.js"></script>
   <script src="script.js"> </script>
</head>
<body> 
  <div id="result"></div><!-- created a div to represent the output of ajax code-->
</body>
</html>

警报输出;它正在做你告诉它做的事情。你需要将此内容粘贴到html页面上的任何div或其他html元素中。alert正在执行它所指的确切工作,您是想将loadContent.php的响应放入原始页面还是向其发出警报?Uuuh。。。Omatt仍然在提醒我,这肯定是模棱两可的,我想他希望php能够响应原始页面的内容,而不是将其放在页面上
<!DOCTYPE html>
<html>
<head>
   <script src="jquery.js"></script>
   <script src="script.js"> </script>
</head>
<body> 
  <div id="result"></div><!-- created a div to represent the output of ajax code-->
</body>
</html>
$.ajax({ url: 'loadContent.php',
  data: {action: $('html').html()},
  type: 'post',
  success: function(output) {
    $('#result').html(output); // paste the output to the div
  }
});