1

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.

3
  • JavaScript runs in the browser, on the client. PHP runs on the server. I'm not sure what you're trying to achieve. You can use AJAX to send data from JavaScript to the server. Commented Jul 14, 2012 at 9:09
  • i want to store values of html table into sql database Commented Jul 14, 2012 at 9:10
  • Read up on AJAX. You need to send your data to a PHP script on the server. You cant't just "convert" a JavaScript variable to a PHP variable. Commented Jul 14, 2012 at 9:11

5 Answers 5

3

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.
Sign up to request clarification or add additional context in comments.

3 Comments

Send me ajax code for connection between Database or refer link
@user1525293 - That isn't how this site works. We don't just send you code. I'd suggest you Google ajax and do some reading on your own and then ask much more specific questions if there's something you don't understand. There are millions of pages written about it as it's a widely used web capability. It's easiest to use if you use a library that does some of the gruntwork for you (like jQuery or many others).
Too bad comments can't be downvoted. (In reference to OP's comment)
3

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>

1 Comment

What if I just want jstophp() to return a PHP variable which I can then use in my HTML code ? How do I do that ?
0

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>

2 Comments

not working as i am calling like this $hidden = $_POST['hidden'];
try doing this $rowCount= $_POST['rowCount'];
0

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"]; 
?>

Comments

0

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; 
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.