javascript - Passing js variable into php string - Stack Overflow

I need a little bit of help im trying to page a js variable into a url thats being parsed in php using

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('.csv?s=$ticker&f=soac1p2ghjkj1re');

?>

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

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./d/quotes.csv?s=$ticker&f=soac1p2ghjkj1re');

?>

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

Share Improve this question edited May 25, 2012 at 21:59 tigrang 6,7671 gold badge21 silver badges22 bronze badges asked May 25, 2012 at 21:54 SuzedSuzed 4871 gold badge6 silver badges22 bronze badges 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 – Jashwant Commented May 25, 2012 at 21:58
  • 1 use ajax to get the content you want – aurel Commented May 25, 2012 at 22:03
  • 2 This exact question keeps appearing again and again... – Imp 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)? – Aerik Commented May 25, 2012 at 22:11
Add a ment  | 

3 Answers 3

Reset to default 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./d/quotes.csv?s=$ticker&f=soac1p2ghjkj1re");
 ?>

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./jQuery.ajax/

Hope it works well.

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./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/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis./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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745428714a4627297.html

相关推荐

  • javascript - Passing js variable into php string - Stack Overflow

    I need a little bit of help im trying to page a js variable into a url thats being parsed in php using

    3小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信