0

I'd like to know how I'm able to call a function in PHP when clicking either a link or a button. I'm making a calendar and need to have links for the next month and previous month, and the page needs to reload with the next month or previous. I have a function that creates a calendar given the month and year so I was hoping I could just modify the month parameters by incrementing or decrementing and passing it into the function.

could I do something like this?

echo "<a href= 'calendarFunction($m , $y)'> NEXT MONTH </a>";

  • 2
    It is not possible to call a PHP function directly in HTML, JS or other browser-side technology. The only one solution is to use AJAX call. – barell Jun 02 '14 at 19:58
  • Or you could write a javascript function calendarFunction, and use it with onClick method on the html tag. – Cristofor Jun 02 '14 at 20:03
  • Thanks @bloodyKnuckles, I knew there was one I just couldn't find it. – Jeff Lambert Jun 02 '14 at 20:03
  • I would greatly recommend that you read up on the differences between client side and server side execution before you do anything further. These are fundamental concepts that you must understand, if nothing else because you may otherwise open up your site to major security vulnerabilities. – Daniel Perván Jun 02 '14 at 22:07

1 Answers1

0

The easiest way is to use $_GET variable. More over if you want to use pagination for your months.

<a href="/path/calendar_script.php?month=<?= $m ?>&year=<?= $y ?>"> NEXT MONTH</a>;

Then handle this in your PHP script :

$month = isset($_GET['month']) ? $_GET['month'] : date('m');
$year = isset($_GET['year']) ? $_GET['year'] : date('Y');
Vincent Decaux
  • 9,857
  • 6
  • 56
  • 84
  • thank you, but how do I konw what my url extension will be? because right now it's just clndr.php and I was hoping to just reload clndr.php by passing that function the different parameters. – user3596343 Jun 02 '14 at 20:13
  • So just change /path/calendar.php to clndr.php... – Vincent Decaux Jun 02 '14 at 20:21
  • I meant the extension after ".php". But basically I have a function that only needs modifying to display the next month ie. createCal($nextM, $y) and I just need to call this function with the new parameters whenever someone clicks the link – user3596343 Jun 02 '14 at 20:26
  • all i want to do is say if (user clicks on 'next') then createCal($nextM, $y) . else if (user clicks on 'prev') then createCal($prevM, $y) – user3596343 Jun 02 '14 at 20:29