2014-05-17 2 views
-1

저는 현재 HTML쪽에 정보를 넣는 사이트를 만들고자합니다. HTML쪽에 넣은 내용에 따라 다른 사이트의 정보와 PHP 파일을 보냅니다. 하나의 PHP 파일에서 자바 스크립트 파일로 변수를 전달하는 방법은 무엇입니까?

이 그것을 어떻게 작동이 더 명확하게하려면 :

  1. 당신은 (자바 스크립트 코드 는) HTML 사이트에 사용자 이름에 넣어.
  2. 그런 다음 사용자 이름을 PHP 파일로 보냅니다.
  3. php 파일은 정보를 가져 와서 링크에 넣고 요청합니다.
  4. 이 요청 (최고 기록 페이지)에서 정보를 가져옵니다.
  5. 여기가 중지됩니다.

정보를 자바 스크립트 파일로 보내는 법을 모르겠습니다.

HTML/자바 스크립트 페이지 : 이것은 내 PHP 파일이

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

입니다 : (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을 보내시겠습니까?

감사

+2

'var javascript_var = ' '' – adeneo

답변

1

변경

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

그리고 다음 줄

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

은 이런 식으로 뭔가를 할 수있는 자바 스크립트에 변수를 전달합니다.

<script type="text/javascript"> 

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

</script> 
1

스크립트 태그를 작성하고 변수를 PHP 변수와 동일하게 설정하십시오.

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

이것은 grabinfo.php하고 원하는 정보를 표시하는 탐색 브라우저를 발생합니다

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

은 그래서 당신이 할 것은 실행이다. 정보를 표시하는 것을 원하지 않는다는 것을 제외하고는 JavaScript 변수로 검색해야합니다.

이렇게하려면 window.location.href을 설정하지 마십시오. 대신 AJAX를 사용하여 스크립트를 호출하십시오. 밖에있는 AJAX에 대한 많은 자습서를 찾을 수 있습니다. 이렇게하면 PHP 스크립트의 출력을 캡처 할 수 있습니다.

0

jQuery가 이미 포함되어 있으므로 쉽게 할 수 있습니다.(사용 아약스)

<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/
잘 전체의 jQuery API 문서 http://api.jquery.com/val

그리고
http://api.jquery.com/text/
http://api.jquery.com/get/에서

더 많은 정보 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에서 가져온 것입니다. 무시해. –

관련 문제