简体   繁体   English

innerHTML和PHP

[英]innerHTML and PHP

I want to do some innerHTML replacements, but using PHP includes, I dont get how. 我想做一些innerHTML替换,但是使用PHP包括在内,我不知道如何做。 I have done innerHTML replacements before, just not with PHP. 我以前做过innerHTML替换,只是没有使用PHP。

I have: 我有:

var xmlHttp

function DisplayAerialProductListing()
{ 
    var strStartCode="<p></p>";
    document.getElementById("txtData").innerHTML= strStartCode;

    var code="";
    code = code + "<?php include 'newpage.inc'; ?>";
    document.getElementById("txtData").innerHTML= code;
}

I have the 'txtData' Div as: 我有'txtData'Div作为:

initially and I want to replace with code from the .inc I mention. 最初,我想替换为我提到的.inc中的代码。 I use .inc to separate out pieces of the site and to make like easier for the designer so they dont go breaking stuff! 我使用.inc来分隔网站的各个部分,并使设计师更容易设计,这样他们就不会破坏东西!

In the .inc I just have HELLO for now. 在.inc中,我现在只有HELLO。

It doesn't work. 没用 What is the trick? 诀窍是什么?

PHP is processed server-side; PHP是在服务器端处理的; Javascript is processed client-side. Javascript是在客户端处理的。 You can't insert PHP code via Javascript because you've already left the server. 您无法通过Java脚本插入PHP代码,因为您已经离开了服务器。 Normally what you'd do is use AJAX to run the PHP code on the server and then insert the results dynamically into the page. 通常,您要做的是使用AJAX在服务器上运行PHP代码,然后将结果动态插入页面中。

Using jQuery , you can nail that almost effortlessly: 使用jQuery ,您几乎可以毫不费力地确定:

<p id="myParagraph"></p>

<script>
//when the DOM is ready
$(document).ready(function() {
    //replace contents of p id="myParagraph" with output of specified script
    $('#myParagraph').load('myPage.php');
});
</script>

Still, make sure you understand the difference between client and server as per @Dav's answer. 尽管如此,请确保您了解@Dav的答案所对应的客户端和服务器之间的区别。

See http://docs.jquery.com/Ajax 参见http://docs.jquery.com/Ajax

<script style="text/javascript" language="javascript">
<!--
function check_country()
{
    var sel_country = document.getElementById("country");
    if(sel_country.value == "US")
    {
        <?php
        $query = "SELECT stateName, stateAbbrev
                FROM states
                WHERE stateEnabled = '1'
                ORDER BY stateName ASC";
        $result = mysql_query($query);

        $prov = "<select name=\"state\" id=\"state\" class=\"widthed\">";

        while($row = mysql_fetch_row($result))
        {
            $prov .= "<option value=\"$row[1]\">$row[0]</option>";
        }
        $prov .= "</select>";
        ?>

document.getElementById("tab1").rows[2].cells[3].innerHTML = "*State:";
document.getElementById("tab1").rows[3].cells[0].innerHTML = "*Zip Code:";
document.getElementById("tab1").rows[2].cells[4].innerHTML = <?php echo $prov; ?>;
    }
}
-->
</script>

That will work as long as your JavaScript file is parsed by PHP, ie. 只要PHP对您的JavaScript文件进行了解析,该文件即会起作用。 with an .htaccess that says SetHandler application/x-httpd-php .js . 使用.htaccess表示SetHandler application/x-httpd-php .js You'll want to escape the text inside the include, so it may be better to use PHP's file_get_contents() and addslashes(). 您可能希望转义include中的文本,因此最好使用PHP的file_get_contents()和addlashes()。 Here's an example of how to super sanitize a string in PHP that is destined for JavaScript: http://sixohthree.com/241/escaping 这是一个示例,说明如何在PHP中超级清理以JavaScript为目标的字符串: http : //sixohthree.com/241/escaping

An alternate solution would be to load the page content via XMLHttpRequest. 另一种解决方案是通过XMLHttpRequest加载页面内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM