Reputation: 569
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.
Upvotes: 1
Views: 17189
Reputation: 73
This will convert js variable to php variable and php variable to js variable
<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>
Upvotes: 3
Reputation: 119
Best Example convert JavaScript value to PHP:
JavaScript
<script type="text/javascript">
var ja =50;
</script>
PHP
<?php
$dummy = "<script>document.write(ja)</script>";
echo $dummy;
?>
Upvotes: 0
Reputation: 51
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.
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"];
?>
Upvotes: 0
Reputation: 707218
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:
?rowCnt=49
on the end of the URL which you would parse out of the URL in your PHP.Upvotes: 3
Reputation: 3214
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.
Add this to HTML
<input type="hidden" id="rowCount"/>
in JS code
<script>
var objInputFieldRowCount = docuement.getElemendbyId("rowCount");
var jsRowCount = table.getElementsByTagName("TR").length;
objInputFieldRowCount.value = jsRowCount;
</script>
Upvotes: 0