简体   繁体   English

如何使用jquery从.php文件中提取一些php并在另一页上运行它?

[英]How can I use jquery to pull some php from a .php file and run it on another page?

With jquery, I'm attempting to pull information from a .php file and run it in another html page. 使用jquery,我试图从.php文件中提取信息并在另一个html页面中运行它。

To try this out I attempted to use this example: http://www.tutorialspoint.com/jquery/ajax-jquery-get.htm 为了尝试这一点,我尝试使用以下示例: http : //www.tutorialspoint.com/jquery/ajax-jquery-get.htm

The .php file would include: .php文件将包括:

    <?php
    if( $_REQUEST["name"] )
    {
        $name = $_REQUEST['name'];
        echo "Welcome ". $name;
    }
    ?>

================ The html page code includes: ================ html页面代码包括:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Sandbox</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
    // has the google object loaded?
    if (window.google && window.google.load) {
        google.load("jquery", "1.3.2");
    } else {
        document.write('<script type="text/javascript" src="http://joecrawford.com/jquery-1.3.2.min.js"><\/script>');
    }
    window.onload = function() {
        $('#test').css({'border':'2px solid #f00'});
    };
    </script>
    </head>
    <body>
            <!-- -------- Test jQuery -------- -->
            <p id="test">hello jQuery</p>
            <!-- -------- /end Test jQuery -------- -->

    <script type="text/javascript" language="javascript">
      $(document).ready(function() {
          $("#driver").click(function(event){
              $.get( 
                 "/testing.php",
                 { name: "Zara" },
                 function(data) {
                    $('#stage').html(data);
                 }

              );
          });
       });
    </script>
     <p>Click on the button to load result.html file:</p>
       <div id="stage" >
              STAGE
       </div>
       <input type="button" id="driver" value="Load Data" />
    </body>

The jquery library has successfully loaded, as per the "test result". 根据“测试结果”,jQuery库已成功加载。

For some reason, the contents of the "testing.php" file are not being pulled... almost as though it's linked incorrectly. 由于某种原因,“ testing.php”文件的内容没有被拉出……几乎就像是链接错误一样。 Is the file linked improperly? 文件链接不正确吗? (both the .php and .html files are in the same folder) (.php和.html文件都在同一文件夹中)

I even tried doing something simple, like an echo statement in the php, but still nothing was pulled from the file and published to the html page. 我什至尝试做一些简单的事情,例如php中的echo语句,但仍然没有任何内容从文件中提取并发布到html页面。

There might be a simple fix that you see. 您可能会看到一个简单的修复程序。 I appreciate your time and energy in helping me resolve this issue. 感谢您的时间和精力帮助我解决此问题。 Thanks in advance! 提前致谢!

您可以简单地做到这一点: $('#stage').load('testing.php?name=Zara');

The files are hosted from a folder on my computer 这些文件从我计算机上的文件夹托管

This gives you two problems: 这给您两个问题:

  1. Browsers put pretty strict restrictions on what webpages loaded from the filesystem can do, loading other files via XMLHttpRequest is forbidden 浏览器对从文件系统加载的网页可以执行的操作施加了非常严格的限制,禁止通过XMLHttpRequest加载其他文件
  2. PHP is a server side technology, it needs a server to work (in this context) PHP是服务器端技术,它需要服务器才能工作(在这种情况下)

Host your site on a webserver. 将您的网站托管在网络服务器上。 It can be one you run locally, then you can access the site via http://localhost/etc 它可以是您在本地运行的一个,然后您可以通过http://localhost/etc访问该站点

Use jquery.ajax instead. 使用jquery.ajax代替。 Here is your HTML code 这是您的HTML代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Sandbox</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
// has the google object loaded?
if (window.google && window.google.load) {
    google.load("jquery", "1.3.2");
} else {
    document.write('<script type="text/javascript" src="http://joecrawford.com/jquery-1.3.2.min.js"><\/script>');
}
window.onload = function() {
    $('#test').css({'border':'2px solid #f00'});
};
</script>
</head>
<body>
        <!-- -------- Test jQuery -------- -->
        <p id="test">hello jQuery</p>
        <!-- -------- /end Test jQuery -------- -->

<script type="text/javascript" language="javascript">
  $(document).ready(function() {
      $("#driver").click(function(event){
          $.ajax({ 
             url: "testing.php",
             data: { name: "Zara" },
             success: function(data) {
                $('#stage').html(data);
             }

          });
      });
   });
</script>
 <p>Click on the button to load result.html file:</p>
   <div id="stage" >
          STAGE
   </div>
   <input type="button" id="driver" value="Load Data" />
</body>

I would rather do the following... 我宁愿做以下事情...

<?php
    if( $_GET["name"] )
    {
        $name = htmlspecialchars(trim($_GET['name']));
        echo "Welcome ". $name;
    }
?>

and

<script type="text/javascript" language="javascript">
  $(document).ready(function() {
      $("#driver").click(function(event){
        $.load( "testing.php?name=Zara, function(data) {
                $('#stage').html(data);
             }

          );
      });
   });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM