Junaid Akhtar
Junaid Akhtar

Reputation: 569

How to convert JavaScript variable value into PHP variable

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

Answers (5)

Sudarshan G Hegde
Sudarshan G Hegde

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

Vikas kumar
Vikas kumar

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

ZosoOfZep
ZosoOfZep

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

jfriend00
jfriend00

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:

  1. You can make an ajax call to the server and pass the table row count from the browser to your server.
  2. 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.
  3. 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).
  4. 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.

Upvotes: 3

user961954
user961954

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

Related Questions