如何将 JavaScript 变量值转换为 PHP 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11482418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert JavaScript variable value into PHP variable
提问by Junaid Akhtar
I actually need to get count of HTML table rows (<tr>
). Using JavaScript I have easily got the count of the table rows but now I actually want to use that count in a PHP variable which I am unable to do. Please kindly help me.
我实际上需要获取 HTML 表格行数 ( <tr>
)。使用 JavaScript 我很容易得到表行的计数,但现在我实际上想在我无法做到的 PHP 变量中使用该计数。请帮助我。
回答by jfriend00
Your javascript is in the browser. Your PHP is on the server. To get this count into your PHP on the server, you have several options:
您的 javascript 在浏览器中。您的 PHP 在服务器上。要将此计数计入服务器上的 PHP,您有多种选择:
- You can make an ajax call to the server and pass the table row count from the browser to your server.
- If you are requesting a new page, you can encode the row count in the URL with a URL parameter
?rowCnt=49
on the end of the URL which you would parse out of the URL in your PHP. - If you are just making a straight request of the server which will load a new page, you can also do a Post and send the data with the post (similar to the previous option which is a GET instead of a POST).
- If the row count is just in the HTML that was originally generated by your PHP on the server, then you can use some server-side logic to calculate what that rowcnt is using the same logic that generated the page in the first place.
- 您可以对服务器进行 ajax 调用,并将表格行数从浏览器传递到您的服务器。
- 如果您正在请求一个新页面,您可以使用 URL
?rowCnt=49
末尾的URL 参数对URL 中的行数进行编码,您将从 PHP 中的 URL 中解析出该参数。 - 如果您只是直接向将加载新页面的服务器发出请求,您还可以执行 Post 并通过 post 发送数据(类似于上一个选项,即 GET 而不是 POST)。
- 如果行数只是在最初由服务器上的 PHP 生成的 HTML 中,那么您可以使用一些服务器端逻辑来计算该 rowcnt 使用的逻辑与最初生成页面的逻辑相同。
回答by Sudarshan G Hegde
This will convert js variable to php variable and php variable to js variable
这会将 js 变量转换为 php 变量,将 php 变量转换为 js 变量
<script>
function jstophp(){
var javavar=document.getElementById("text").value;
document.getElementById("rslt").innerHTML="<?php
$phpvar='"+javavar+"';
echo $phpvar;?>";
}
function phptojs(){
var javavar2 = "<?php
$phpvar2="I am php variable value";
echo $phpvar2;
?>";
alert(javavar2);
}
</script>
<body>
<div id="rslt">
</div>
<input type="text" id="text" />
<button onClick="jstophp()" >Convert js to php</button>
<button onClick="phptojs()">Convert php to js</button>
PHP variable will appear here:
<div id="rslt2">
</div>
</body>
回答by Vikas kumar
Best Example convert JavaScript value to PHP:
最佳示例将 JavaScript 值转换为 PHP:
JavaScript
JavaScript
<script type="text/javascript">
var ja =50;
</script>
PHP
PHP
<?php
$dummy = "<script>document.write(ja)</script>";
echo $dummy;
?>
回答by user961954
I think that you are trying to read the row count in PHP. So you may want to try using a input field for e.g.
我认为您正在尝试读取 PHP 中的行数。所以你可能想尝试使用输入字段,例如
Add this to HTML
将此添加到 HTML
<input type="hidden" id="rowCount"/>
in JS code
在 JS 代码中
<script>
var objInputFieldRowCount = docuement.getElemendbyId("rowCount");
var jsRowCount = table.getElementsByTagName("TR").length;
objInputFieldRowCount.value = jsRowCount;
</script>
回答by ZosoOfZep
I have include a simple object that allows you to send data to a PHP script. The POST in synchronous to keep it simple. I have also includes a simple php script that will read the data.
我包含了一个简单的对象,它允许您将数据发送到 PHP 脚本。POST 同步以保持简单。我还包括一个简单的 php 脚本,它将读取数据。
var phpSend = phpSend ||
{
sent: false,
xmlhttp: null,
xmlhttpstatus:null ,
// You would call this function when you need to send your data
sendInfo: function()
{
// Your Data
var tableData = "&tableRowCount=" + yourTableRowCount ;
// The url where the php file is situated
var httpstring = 'http://www.mywebsite.com/php/table.php' ;
phpSend.ContactXMLHttpRequest(httpstring,tableData) ;
// Check to see if http request for table was OK
if(phpSend.xmlhttpstatus == true)
{
// Do something here
}
},
ContactXMLHttpRequest: function(url,data)
{
if (phpSend.xmlhttp)
{
phpSend.xmlhttp.onreadystatechange=phpSend.stateChanged;
phpSend.xmlhttp.open("POST",url,false);
phpSend.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
phpSend.xmlhttp.send(data);
}
},
stateChanged: function()
{
if(phpSend.xmlhttp.readyState==4)
{
if (phpSend.xmlhttp.status==200)
{
phpSend.xmlhttpstatus = true ;
}
else
{
phpSend.xmlhttpstatus = false ;
}
}
},
InitXMLHttpRequest: function()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
alert ('Your browser does not support XMLHTTP!');
return null;
}
} ;
//-----------------------------------------------------------------------------------------------------------------------
//
// Run on load
//
//-----------------------------------------------------------------------------------------------------------------------
(function()
{
phpSend.xmlhttp=phpSend.InitXMLHttpRequest();
})() ;
//-----------------------------------------------------------------------------------------------------------------------
//
// PHP code that will be sitting on your server (for this example, 'http://www.mywebsite.com/php' and called 'table.php')
//
//-----------------------------------------------------------------------------------------------------------------------
<?php
// Pick up the count and do what you need to do
$count = $_POST["tableRowCount"];
?>