简体   繁体   中英

How do I pass a variable from one php file to a javascript file?

I am currently trying to make a site where I put in info in on my html side, it send the info to the php file and grabs info from another site depending on what you put in on the html side.

To make this more clear this is how it works:

  1. You put in your username in the html site(where the javascript code is).
  2. Then it sends your username to the php file.
  3. The php file gets the information, puts it in a link and request it.
  4. It grabs information from this request (highscores page).
  5. This is where it stops.

I don't know how to return the information to my javascript file.

This is my code:

html/javascript page:

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

This is my php file: ( 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>

So how do I proceed to send the $formatxp to my javascript?

Thanks

So what you do is execute:

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

This causes the browser to navigate to grabinfo.php and display the information that you want. Except that you don't want the information to be displayed, you want to retrieve it into a JavaScript variable, right?

To do so, don't set window.location.href . Instead call your script using AJAX. You'll find plenty of tutorials on AJAX out there. This will allow you to capture the output of your PHP script.

Change the following line

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

to

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

And to pass the variable to Javascript you can do something like this.

<script type="text/javascript">

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

</script>

Write a script tag and set the variable equal to your PHP variable.

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

This is going to be easy as you already include jQuery. (Using ajax)

Look

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

And that's it!

More info at http://api.jquery.com/on/
And http://api.jquery.com/get/
http://api.jquery.com/text/
http://api.jquery.com/val

And well the whole jQuery API documentation http://api.jquery.com

EDIT:
OH. You have to change grabinfo.php . It should only have this:

<?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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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