0

So basically I have an event and I want to change a value of that event in the database when I click it. The idea is this (this code does not work)

eventClick: function(calEvent, jsEvent, view) [<?php
                        while($row_events = mysqli_fetch_array($resultado_events)){
                            ?>
                            {
    location.href = "update_task.php?id=<?php echo $fetch['id'];?>";
},<?php
                        }
                    ?>]

Update task code

<?php
require_once 'conn.php';
if($_GET['id'] != ""){
    $id = $_GET['id'];
    $conn->query("UPDATE `task` SET `status` = 'Done' WHERE `id` = $id") or die(mysqli_errno());
    header('location: index.php');
}
?>

The code on the update task is working, cause I tested in a regular page, the code in the fullcalendar is the one that is not working. Thanks in advance.

ADyson
  • 57,178
  • 14
  • 51
  • 63
Miguel
  • 153
  • 1
  • 3
  • 10

2 Answers2

-1

The location.href url you printed is wrong.

Change

location.href = "update_task.php?id="'.$fetch['id'].';

to

location.href = "update_task.php?id=<?php echo $fetch['id'];?>";

Saji
  • 1,374
  • 1
  • 12
  • 19
  • It breaks fullcalendar. It turns into a white page. Probably a syntax error. – Miguel May 02 '19 at 14:03
  • View page source and check whether the url is correctly printed or not. Ensure the variable `$fetch['id']` is defined. – Saji May 02 '19 at 14:06
  • How do I define the fetch. – Miguel May 02 '19 at 14:08
  • Are you taking records from db and you print its value? – Saji May 02 '19 at 14:13
  • No. Only in the events. events: [ { title: '=$row_events['id'].' '.$row_events['aluno'];?>', description: '=$row_events['morada'].' '.$row_events['condutor'];?>', start: '', end: '', }, – Miguel May 02 '19 at 14:15
  • You may need to update your question with complete code. Then only we can give you good update. – Saji May 02 '19 at 14:19
  • That is the complete code of the eventClick. I didn't declare anything prior to it. – Miguel May 02 '19 at 14:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/192729/discussion-between-saji-and-miguel). – Saji May 02 '19 at 14:28
  • @Saji this can't be done with PHP - how does the PHP know which event is going to be clicked on? The concept doesn't make any sense. It has to be done with JavaScript code. See my answer for a workable solution. – ADyson May 02 '19 at 15:27
-1

You didn't actually say precisely what is going wrong, but I can see that what you're doing with your "eventClick" callback doesn't make any sense. It will try to generate a location.href command for every event in your query results. These will all be added to the JavaScript, one after another, as a series of consecutive commands. But eventClick is only defined once....so this means it will try to navigate to all your events simultaneously.

To add to this, you've put the PHP code in the wrong place, so you're ending with some invalid JavaScript. The finished JS will look something like this:

eventClick: function(calEvent, jsEvent, view)
  { location.href = "update_task.php?id=1"; }
  { location.href = "update_task.php?id=2"; }
  { location.href = "update_task.php?id=3"; }
  { location.href = "update_task.php?id=4"; }
  { location.href = "update_task.php?id=5"; }

which is clearly nonsense, and will probably generate a syntax error.

Even if you did it correctly, you'd end up with the finished JavaScript code looking like something this:

eventClick: function(calEvent, jsEvent, view) {
  location.href = "update_task.php?id=1";
  location.href = "update_task.php?id=2";
  location.href = "update_task.php?id=3";
  location.href = "update_task.php?id=4";
  location.href = "update_task.php?id=5";
}

Clearly this a) isn't useful and b) won't work anyway. You'll just always end up navigating to the first event, since that'll be the first location.href command which gets printed.

The conclusion is: you can't implement this functionality with PHP, because at the time when PHP is running and you're generating your page on the server, you have no way of knowing which event is going to be clicked on, so you can't set the ID in advance like this.

Luckily, the eventClick callback provides the calEvent parameter, containing details of the event which was clicked on. Assuming that, in your JSON event data, you supply the id of each event to fullCalendar, then you can use this value instead to build the URL of the specific event which has been clicked:

eventClick: function(calEvent, jsEvent, view) {
  location.href = "update_task.php?id=" + calEvent.id;
}
ADyson
  • 57,178
  • 14
  • 51
  • 63