JQuery syntax error found in firebug but not in my code

On the PHP page, I have a drop-down list that runs the jQuery change function and does a POST with AJAX to populate the second drop-down list on the page.

Everything should be pretty simple, however Firebug throws a syntax error for '?' which appears in my code at runtime, which is not 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); } });​ }); }); 

The following is the PHP code that runs 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 () displays the fill selection parameters that should be preselected. It works fine as I use on other pages without problems. To make sure that no error was selected from the function itself, I even tried to change the function to output the string $ catlist = 'abc' as an answer, still the same error.

The strange part is that Firebug throws an error in the last two lines of javascript code, as you can see in the attached image:

Firebug errorSource code on execution

What can cause the cause ?? appear in my code?

+8
jquery post ajax php
source share
4 answers

Just check your encoding. When you see “weird” characters at the end of a line, this can be mainly caused by encoding problems.

And, in addition, general advice:

When the program does not know how to represent the Unicode code point, it prints a question mark inside the square (I suppose you saw this before), or in case this is not possible, a simple question mark. An unexpected question mark always makes the suspect developer an encoding.
(c) Axel

+5
source share

you send javascript code for this data,

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

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

variable names do not match.

try changing the data: "taglist = ...." or $ _POST ['tag'].

+1
source share

Debug the code step by step. First put the else in your ajax.php file and check where the execution is performed. If it is included in the if , then put the die() function on each line to check where the error is. One possible error in your code is that you send a tag to your ajax function and get $_POST['taglist'] . #taglist runs this ajax function, its value is not published. Only the value that you specify in the data attribute is data . Therefore, change the if condition from

FROM

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

To

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

This may be the reason. Else is good.

+1
source share

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

0
source share

All Articles