0

i use FullCalendar with a database with the information of events, besides the normal info in the database, i have one column that is the doctor and in other is the city, so i have working my fullcalendar 5 with MVC php mysql. I made a lot of changes to my calendar and is working.

I add two select in the top of calendar, outside the calendar, and what i like is that when i change the select (ex. choose one doctor) the calendar have a refetchEvents() and of course show only the events from these doctor, and when i put in blank the select show alls events, or if i choose another doctor show me his only events.

In some point, my code is working, only that the calendar don't refresh, i mean calendar.refetchEvents(); do nothing.

these are my code in js:

sedegeneral.addEventListener("click", clicksede);
    function clicksede(){
    sedegeneral.addEventListener("change", clicksede2);
        function clicksede2(){
        const sedegeneral1 = document.getElementById('sedegeneral').value;
        const doctorseleccionado1 = document.getElementById('doctorseleccionado').value;
        //alert("Alert 1");
        //calendar.refetchEvents();
        //alert("Estamos a que el doc es: "+doctorseleccionado1+" Y la sede escogida es: "+sedegeneral1);
            const url = base_url + 'Home/listar';
            const http = new XMLHttpRequest();
            http.open("POST", url, true);
            http.send(new FormData(frmflt));
            http.onreadystatechange = function () {
                if (this.readyState == 4 && this.status == 200) {
                    console.log(this.responseText);
                    const res = JSON.parse(this.responseText);
                     Swal.fire(
                         'Avisos?',
                         res.msg,
                         res.tipo
                     )
                    if (res.estado) {
                        eventSource.remove()
                        //calendar.getEventSources()
                        calendar.addEventSource(res)
                        calendar.refetchEvents();
                        calendar.render();
                    }
                }
            }

        sedegeneral.removeEventListener("change", clicksede2);
        };
    };

and these are my controller function:

public function listar()
    {
        $doctor = $_POST['doctorselecionado'];
        //$doctor = '';
        //$doctor = 'guti';
        $sede = $_POST['sedegeneral'];
        //$sede =  '';
        //$sede =  'dentalsoftweb';
        //$sede =  'guti';

        $data = $this->model->getEventos($doctor,$sede);
        echo json_encode($data);
        die();
    }

And these are my model function:

public function getEventos($doctor,$sede)
    {
        $where_sql = ''; 
        //if(!empty($_GET['start']) && !empty($_GET['end'])){$where_sql .= " WHERE start BETWEEN '".$_GET['start']."' AND '".$_GET['end']."' ";}
        if(!empty($_GET['start']) && !empty($_GET['end'])){$where_sql .= " AND start BETWEEN '".$_GET['start']."' AND '".$_GET['end']."' ";}

        //$sql = "SELECT id,title, CONCAT(start,'T',startTime) as start, CONCAT(start,'T',endTime) as end, color, duracioncita, finalidad, resultado, observaciones, start as fechacita, startTime as horacita FROM evento $where_sql";
        $sql = "SELECT id,title, CONCAT(start,'T',startTime) as start, CONCAT(start,'T',endTime) as end, color, duracioncita, finalidad, resultado, observaciones, start as fechacita, startTime as horacita FROM evento WHERE lastuser LIKE '%$doctor%' AND lastusermodif LIKE '%$sede%' $where_sql";
        $array = array($doctor);
        return $this->selectAll($sql, $array);
    }

So when i try, i see that in my console works fine the (php ajax), so when i start the calendar show me all events, of course the select option is in blank, and when i change the select (ex. doctor), in the console show me the doctors events, but in the calendar don't reload with the new data. So in the imagen as you can see in one selecction bring all data, and in the other selection (when i change the select, choose one doctor) brings me only his events, so these are less events, but only i can see in the console, the calendar do nothing to refresh.

Console IMG

I add the images of the code, the code that i wrote before (Controller):

Controller IMG

I add the images of the code, the code that i wrote before (JS):

JS IMG

The funny thing is that, if i ignore the selects, and modify my code onlive (Controller) the calendar do refresh, so i think my problem is in my JS. i think has something to do with:

eventSource.remove()
calendar.getEventSources()
calendar.addEventSource(res)
calendar.refetchEvents();

So, any ideas, suggestions ? i will be eternal grateful. Thanks in advance !!!

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • `refetchEvents` doesn't do anything unless you define a _dynamic_ event source (e.g. a JSON URL or a callback function) which fullCalendar can activate when refetchEvents is executed. If you just pass in a list of events in an array, then refetch has nothing else to fetch when you run that. But you seem to be adding and removing event sources each time, with `calendar.addEventSource(res)` etc, so refetchEvents shouldn't be relevant or needed. What we can't see, is whether `res` contains a valid list of events or not. And also `eventSource.remove()`...where is `eventSource` defined?? – ADyson Nov 23 '22 at 21:25
  • **Warning:** Your code is vulnerable to SQL Injection attacks. You should use parameterised queries and prepared statements to help prevent attackers from compromising your database by using malicious input values. http://bobby-tables.com gives an explanation of the risks, as well as some examples of how to write your queries safely using PHP / mysqli / PDO. **Never** insert unsanitised data directly into your SQL. The way your code is written now, someone could easily steal, incorrectly change, or even delete your data. – ADyson Nov 23 '22 at 21:26
  • 1
    https://phpdelusions.net/ also contains good examples of writing safe SQL using mysqli and PDO. See also: [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) . Parameterising your queries will also greatly reduce the risk of accidental syntax errors as a result of un-escaped or incorrectly quoted input values. If you learnt your current technique from a tutorial or book, please don't use it again. – ADyson Nov 23 '22 at 21:27
  • Thanks @ADyson, i will consider your suggest of safe SQL. About the first question, the eventSource is defined in the JS code, i just put only the code for the function, the event source is inside of document.addEventListener('DOMContentLoaded', function () { and said: events: base_url + 'Home/listar', and is working, work with modal, add, modify, delete, drag. i guess that what i want in my question is reload the calendar with the new array, erasing the last data array. but don't know how to do it :( – Juan Carlos Gallego A Nov 23 '22 at 23:14
  • 1
    I fix it :) !!!!!!!!!! am happy. Am just change in my JS and put these two lines calendar.removeAllEvents(); calendar.addEventSource(res) Thanks @ADyson, when you told me that is not necesary the refetchEvents that was my clue. Thanks again – Juan Carlos Gallego A Nov 23 '22 at 23:41
  • Great. If you fixed it please add an Answer below with your code and a short explanation, then other people can find it and upvote it. Comments aren't searchable and you don't get reputation points for votes on them! :-) – ADyson Nov 24 '22 at 10:00

1 Answers1

1

I Fixed changing the JS code like these:

//sedegeneral.addEventListener("click", clicksede);
//function clicksede(){
sedegeneral.addEventListener("change", clicksede2);
    function clicksede2(){
    const sedegeneral1 = document.getElementById('sedegeneral').value;
    const doctorseleccionado1 = document.getElementById('doctorseleccionado').value;
        const url = base_url + 'Home/listar';
        const http = new XMLHttpRequest();
        http.open("POST", url, true);
        http.send(new FormData(frmflt));
        http.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
                console.log(this.responseText);
                const ressede = JSON.parse(this.responseText);
                 Swal.fire(
                     'Filtrando Sede '+sedegeneral1+' y el doctor '+doctorseleccionado1,
                     //ressede.msg,
                     //ressede.tipo
                 )
                    calendar.removeAllEvents();
                    calendar.addEventSource(ressede)
            }
        }
    sedegeneral.removeEventListener("change", clicksede2);
    };
//};
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 29 '22 at 10:41