我实际上需要计算HTML表行(<tr>
)。使用JavaScript我很容易得到表行的计数,但现在我实际上想在PHP变量中使用该计数,我无法做到。请帮助我。
答案 0 :(得分:3)
您的javascript在浏览器中。您的PHP在服务器上。要在服务器上对PHP进行计数,您有以下几种选择:
?rowCnt=49
对URL中的行计数进行编码,您可以从PHP中解析该URL。答案 1 :(得分:3)
这会将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>
答案 2 :(得分:0)
我认为您正在尝试读取PHP中的行数。因此,您可能想尝试使用输入字段,例如
将此添加到HTML
<input type="hidden" id="rowCount"/>
在JS代码中
<script>
var objInputFieldRowCount = docuement.getElemendbyId("rowCount");
var jsRowCount = table.getElementsByTagName("TR").length;
objInputFieldRowCount.value = jsRowCount;
</script>
答案 3 :(得分:0)
我已经包含了一个简单的对象,允许您将数据发送到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"];
?>
答案 4 :(得分:0)
最佳示例将JavaScript值转换为PHP:
的JavaScript
<script type="text/javascript">
var ja =50;
</script>
PHP
<?php
$dummy = "<script>document.write(ja)</script>";
echo $dummy;
?>