简体   繁体   English

我如何从javascript获取变量到PHP或HTML

[英]How i can get a variable from javascript to PHP or HTML

if i make a variable in javascript with this script 如果我使用此脚本在javascript中创建变量

var kakakSisa        = 10;

and i wanna call it in PHP for example 我想用PHP为例

<?php echo <script type="text/javascript">kakakSisa;</script> ?>

and that doesn't work!! 那是行不通的! how i can make it work? 我如何使其工作?

Use AJAX to communicate client and server side. 使用AJAX进行客户端和服务器端通信。

$.get('/path/to/file', kakakSisa: 10, function(data, textStatus, xhr) {
  /*optional stuff to do after success */
});

Get variable in php using $_GET['kakakSisa']; 使用$_GET['kakakSisa'];在php中获取变量$_GET['kakakSisa'];

You can't use javascript variable in php code. 您不能在php代码中使用javascript变量。 Php code run's on the serverside and javascript code runs in the client side (If you are not using Node.js). php代码在服务器端运行,而javascript代码在客户端运行(如果您未使用Node.js)。 You can't ask the browser to run php code. 您不能要求浏览器运行php代码。

Your variable loc will have a value only when the code reaches the browser. 仅当代码到达浏览器时,变量loc才会具有值。

If you want to get some value from server and combine it with javascript variables then do the following. 如果要从服务器获取一些值并将其与javascript变量组合,请执行以下操作。

Use an ajax request and send the desired values to server. 使用ajax请求并将所需的值发送到服务器。 The server will return with a response. 服务器将返回响应。 Use that response text and store it in your action variable. 使用该响应文本并将其存储在您的操作变量中。

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

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