简体   繁体   English

将js变量传递给php字符串

[英]Passing js variable into php string

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. 我需要一些帮助,我试图将js变量页面转换为使用file_get_contents在php中解析的url。 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. 任何建议都表示赞赏,就像我在这个黑暗中说的那样。

Here's an example using jquery. 这是使用jquery的示例。

Javascript: 使用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:

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

Expanding on what Jashwant says... 扩展Jashwant所说的......

PHP is a server-sided language, which does work behind the scenes. PHP是一种服务器端语言,可以在幕后工作。 Javascript is client-side, which runs and executes code on the local client's machine (ie through the browser). Javascript是客户端,它在本地客户端的计算机上运行并执行代码(即通过浏览器)。

You can however use AJAX (Asynchronous JavaScript and XML) so the local client sends HTTP requests to the server without reloading the current page. 但是,您可以使用AJAX(异步JavaScript和XML),以便本地客户端将HTTP请求发送到服务器,而无需重新加载当前页面。 For instance, you can use AJAX to send the contents of the variable to the server. 例如,您可以使用AJAX将变量的内容发送到服务器。

For easier usage, you should check out jQuery's methods regarding ajax calls. 为了便于使用,您应该查看jQuery关于ajax调用的方法。 See: http://api.jquery.com/jQuery.ajax/ 请参阅: http//api.jquery.com/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. 继承人如何使用jquerys post()然后返回json,你可以按照你希望在php部分中输出的结果来构建结果,或者你可以使用jquery通过结果循环每个()。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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