JQuery Ajax Updating MySQL Database, But Not Running Success Function

Posted by myrmidon16 on Stack Overflow See other posts from Stack Overflow or by myrmidon16
Published on 2012-04-10T05:24:16Z Indexed on 2012/04/10 5:29 UTC
Read the original article Hit count: 194

Filed under:
|
|
|
|

I am currently using the JQuery ajax function to call an exterior PHP file, in which I select and add data in a database. Once this is done, I run a success function in JavaScript. What's weird is that the database is updating successfully when ajax is called, however the success function is not running. Here is my code:

<!DOCTYPE html>
<head>
<script type="text/javascript" src="jquery-1.6.4.js"></script>
</head>
<body>
<div onclick="addtask();" style="width:400px; height:200px; background:#000000;"></div>
<script>
function addtask() {
var tid = (Math.floor(Math.random() * 3)) + 1;
var tsk = (Math.floor(Math.random() * 10)) + 1;

if(tsk !== 1) {
    $.ajax({
    type: "POST",
    url: "taskcheck.php",
    dataType: "json",
    data: {taskid:tid},
    success: function(task) {alert(task.name);}
    });
}
}

</script>
</body>
</html>

And the PHP file:

session_start(); 

$connect = mysql_connect('x', 'x', 'x') or die('Not Connecting'); 
mysql_select_db('x') or die ('No Database Selected'); 

$task = $_REQUEST['taskid']; 
$uid = $_SESSION['user_id']; 

$q = "SELECT task_id, taskname FROM tasks WHERE task_id=" .$task. " LIMIT 1"; 
$gettask = mysql_fetch_assoc(mysql_query($q)); 

$q = "INSERT INTO user_tasks (ut_id, user_id, task_id, taskstatus, taskactive) VALUES (null, " .$uid. ", '{$gettask['task_id']}', 0, 1)"; 
$puttask = mysql_fetch_assoc(mysql_query($q)); 

$json = array( 
    "name" => $gettask['taskname'] 
); 

$output = json_encode($json); 
echo $output; 

Let me know if you have any questions or comments, thanks.

© Stack Overflow or respective owner

Related posts about php

Related posts about JavaScript