James Pitt
James Pitt

Reputation: 441

JQuery ajax request - echo will not output result

I've never encountered anything this strange before. I raise an Ajax request using jQuery to get a value from mysql.

$.ajax({
    url: 'ajaxClassificationCheck.php?category='+category,
    cache: false,
    success: function(returnClass)
    {
        $("#classification").val(returnClass);
}});

and the php...

include_once ('functions.php');
connectdb();

$category = $_GET['category'];

$q = "SELECT classification_id FROM sch_category WHERE category_id = $category";

$r = mysql_query($q) or die (mysql_error());
$row = mysql_fetch_assoc($r);
$classification = $row['classification_id'];
echo $classification;

The result in network capture shows the output from the ajax request which is empty.

If I change:

echo $classification;

to

echo "result is $classification";

outputs:

result is 2

It also works with:

echo "a".$classification;

outputs:

a2

Is there a minimum character limit on what can be returned via Ajax?

EDIT

Thanks for all your suggestions

@RocketHazmat - tried dataType: 'text', still not returning single integer. Definitely using echo $classification;

@dualed - I put the url into the browser window and I get the correct integer response so it is something wrong with my ajax.

Any other ideas?

Upvotes: 3

Views: 2976

Answers (1)

jonas
jonas

Reputation: 126

In $.ajax you have to specify which content-type is expected. In your case it is 'html', so

$.ajax({
  ...
  dataType : 'html'
});

But instead of this i would recommend to you to use dataType 'json' and change your php script's last line to json_encode($classification). If you do not specify dataType, jQuery will make an intelligent guess and this can result in strange behaviour.

And please read something about SQL Injection.

Upvotes: 2

Related Questions