简体   繁体   中英

JS - PHP JSON error from array php while using path

I have an error in firebug :

SyntaxError: missing ] after element list
var o99_images = [ C:\Documents and Settings\XXXX\Desktop\xampp\htdocs\www.s
--------------------^

The var in question is an array of paths (local) . an example array looks like so :

var o99_images = [C:\Documents and Settings\XX\XX\xampp\htdocs\XX\XX\uploads\images_to_post\2000501.freddy.lulu.milano.jpg,C:\Documents and Settings\XX\XX\xampp\htdocs\XX\XX\uploads\images_to_post\4147941_460s.jpg,C:\Documents and Settings\XX\XX\xampp\htdocs\XX\XX\uploads\images_to_post\732331_26687367.jpg];

I am by no means a javascript master , but I suspect that the problem is the path itself as a string (maybe the : ? ) becasue when I do the same with an array of number only (eg var o99_images = [12,14,15,18] there is no problem..

This is the JSON function :

function O99_Do_Action( id ) {
                $.ajax({
                    type: 'POST',
                    url: ajaxurl,
                    data: { action: "ajaxresponse", id: id },
                    success: function( response ) {
                        if ( response.success ) {
                            O99_Do_ActionUpdateStatus( id, true, response );
                        }
                        else {
                            O99_Do_ActionUpdateStatus( id, false, response );
                        }

                        if ( o99_images.length && o99_continue ) {
                            O99_Do_Action( o99_images.shift() );
                        }
                        else {
                            O99_Do_ActionFinishUp();
                        }
                    },
                    error: function( response ) {
                        O99_Do_ActionUpdateStatus( id, false, response );

                        if ( o99_images.length && o99_continue ) {
                            O99_Do_Action( o99_images.shift() );
                        } 
                        else {
                            O99_Do_ActionFinishUp();
                        }
                    }
                });
            }

and before that , the vars :

jQuery(document).ready(function($){
            var i;
            var o99_images = [<?php echo $ids; ?>];
            var o99_total = o99_images.length;
            var o99_count = 1;

                        //... continue vars and function

as you can see , the var in question is being generated in php. (implode() string with , )

So is the path string itself is the problem ? and if so , how can one pass a path to such a var ??

RESOLVED : thanks to @Aatif Farooq that got me on the right track, the answer was json_encode() that handles both the quores " and the escape problem // ..

while I searched for the addslashes() php function, I found the json_encode() .. and in the meantime, @slashingweapon got it right ..

echo json_encode($ids);

Path is actually a string, so you should wrap the path in single or double quotes. Like:

'c://any-path/to/file.php'

In your case it should be:

var o99_images = ['C:\Documents and Settings\XX\XX\xampp\htdocs\XX\XX\uploads\images_to_post\2000501.freddy.lulu.milano.jpg','C:\Documents and Settings\XX\XX\xampp\htdocs\XX\XX\uploads\images_to_post\4147941_460s.jpg','C:\Documents and Settings\XX\XX\xampp\htdocs\XX\XX\uploads\images_to_post\732331_26687367.jpg'];

The text /string data must be added between simple or double quotes, like this:

var o99_images = ['C:\Documents and Settings\XX\XX\xampp\htdocs\XX\XX\uploads\images_to_post\2000501.freddy.lulu.milano.jpg', 'C:\Documents and Settings\XX\XX\xampp\htdocs\XX\XX\uploads\images_to_post\4147941_460s.jpg,C:\Documents and Settings\XX\XX\xampp\htdocs\XX\XX\uploads\images_to_post\732331_26687367.jpg'];

It works with numbers [5, 88, 8] because the numbers not need to be added within quotes.

Your problem is in how you are encoding the data. PHP provides a very useful function, json_encode() , which you should use whenever you want to convert PHP data into Javascript literals.

For example, if you want to convert a string into a Javascript string:

php > $x = 'C://some/series/of/path/segments';
php > echo json_encode($x);
"C:\/\/some\/series\/of\/path\/segments"

And if you want to convert an array of strings into a Javascript array of strings:

php > $x = array('a', 'b', 'c');
php > echo json_encode($x);
["a","b","c"]

You can even do this for numbers, booleans, and complex objects. Any quoting, escaping, and encoding is taken care of. This trick will work for any PHP data that can be serialized. It won't work on cyclical objects or resources, but everything else is fair game.

So you should probably not implode() your data, and instead leave it as an array. Then code should be:

jQuery(document).ready(function($){
    var i;
    var o99_images = <?php echo json_encode($ids); ?>;
    var o99_total = o99_images.length;
    var o99_count = 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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