Calendar Event

In this tutorial we will show you how to create an Event Calendar using jQuery, Ajax, and PHP. This tutorial provides the script for generating calendar and display events from the MySQL database on the respective date. In this second part of the PHP event calendar well show you how to add events to the calendar using jQuery, Ajax, PHP & MySQL. Means we’ll extend our PHP event calendar script with add event and view event functionality. See the example code and image below.

Sample Code

For the addEvent function. In JavaScript code, getEvents function need slight modification and new code to be needed for handle event add. addEvent function is added and Ajax would be called on #addEventBtn click.
  1. function addEvent(date){
  2. $('#eventDate').val(date);
  3. $('#eventDateView').html(date);
  4. $('#event_list').slideUp('slow');
  5. $('#event_add').slideDown('slow');
  6. }
  7.  
  8. $(document).ready(function(){
  9. $('#addEventBtn').on('click',function(){
  10. var date = $('#eventDate').val();
  11. var title = $('#eventTitle').val();
  12. $.ajax({
  13. type:'POST',
  14. url:'functions.php',
  15. data:'func=addEvent&date='+date+'&title='+title,
  16. success:function(msg){
  17. if(msg == 'ok'){
  18. var dateSplit = date.split("-");
  19. $('#eventTitle').val('');
  20. alert('Event Created Successfully.');
  21. getCalendar('calendar_div',dateSplit[0],dateSplit[1]);
  22. }else{
  23. alert('Their was an error, please try again.');
  24. }
  25. }
  26. });
  27. });
  28. });
resultAnd for the addEvent Function script in function.php to add a file to insert the event data to the MySQL database.
  1. function addEvent($date,$title){
  2. include 'dbConfig.php';
  3. $currentDate = date("Y-m-d H:i:s");
  4. $insert = $db->query("INSERT INTO events (title,date,created,modified) VALUES ('".$title."','".$date."','".$currentDate."','".$currentDate."')");
  5. if($insert){
  6. echo 'ok';
  7. }else{
  8. echo 'err';
  9. }
  10. }
And if you already done or complete the project all you have to do is test it so you can know if their's an error. Hope that you learn in this project and enjoy coding. Don't forget to LIKE & SHARE this website.

Comments

Submitted byJanuz (not verified)on Fri, 10/13/2017 - 19:35

How to display alert on the exact date of the event?
Submitted byHill Bill (not verified)on Fri, 03/01/2024 - 03:53

Hey

Add new comment