I have a page where I use load
to load a dialog box. From that dialog box, I have jQuery in there that posts an ajax request to a PHP script. This all works fine. From the PHP I echo out the result and attempt to catch that on the done
callback from this loaded dialog - but its not working. It updates the database, but I can't catch the echo. This works elsewhere in my site, but I can't get it working here. I think it might have something to do with the fact that this is coming from a 'loaded' dialog? Any help would be appreciated.
Code on original page initiating the load...
$('#edit_cats').click(function() {
$('#overlay').show();
$("#loader4").show().center();
$('#dialogLoad').show().load('../../dialog/categories.php', {'site_user':'<?=$user_id?>','id':'<?=$blog['id']?>'});
});
AJAX code in the loaded dialog
$('.cat_input').bind('blur keyup', function(e) {
if(e.type === 'keyup' && e.keyCode !== 10 && e.keyCode !== 13) return;
$(this).hide();
var cat_title = $(this).val();
var cat_id = $('.cat_selected').attr('id');
$.ajax({
type: "POST",
url: "../../system/process-cat-update.php",
dataType: "json",
data: { 'title':cat_title, 'cat_id':cat_id}
}).done(function(data) {
$('.cat_selected').find('.cat_title').html(data).show();
});
});
PHP code
<?php
include ('connect.php');
mysql_query("UPDATE blog SET category='".$_POST['title']."' WHERE id='".$_POST['cat_id']."' ");
echo $_POST['title'];
?>
You aren't sending json in your response so dataType: "json"
is incorrect, removing the dataType
property should fix your problem. Also you aren't sanitizing you input so you are open to sql injection.