Home » Php » php – JQuery syntax error found in firebug but not in my code

php – JQuery syntax error found in firebug but not in my code

Posted by: admin July 12, 2020 Leave a comment

Questions:

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 error
Source code on execution

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

How to&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

Answer:

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’].

Answer:

Debug the code step by step. First put a else statement in your ajax.php file and check where the execution goes. If it goes in if statement then put a die() function on each line to check where error come. One possible error in your code is that you are sending tag in your ajax function and you are getting $_POST['taglist']. #taglist trigger this ajax function, its value is not posted. Only the value that you give in data attribute is posted. So change your if condition from

From

if(isset($_POST['taglist'])){

To

if(isset($_POST['tag'])){

This may be the reason. Else is good.

Answer:

I think you might have a space over there .. Remove the space after });​