Wednesday, March 29, 2017

Laravel : AJAX not updating Database table

What i'm trying to achieve here is whenever the user clicks on a radio button, it changes the task's status accordingly

<input type="radio" name="status"
<?php if($task->status == 'todo'){echo('checked');}?>
onchange = "change('todo')">Todo

<input type="radio" name="status"
<?php if($task->status == 'doing'){echo('checked');}?>
onchange = "change('doing')">Doing

<input type="radio" name="status"
<?php if($task->status == 'done'){echo('checked');}?>
onchange = "change('done')">Done

<script>
  function change(status){
  $.ajax({
      url: "/changeStatus.php/",
      type: "GET",
      data: { 'status': status, 'task_id': '<?php echo($task->id); ?>' },                   
  });
  }
</script>

and in my "changeStatus.php" file

<?php
    dd($_GET['status']);
    $con=mysqli_connect("127.0.0.1","root","","project_management");
    // Check connection
    if (mysqli_connect_errno()){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $status = $_GET['status'];
    $task_id = $_GET['task_id'];
    dd($status,$task_id);
    $sql = "UPDATE 'tasks' SET 'status' = '$status' WHERE 'id' = 'task_id'";

    if (!mysqli_query($con,$sql))
    {
        die('Error: ' . mysqli_error($con));
    }

    mysqli_close($con);

?>

the problem is that the Database is not updated when i click on the radio buttons, and there's no error too! any help is appreciated!



via Kevin fu

Advertisement