English 中文(简体)
按下按钮运行一个php脚本,该脚本与javascript脚本相呼应
原标题:Pressing a button to run a php script which echos a javascript script

以下是单击按钮时运行的javascript的末尾

xmlObj.open ( GET , /ajax.php, true);
xmlObj.send (  );
}

因此,这将在根目录中执行php脚本ajax.php:

<?php
echo "<script type= text/javascript >
";
echo "var e = document.getElementById( widget_more_ );
";
echo "e.innerHTML +=  <p> <a>TEST</a> </p> ;
";
echo "</script>";
?>

这是一个javascript代码,它通过id在html文档中搜索元素,并在其中添加TEST链接。我已经在html中正常运行了这个javascript,而没有尝试通过php回显它,它很有效!但当我尝试通过php来做的时候就不会了!我做错了什么?

我之所以这样做,是因为TEST实际上是php变量中的一个字符串。

请告诉我。

谢谢

最佳回答

最好是:

ajax.php

<?php
$string =  TEST ;
echo $string;
?>

JavaScript

window.onload = function() {
    var e = document.getElementById( widget_more_ );
    xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject( Microsoft.XMLHTTP );
    xmlhttp.open( GET ,  /ajax.php , true);
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            e.innerHTML +=  <p><a>  + xmlhttp.responseText +  </a></p> ;
        }
    }
    xmlhttp.send(null);
}

我真的不明白为什么不能使用PHP变量。

问题回答

在php中编写javascript并没有太多好处,通常这只会导致不必要的代码耦合。然而,对于未来的注意事项,可以通过以下几种方式从PHP或任何其他服务器端语言定义javascript。

第一种方法是简单地从php中剥离脚本标记,并从javascript中调用eval:

ajax.php

<?php
echo "var e = document.getElementById( widget_more_ );
";
echo "e.innerHTML +=  <p> <a>TEST</a> </p> ;
";
?>

Javascript

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        //For function scope
        eval(xmlhttp.responseText);
        //For global scope
        window.eval(xmlhttp.responseText);
    }
}

另一种“稍微”更灵活的解决方案:

ajax.php

<?php
echo "function showTest(){";
echo "    var e = document.getElementById( widget_more_ );
";
echo "    e.innerHTML +=  <p> <a>TEST</a> </p> ;
";
echo "}";
?>

Javascript

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        //Now we will dynamically create your script element in the header
        //Get the header element on the page
        var head= document.getElementsByTagName( head )[0];
        //Create the new script tag
        var script= document.createElement( script );
        script.type=  text/javascript ;
        script.innerHTML = xmlhttp.responseText;
        //Append the new script to the header
        head.appendChild(script);
        showTest();
    }
}

这些示例更多的是出于学术目的而非实际目的(尽管动态加载javascript有其用途,请参阅google-ajax-apis)。这两个示例仍然会使代码严重耦合。Midas的答案是一种更恰当的方式来完成你所追求的。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签