最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What is wrong with this javascript and php code - Stack Overflow

programmeradmin1浏览0评论

I've been trying to debug this for an hour:

<script type="text/javascript">   
function initialize() {     
    alert('test');
    var latlngarr = new Array();
    var titlearr = new Array();
    <?php
        echo "latlngarr.length=".$response->total.";";
        echo "titlearr.length=".$response->total.";";
        for ($i=0;$i<$response->total;$i++){
            echo "latlngarr[".$i."] = new google.maps.LatLng(".$response->businesses[$i]->location->coordinate->latitude.",".$response->businesses[$i]->location->coordinate->longitude.");";
            echo "titlearr[".$i."] = \"".$response->businesses[$i]->name."\";";
        }
    ?>

    var myOptions = {       
        zoom: 10,       
        center: latlngarr[0],       
        mapTypeId: google.maps.MapTypeId.ROADMAP     
    };     
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);   
    var markerarr = new Array(titlearr.length);
    for(var i=0;i<markerarr.length;i++)
        markerarr[i] = new google.maps.Marker({position: latlngarr[i], map: map, title:titlearr[i]});

}  
</script> 

<body onload="initialize()">

The error that I got is:

Uncaught SyntaxError: Unexpected token <

and

Uncaught ReferenceError: initialize is not defined

When I remove that block of PHP code, it doesn't give me that error. Why?

UPDATE:

Here's where the error is:

$(".saved").live('click', function() {
    var $btn = $(this);
    $.post("update.php", {uid: my_uid, save: "no", mid: "<?php echo $mid; ?>"}, function(){
        setTimeout(function(){
            $btn.replaceWith('<a class="save action_btn" onclick="return false;">Save</a>');   
        }, 100); //this is line 76
    });
});

Here's the generated JS from that initialize function:

function initialize() {     
    alert('test');
    var latlngarr = new Array();
    var titlearr = new Array();

    <br />
    <font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
    <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined property: stdClass::$total in C:\wamp\www\movie.php on line <i>89</i></th></tr>
    <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
    <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
    <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0009</td><td bgcolor='#eeeeec' align='right'>711160</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\movie.php' bgcolor='#eeeeec'>..\movie.php<b>:</b>0</td></tr>
    </table></font>
    latlngarr.length=;<br />
    <font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
    <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined property: stdClass::$total in C:\wamp\www\movie.php on line <i>90</i></th></tr>
    <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
    <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
    <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0009</td><td bgcolor='#eeeeec' align='right'>711160</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\movie.php' bgcolor='#eeeeec'>..\movie.php<b>:</b>0</td></tr>
    </table></font>
    titlearr.length=;<br />
    <font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
    <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined property: stdClass::$total in C:\wamp\www\movie.php on line <i>91</i></th></tr>
    <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
    <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
    <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0009</td><td bgcolor='#eeeeec' align='right'>711160</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\movie.php' bgcolor='#eeeeec'>..\movie.php<b>:</b>0</td></tr>
    </table></font>

    var myOptions = {       
        zoom: 10,       
        center: latlngarr[0],       
        mapTypeId: google.maps.MapTypeId.ROADMAP     
    };     
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);   
    var markerarr = new Array(titlearr.length);
    for(var i=0;i<markerarr.length;i++)
        markerarr[i] = new google.maps.Marker({position: latlngarr[i], map: map, title:titlearr[i]});
}  

I've been trying to debug this for an hour:

<script type="text/javascript">   
function initialize() {     
    alert('test');
    var latlngarr = new Array();
    var titlearr = new Array();
    <?php
        echo "latlngarr.length=".$response->total.";";
        echo "titlearr.length=".$response->total.";";
        for ($i=0;$i<$response->total;$i++){
            echo "latlngarr[".$i."] = new google.maps.LatLng(".$response->businesses[$i]->location->coordinate->latitude.",".$response->businesses[$i]->location->coordinate->longitude.");";
            echo "titlearr[".$i."] = \"".$response->businesses[$i]->name."\";";
        }
    ?>

    var myOptions = {       
        zoom: 10,       
        center: latlngarr[0],       
        mapTypeId: google.maps.MapTypeId.ROADMAP     
    };     
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);   
    var markerarr = new Array(titlearr.length);
    for(var i=0;i<markerarr.length;i++)
        markerarr[i] = new google.maps.Marker({position: latlngarr[i], map: map, title:titlearr[i]});

}  
</script> 

<body onload="initialize()">

The error that I got is:

Uncaught SyntaxError: Unexpected token <

and

Uncaught ReferenceError: initialize is not defined

When I remove that block of PHP code, it doesn't give me that error. Why?

UPDATE:

Here's where the error is:

$(".saved").live('click', function() {
    var $btn = $(this);
    $.post("update.php", {uid: my_uid, save: "no", mid: "<?php echo $mid; ?>"}, function(){
        setTimeout(function(){
            $btn.replaceWith('<a class="save action_btn" onclick="return false;">Save</a>');   
        }, 100); //this is line 76
    });
});

Here's the generated JS from that initialize function:

function initialize() {     
    alert('test');
    var latlngarr = new Array();
    var titlearr = new Array();

    <br />
    <font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
    <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined property: stdClass::$total in C:\wamp\www\movie.php on line <i>89</i></th></tr>
    <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
    <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
    <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0009</td><td bgcolor='#eeeeec' align='right'>711160</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\movie.php' bgcolor='#eeeeec'>..\movie.php<b>:</b>0</td></tr>
    </table></font>
    latlngarr.length=;<br />
    <font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
    <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined property: stdClass::$total in C:\wamp\www\movie.php on line <i>90</i></th></tr>
    <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
    <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
    <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0009</td><td bgcolor='#eeeeec' align='right'>711160</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\movie.php' bgcolor='#eeeeec'>..\movie.php<b>:</b>0</td></tr>
    </table></font>
    titlearr.length=;<br />
    <font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
    <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined property: stdClass::$total in C:\wamp\www\movie.php on line <i>91</i></th></tr>
    <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
    <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
    <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0009</td><td bgcolor='#eeeeec' align='right'>711160</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\movie.php' bgcolor='#eeeeec'>..\movie.php<b>:</b>0</td></tr>
    </table></font>

    var myOptions = {       
        zoom: 10,       
        center: latlngarr[0],       
        mapTypeId: google.maps.MapTypeId.ROADMAP     
    };     
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);   
    var markerarr = new Array(titlearr.length);
    for(var i=0;i<markerarr.length;i++)
        markerarr[i] = new google.maps.Marker({position: latlngarr[i], map: map, title:titlearr[i]});
}  
Share Improve this question edited May 30, 2017 at 13:46 Mantas Čekanauskas 2,2386 gold badges26 silver badges48 bronze badges asked Apr 29, 2011 at 22:26 aditadit 33.7k72 gold badges235 silver badges380 bronze badges 5
  • 4 Show the generated, final JavaScript as it shows in the browser. And show the line that throws the error – Pekka Commented Apr 29, 2011 at 22:27
  • But if he does that then he'll see what the problem is and won't need to ask the question! That's no fun! – mVChr Commented Apr 29, 2011 at 22:30
  • Easy to debug, just look at the generated javascript – gd1 Commented Apr 29, 2011 at 22:34
  • Comment post-update: Your code is trying to tell you that your PHP is wrong in the movie.php. ;) – dee-see Commented Apr 29, 2011 at 22:34
  • Resolve your PHP errors and then the munity can look at your javascript. – afuzzyllama Commented Apr 29, 2011 at 22:37
Add a ment  | 

4 Answers 4

Reset to default 4

Well, <br /> is no javascript so that would cause an error.

As would the rest of the html echoed inside the javascript block...

As you can see it´s basically one big error message, ing from php.

Its because its showing the data as text in place of html

Its basically a php errors thrown out by javascript.

Your problem is that you're generating HTML into JavaScript. Doing this causes a syntax error at the first < as indicated by the error message. It looks like your PHP is ONLY generating HTML. If you're trying to do this, put it outside your <script> tags. If you're not, you're going to need to wrap the HTML in quotations (or something) so that it doesn't cause a parse error.

Also, your $response object doesn't have a property called $total, as the error message says. Be sure that you're accessing the right properties to avoid errors.

This error occurs because there are variables that are not being used within the code, that is, variables that have not been assigned any value. Please check the variable laws that are used one by one and delete those that are not being used

发布评论

评论列表(0)

  1. 暂无评论