-1

i am trying to run a public function whenever a js function is executed in laravel

here is my code:

select: function(info) {
  var title = prompt('Event Title:');

  calendar.addEvent({
          title: title,
          start: info.startStr,
          end: info.endStr,
          allDay: false
        });

            var start = info.startStr;
            var end = info.endStr;

            $title = title;
            $start = start;
            $end = end;
            $verified = 0;


            <?php echo \App\Http\Controllers\FullCalendarController::create();?>



        calendar.unselect();

},

the fuction runs whenever the user tries to add an event to the calendar

i want to specificaly run <?php echo \App\Http\Controllers\FullCalendarController::create();?> whenever the function is called but it always runs on load.

this is what the "create" function does:

public static function create()
{  
    DB::table('events')->insertGetId(
        ['title' => $title, 'start' => $start, 'end' => $end, 'verified']
    );
}

it does not work specificaly because the variables that the function uses ($title, $start, $end) are not defined until the function runs.

i have tried putting a laravel if statement around it but then it dosn't work at all and a js if statement runs it on load anyway.

  • "but it always runs on load"...of course it does. PHP runs on the **server** when your page is being loaded. That's how the HTML gets to your browser - PHP runs some code and, when it finishes, it sends some HTML and other data back to the browser. Then the JavaScript code only runs when the page is loaded into the browser. It happens later. So any PHP you write will be executed first, and the **result** of executing that PHP code is sent to the browser. Look at the "View Source" screen in your browser when you load the page, you won't see any PHP code there.... – ADyson Nov 21 '19 at 13:13
  • ...and even if you did, it wouldn't be useful because browsers only know how to execute JavaScript code, not PHP code. If you want to cause some PHP code to run when you've added an event to your calendar, then you need the browser to make contact with the server and tell it to run a PHP script. The browser also needs to send the server the event data. How does a browser make contact with a server? Via a HTTP request, of course. – ADyson Nov 21 '19 at 13:15
  • In this situation, the style of request you'd need is an **AJAX** request - i.e. the sort which is initiated by some JavaScript code (rather than by, for example, clicking on a link or submitting a form). That enables you to specify the URL of the PHP script (or in Laravel's case, the URL of the action) you want to run, and also specify the event data you want to send to the script for processing. You can find examples of AJAX requests by the thousand online. I suggest trying a newer style one using the "fetch()" function. – ADyson Nov 21 '19 at 13:16
  • P.S. You probably ought to read [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) as well, for further background. – ADyson Nov 21 '19 at 13:16

1 Answers1

1

Let me explain to you how the php and js works. First the php file is compiled and executed (on the server side), then the js, html etc is executed via browser. Thats what we call frontend and backend.

Basically what you want to achieve is easy by creating endpoint that will add "events" to the database. To access this endpoint then you will need to use for example: XMLHttpRequest javascript object. The parameters like start etc you are able to send as a request params