2013-01-03 40 views
1

使用jquery,我試圖從.php文件中提取信息並在另一個html頁面中運行它。如何使用jquery從.php文件中拉出一些php並在另一個頁面上運行它?

要嘗試使用此我試圖用這個例子: http://www.tutorialspoint.com/jquery/ajax-jquery-get.htm

的PHP文件將包括:

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

=============== = 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> 

jQuery庫已按照「測試結果」成功加載。

出於某種原因,「testing.php」文件的內容沒有被拉動......就好像它的鏈接不正確。文件鏈接不正確? (.php和.html文件都在同一個文件夾中)

我甚至試着做一些簡單的事情,比如php中的echo語句,但仍然沒有任何東西從文件中拉出來併發布到html頁面。

您可能會看到一個簡單的修復程序。我很感激你幫助我解決這個問題的時間和精力。提前致謝!

+1

嘗試改變'「/測試。 PHP「'到'」testing.php「' –

+0

,但等待這已經起作用了。我只是在我的服務器上試過你的代碼,它的工作方式和你的期望一樣所以你面臨的問題是什麼? –

+0

jQuery 1.3.2?這差不多已經三年了。使用庫的最新版本。 – Quentin

回答

0

你可以簡單地這樣做:$('#stage').load('testing.php?name=Zara');

+0

但是,這不會將'name'參數傳遞給php頁面? –

+0

如果問題中的代碼不適用,爲什麼要這樣工作?編寫新代碼以執行與原始代碼相同的工作並不能解決問題,因爲它可能具有相同的基本問題。這甚至不會執行與URL已更改並且查詢字符串數據已被丟棄的作業。 – Quentin

+0

下面是我最終理想的結果的更詳細的描述:http://stackoverflow.com/questions/14148420/determine-browser-width-with-jquery-then-call-one-out-of-two-php-files - 從...再次感謝您的時間,精力和努力!非常感謝。 –

-1

使用jquery.ajax代替。這裏是你的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> 
+0

爲什麼要這樣工作,當原來不?你爲什麼改變網址? – Quentin

+0

因爲URL是這裏的主要問題。改變它做的工作。 – yajakass

+0

你怎麼知道URL是主要問題?如果URL是主要問題,爲什麼不在你的回答中說,而是告訴OP使用'.ajax'而不是'.get'? – Quentin

0

的文件從一個文件夾託管在我的電腦上

這給了你兩個問題:

  1. 瀏覽器換上什麼網頁相當嚴格的限制從文件系統加載就可以了,通過XMLHttpRequest加載其他文件被禁止
  2. PHP是服務器端技術,我t需要服務器才能工作(在這種情況下)

將您的網站託管在網絡服務器上。它可以是一個你在本地運行,那麼你可以通過http://localhost/etc

+0

下面是我最終理想的結果的更詳細的描述:http://stackoverflow.com/questions/14148420/determine-browser-width-with-jquery-then-call-one-out-of-two-php-files - 從...再次感謝您的時間,精力和努力!非常感謝。 –

-1

訪問該網站我寧願做以下...

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

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

     ); 
     }); 
    }); 
</script> 
+0

下面是我最終理想的結果的更詳細的描述:http://stackoverflow.com/questions/14148420/determine-browser-width-with-jquery-then-call-one-out-of-two-php-files - 從...再次感謝您的時間,精力和努力!非常感謝。 –

相關問題