Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery syntax error found in firebug but not in my code

In a PHP page I have a drop-down list that triggers the jQuery change function and does a POST with AJAX in order to populate a second drop-down list in the page.

It should all be pretty simple, however Firebug throws an syntax error for a '?' that appears in my code at run-time which isn't actually in my code.

This is my javascript code:

$(document).ready(function() {
    $('#taglist').change(function(){
        $.ajax({
            type: "POST",
            url: "../includes/ajax.php",
            data: "taglist=" + $(this).find("option:selected").attr('value'),
            dataType: 'json',
            success: function(response, textStatus, jqXHR) {
                $("#catlist").html(response.catlist);
            },
            error: function (xhr, textStatus, errorThrown) {
                $("#catlist").html(xhr.responseText);
            }
        });​
    });
});

Following, is the PHP code that executes in ajax.php:

if(isset($_POST['taglist'])){
    $catlist = '<select name="cat_id[]" size="5" multiple id="cat_id[]">';
    $catlist .= fillselecteditmultiple(0, 0, $_POST['taglist']);
    $catlist .= '</select>';

    echo json_encode(array("status"=>"success", "catlist" => $catlist));
}

fillselecteditmultiple() outputs the options for the populated highlighting those that should be pre-selected. It works fine as I use in other pages without a problem. To make sure it wasn't an error being thrown from the function itself, I even tried changing the function to output a simple $catlist='abc' string as a response, still the same error.

The strange part is that Firebug throws an error at the last 2 lines of the javascript code like you can see in the image attached:

Firebug errorSource code on execution

What could be causing cause the '?' to appear in my code?

like image 881
bikey77 Avatar asked Dec 05 '12 07:12

bikey77


2 Answers

Just check your encoding. When you see "strange" symbols in the end of a line, that can be caused mostly with encoding troubles.

And in addition, common advice:

When a program doesn't know how to represent a unicode code point, it prints a question mark inside a square (I suppose you have seen this before), or in case it cannot, a simple question mark. An unexpected question mark always makes a developer suspect of encoding.
(c) Áxel

like image 185
StasGrin Avatar answered Nov 15 '22 17:11

StasGrin


you send in javascript code this data,

data: "tag=" + $(this).find("option:selected").attr('value')

but in php file you try to get $_POST['taglist'],

variable names are not the same.

try to change data: "taglist=...." or $_POST['tag'].

like image 24
saitakyuz Avatar answered Nov 15 '22 16:11

saitakyuz