2014-05-17 36 views
-1

我目前正在試圖做一個網站,我把信息放在我的html端,它將信息發送到php文件,並從另一個網站獲取信息,具體取決於您在html端放置的內容。如何將一個變量從一個php文件傳遞給一個javascript文件?

爲了使這更清楚,這是它如何工作的:

  1. 你把你的用戶名在HTML站點(JavaScript代碼 是)。
  2. 然後它將您的用戶名發送到php文件。
  3. php文件獲取信息,將其放入鏈接並請求它。
  4. 它從這個請求抓取信息(高分頁)。
  5. 這是停止的地方。

我不知道如何將信息返回給我的JavaScript文件。

這是我的代碼:

HTML/JavaScript頁面:

<html> 
<head> 
<script type="text/javascript" src="./aloticcalc_files/jquery.min.js"></script> 
</head> 

<body> 
<script> 
    function test() { 
     var username = "Exzib"; 
     window.location.href = "grabinfo.php?username=" + username; 
    } 
</script> 
</body> 
</html> 

這是我的PHP文件:(grabinfo.php

<html> 
<head> 
</head> 
<?php 
$username = $_GET['username']; 
include_once('simple_html_dom.php'); 
if(isset($_GET['name'])) { 
    $html = file_get_html('https://alotic.com/hs/?name='.$username); 
    $xp = $html->find('td', 10); 
    $formatxp = $result=str_replace(array('$',',','.',' '),'',$xp); 

    echo $formatxp; 
} 
?> 
<body> 
</body> 
</html> 

那麼,如何進行發送$formatxp到我的JavaScript?

感謝

+2

'var javascript_var ='<?php echo $ formatxp; '>'' – adeneo

回答

1

更改以下行

$formatxp = $result=str_replace(array('$',',','.',' '),'',$xp); 

$formatxp = str_replace(array('$',',','.',' '),'',$xp); 

而且傳遞變量的Javascript,你可以做這樣的事情。

<script type="text/javascript"> 

    var $formatxp = '<?php echo $formatxp; ?>'; 

</script> 
1

編寫一個腳本標記並將該變量設置爲等於您的PHP變量。

<script> 
    var thing = '<? echo $formatxp; ?>'; 
</script> 
3

所以,你要做的就是執行:

window.location.href = "grabinfo.php?username=" + username;

這將導致瀏覽器導航到grabinfo.php並顯示你想要的信息。除了你不想顯示信息,你想要將它檢索到一個JavaScript變量,對吧?

爲此,請勿設置window.location.href。而是使用AJAX調用您的腳本。你會發現很多關於AJAX的教程。這將允許您捕獲PHP腳本的輸出。

0

這將會很容易,因爲您已經包含jQuery。(使用AJAX)

<html> 
<head> 
<script type="text/javascript" src="./aloticcalc_files/jquery.min.js"></script> 
</head> 
<body> 
    <label>Type your username:<input id="username" type="text"></label> 
    <input id="submit-button" type="button" value="Get my highscores!"> 
    <p id="response"></p> 
<script> 
    //When user clicks "submit-button" button 
    $("#submit-button").on('click', function() { 
     var username = $("#username").val(); 
     //We request our php with the username 
     $.get("grabinfo.php?username=" + username, function(xpValue) { 
      //When we receive the response from php then we add it to a "p" tag 
      $("#response").text("LOL your xp is: "+xpValue+". NOOOOOOOB!!"); 
     }); 
    }); 
</script> 
</body> 
</html> 

就是這樣!在http://api.jquery.com/on/
http://api.jquery.com/get/
http://api.jquery.com/text/
http://api.jquery.com/val

而且還有整個jQuery的API文檔

更多信息http://api.jquery.com

編輯:
OH。您必須更改grabinfo.php。它應該只有這樣:

<?php 
$username = $_GET['username']; 
include_once('simple_html_dom.php'); 
if(isset($_GET['name'])) { 
    $html = file_get_html('https://alotic.com/hs/?name='.$username); 
    $xp = $html->find('td', 10); 
    $formatxp = $result=str_replace(array('$',',','.',' '),'',$xp); 
    if(empty($formatxp)) { 
     echo "ERROR: Invalid could not find xp with username {$username}"; 
    } 
    else { 
     echo $formatxp; 
    } 
} 
else { 
    echo "ERROR: Invalid arguments"; 
} 
+0

嘿,這看起來不錯,標籤和輸入正是我不得不使用的下一個和我在想什麼,但是當我執行你的代碼時,它給了我這個頁面:http:// puu。 sh/8PIOr.png – user3019278

+0

另外我在控制檯中得到這個錯誤:event.returnValue已被棄用。請改用標準的event.preventDefault()。 – user3019278

+0

我編輯了答案。 「event.returnValue」警告來自jQuery。忽略它。 –

相關問題