How To Convert Javascript Variable Value Into Php Variable
I actually need to get count of HTML table rows (). Using JavaScript I have easily got the count of the table rows but now I actually want to use that count in a PHP vari
Solution 1:
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:
- 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.
Solution 2:
This will convert js variable to php variable and php variable to js variable
<script>functionjstophp(){
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><divid="rslt"></div><inputtype="text"id="text" /><buttononClick="jstophp()" >Convert js to php</button><buttononClick="phptojs()">Convert php to js</button>
PHP variable will appear here:
<divid="rslt2"></div></body>
Solution 3:
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
<inputtype="hidden"id="rowCount"/>
in JS code
<script>var objInputFieldRowCount = docuement.getElemendbyId("rowCount");
var jsRowCount = table.getElementsByTagName("TR").length;
objInputFieldRowCount.value = jsRowCount;
</script>
Solution 4:
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 Datavar tableData = "&tableRowCount=" + yourTableRowCount ;
// The url where the php file is situatedvar httpstring = 'http://www.mywebsite.com/php/table.php' ;
phpSend.ContactXMLHttpRequest(httpstring,tableData) ;
// Check to see if http request for table was OKif(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, Safarireturnnew XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5returnnew ActiveXObject("Microsoft.XMLHTTP");
}
alert ('Your browser does not support XMLHTTP!');
returnnull;
}
} ;
//-----------------------------------------------------------------------------------------------------------------------//// 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"];
?>
Solution 5:
Best Example convert JavaScript value to PHP:
JavaScript
<scripttype="text/javascript">var ja =50;
</script>
PHP
<?php$dummy = "<script>document.write(ja)</script>";
echo$dummy;
?>
Post a Comment for "How To Convert Javascript Variable Value Into Php Variable"