MAO-EYE 2013-07-14 16:20 采纳率: 100%
浏览 69

php echo JavaScript无法运行?

I have a problem where when I echo JavaScript from a PHP page to a HTML page using AJAX it won't run however, when I Inspect Element the JavaScript is there, I also echo some text and the text does show.

When I add the JavaScript manually in the HTML it works perfectly and the PHP, HTML and AJAX files are all in the same directory so there is no problem there.

I have 3 pages index.html, boo.php and ajax.js

index.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/jquery.gritter.css" />
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">google.load('jquery', '1.7.1');</script>
<script type="text/javascript" src="js/jquery.gritter.js"></script>
<script src="ajax.js"></script>
</head>
<body>

<script type="text/javascript"><!--
refreshdiv();
// --></script>

<div id="timediv"></div>

</body>
</html>

boo.php

<?php

// Fetch the data

$query = "SELECT * FROM $tablename";
$result = mysql_query($query);

// Return the results, loop through them and echo

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{

$name = $row['name']; // this equals Admin

if($name == 'Admin') {

echo $name; // this echos Admin and shows on index.html


// the javascript below doesnt
 echo "
<script type='text/javascript'>

    $(function(){



        $(document).ready( function () {

            var unique_id = $.gritter.add({
                title: 'title text',
                text: 'description text',

                image: 'icon.jpg',

                sticky: true,

                time: '',

                class_name: 'my-sticky-class'
            });



            return false;

        });


    });
</script>
";
}


}
?>

ajax.js

// Customise those settings

var seconds = 5;
var divid = "timediv";
var url = "boo.php";

////////////////////////////////
//
// Refreshing the DIV
//
////////////////////////////////

function refreshdiv(){

// The XMLHttpRequest object

var xmlHttp;
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Your browser does not support AJAX.");
return false;
}
}
}

// Timestamp for preventing IE caching the GET request

fetch_unix_timestamp = function()
{
return parseInt(new Date().getTime().toString().substring(0, 10))
}

var timestamp = fetch_unix_timestamp();
var nocacheurl = url+"?t="+timestamp;

// The code...

xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
document.getElementById(divid).innerHTML=xmlHttp.responseText;
setTimeout('refreshdiv()',seconds*1000);
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

// Start the refreshing process

var seconds;
window.onload = function startrefresh(){
setTimeout('refreshdiv()',seconds*1000);
}

What the script is suppose to do check every 5 seconds if there is if there is a username in the database called Admin then show a gritter notification if there is.

Any ideas what I can do? thanks.

  • 写回答

1条回答 默认 最新

  • weixin_33730836 2013-07-14 16:29
    关注

    reverse your string notation on the second echo statement that doesn't work. change double quote to a single qoute like this

      echo "<script type='text/javascript'>
            ...more stuff... 
            title: 'title text',
            ...more stufff...
           ";
    

    to

       echo '<script type="text/javascript">
             ...more stuff..
            title: "title text",
            ...more stuff...
            ';
    

    why this works is because double quotes will look for variables inside the string and single quotes doesn't. so when you have $(document) this inside of the double quote php thinks its a var

    EDIT: since the echo statement is working

    the use of innerHTML method doesn't evaluate scripts it only evaluates html tags. so if you want the response to evaluate the in coming echo statement use the jQuery function append()

    that being said change

    document.getElementById(divid).innerHTML=xmlHttp.responseText;
    

    to

    $('#divid').append(xmlHttp.responseText);
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!