4

I need a little bit of help im trying to page a js variable into a url thats being parsed in php using file_get_contents. Im not sure where to start to do that.

<script type="text/javascript">
var js_variable = appl+goog+fb+mfst+nflx;
</script>

<?php
$ticker = js_varable_here;
$file = file_get_contents('http://finance.yahoo.com/d/quotes.csv?s=$ticker&f=soac1p2ghjkj1re');

?>

any advice is appreciated, like i said im in the dark on this one.

4
  • 6
    php runs on server and js resides at client. First it gets to server and than to client. You cant pass like that. You can use ajax though Commented May 25, 2012 at 21:58
  • 1
    use ajax to get the content you want Commented May 25, 2012 at 22:03
  • 2
    This exact question keeps appearing again and again... Commented May 25, 2012 at 22:06
  • is the js_variable set in your source code (which is what is in your example) or is it set on the client side (a more likely use case)? Commented May 25, 2012 at 22:11

3 Answers 3

2

Here's an example using jquery.

Javascript:

<script type="text/javascript">
  var js_variable = appl+goog+fb+mfst+nflx;
  $.post("/somephp.php", {ticker: js_variable}, function(data) {
    // returned from php
  });
</script>

PHP:

 <?php
   $ticker = $_POST['ticker'];
   $file = file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=$ticker&f=soac1p2ghjkj1re");
 ?>
Sign up to request clarification or add additional context in comments.

2 Comments

so the php part would go into the somephp.php file? just so i understand correctly
@Suzed yes. In this example, that's where the PHP would go. You can also change the url in the javascript to "./" if you are mixing html and php. (Which means you should probably add some logic in the PHP portion so that it detects a post request.)
1

Expanding on what Jashwant says...

PHP is a server-sided language, which does work behind the scenes. Javascript is client-side, which runs and executes code on the local client's machine (ie through the browser).

You can however use AJAX (Asynchronous JavaScript and XML) so the local client sends HTTP requests to the server without reloading the current page. For instance, you can use AJAX to send the contents of the variable to the server.

For easier usage, you should check out jQuery's methods regarding ajax calls. See: http://api.jquery.com/jQuery.ajax/

Hope it works well.

Comments

1

Heres how you can do it with jquerys post() and then return json, you could build the result as you expect to output within the php part or you could use jquery to loop with each() through the result.

<?php
if($_SERVER['REQUEST_METHOD']=='POST'
   && isset($_SERVER['HTTP_X_REQUESTED_WITH'])
   && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'){

    if(!empty($_POST['s'])){

        $ticker = $_POST['s'];
        $file = file_get_contents('http://finance.yahoo.com/d/quotes.csv?s='.$ticker.'&f=soac1p2ghjkj1re');

        header('Content-Type: application/json');
        echo json_encode(array('result'=>$file));
    }else{
        echo 'Request not allowed!';
    }
    die;
}
?>
<!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">
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" charset="utf-8"></script>
<script>
var js_variable = "appl+goog+fb+mfst+nflx";

$.post('this_script.php',{s: js_variable}, function(data) {
  $('#divResult').replaceWith('<div id="divResult">'+ data.result +'<div>');
});
</script>
</head>
<body>

<div id="divResult"><div>
</body>
</html>

4 Comments

i tested your code just to see if there was much of a difference and it doesnt seem to work, data comes back as undefined
Its working for me, have you changed this_script.php in the jquery post?
also you should see if $file contains anything. perhaps FGC is not enabled.
I changed this_script to / so it is working from the same file and if you change the js_variable it to goog, it should return values. but other then that nothing was changed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.