1

我实际上需要获取 HTML 表格行数(<tr>)。使用 JavaScript 我很容易得到表行的计数,但现在我实际上想在我无法做到的 PHP 变量中使用该计数。请帮助我。

4

5 回答 5

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>
于 2013-03-15T19:08:38.287 回答
3

您的 javascript 在浏览器中。您的 PHP 在服务器上。要将此计数计入服务器上的 PHP,您有多种选择:

  1. 您可以对服务器进行 ajax 调用,并将表格行数从浏览器传递到您的服务器。
  2. 如果您正在请求一个新页面,您可以使用 URL 末尾的 URL 参数对 URL 中的行数进行编码?rowCnt=49,您将在 PHP 中解析出 URL。
  3. 如果您只是直接向服务器发出将加载新页面的请求,您还可以执行 Post 并通过 post 发送数据(类似于前面的选项,即 GET 而不是 POST)。
  4. 如果行数只是在最初由服务器上的 PHP 生成的 HTML 中,那么您可以使用一些服务器端逻辑来计算该 rowcnt 使用最初生成页面的相同逻辑。
于 2012-07-14T09:11:34.390 回答
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>
于 2012-07-14T09:19:01.457 回答
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"]; 
?>
于 2012-07-14T11:06:46.007 回答
0

将 JavaScript 值转换为 PHP 的最佳示例:

JavaScript

<script type="text/javascript">
    var ja =50;
</script>

PHP

<?php 
    $dummy = "<script>document.write(ja)</script>";
    echo $dummy; 
?>
于 2016-01-22T11:48:52.053 回答