1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Mysql Javascript Calendar Event Delete and Edit

Discussion in 'PHP' started by newphpcoder, Aug 30, 2015.

  1. #1
    Hi,

    Good day!

    I found a tutorial on creating a calendar that can add Event.

    Now, I want to enhance it to add Delete and Edit button but I have no idea on how to do that here is my code:

    calendar.php
    
    <?php
    $hostname = 'localhost';
    $username = 'root';
    $password = 'root';
    $dbname = 'calendar';
    
    $conn = new mysqli($hostname, $username, $password, $dbname);
    
    // Check connection
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script>
       function goLastMonth(month,year)
       {
         if(month == 1)
         {
           --year;
           month = 13;
         }
         --month
         var monthstring = ""+month+"";
         var monthlength = monthstring.length;
         if(monthlength <= 1)
         {
             monthstring = "0"+monthstring;
         }
    
         document.location.href = "<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
       }
       
       function goNextMonth(month,year)
       {
         if(month == 12)
         {
           ++year;
           month = 0;
         }
         ++month
         var monthstring = ""+month+"";
         var monthlength = monthstring.length;
         if(monthlength <= 1)
         {
             monthstring = "0"+monthstring;
         }
         document.location.href = "<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
       }
    </script>
    
    <style>
       .today { background-color: #d3c6bd; }
       .event { background-color: #8d735a; }
    </style>
    </head>
    
    <body>
    <?php
    if(isset($_GET['day']))
    {
       $day = $_GET['day'];
    }else{
       $day = date("j");
    }
    
    if(isset($_GET['month']))
    {
       $month = $_GET['month'];
    }else{
       $month = date("n");
    }
    
    if(isset($_GET['year']))
    {
       $year = $_GET['year'];
    }else{
       $year = date("Y");
    }
    
    $maxDay = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    if( $day > $maxDay ) {
      $day = $maxDay;
    }
    
    $currentTimeStamp = strtotime("$year-$month-$day");
    
    $monthName = date("F", $currentTimeStamp);
    
    $numDays = date("t", $currentTimeStamp);
    
    $counter = 0;
    
    if(isset($_GET['add']))
    {
       $title = $_POST['txttitle'];
       $detail = $_POST['txtdetail'];
       $eventdate = $month."/".$day."/".$year;
       
       $sqlinsert = "INSERT INTO tbl_calendar (Title, Detail, eventDate, dateAdded)
       VALUES ('".$title."','".$detail."','".$eventdate."', now())";
       if ($conn->query($sqlinsert) === TRUE) {
       //   echo "Event was successfully Added...";
       }
       else
       {
       //   echo "Event Failed to be Added...";
       }
       
    }
    ?>
    <table border="1">
      <tr>
      <td><input style='width:60px; height:40px;' type='button' value='<' name='previousbutton' onClick="goLastMonth(<?php echo $month.",".$year; ?>);"></td>
      <td colspan="5" style='text-align:center;font-weight:bold;background-color:#8d735a; color:#FFF;'><?php echo $monthName. ", ".$year; ?></td>
      <td><input style='width:60px; height:40px;' type='button' value='>' name='nextbutton' onClick="goNextMonth(<?php echo $month.",".$year; ?>);"></td>
      </tr>
       
       
      <tr  style='background-color:#af9c8e; color:white;'>
      <td width='60px' height='40px' align='center'>Sun</td>
      <td width='60px' height='40px' align='center'>Mon</td>
      <td width='60px' height='40px' align='center'>Tue</td>
      <td width='60px' height='40px' align='center'>Wed</td>
      <td width='60px' height='40px' align='center'>Thu</td>
      <td width='60px' height='40px' align='center'>Fri</td>
      <td width='60px' height='40px' align='center'>Sat</td>
      </tr>
         <?php
         echo "<tr>";
           
           for($i = 1; $i < $numDays+1; $i++, $counter++){
             $timeStamp = strtotime("$year-$month-$i");
             if($i == 1)
             {
               $firstDay = date("w", $timeStamp);
                 
                 for($j = 0; $j < $firstDay; $j++, $counter++)
                 {
                   echo "<td>&nbsp;</td>";
                 }
             }
             if($counter  % 7 == 0)
             {
                 echo "</tr><tr>";
             }
             $monthstring = $month;
             $monthlength = strlen($monthstring);
             $daystring = $i;
             $daylength = strlen($daystring);
             if($monthlength <=1)
             {
               $monthstring = "0".$monthstring;
             }
             if($daylength <=1)
             {
               $daystring = "0".$daystring;
             }
             
             $todaysDate = date("m/d/Y");
             $dateToCompare = $monthstring.'/'.$daystring.'/'.$year;
             
               echo "<td align='center' width='60px' height='40px'";
               if($todaysDate == $dateToCompare)
               {
                 echo "class='today'";   
               }
               else
               {
                 $sqlCount = "SELECT * from tbl_calendar where eventDate='".$dateToCompare."'";
                 $resCount = $conn->query($sqlCount);
                 $noOfEvent = $resCount->num_rows;   
                 if($noOfEvent >= 1){
                   echo "class='event'";
                 }
                 
               }
               echo "><a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."&v=true' style='text-decoration:none; color:#000;'>".$i."</a></td>";
           }
           
           echo "</tr>";
         ?>
    </table>
    <br/>
         <?php
           if(isset($_GET['v']))
           {
               echo "<a href='".$_SERVER['PHP_SELF']."?month=".$month."&day=".$day."&year=".$year."&v=true&f=true' style='float:left; margin:-390px auto auto 500px; background-color:#83694d; padding:5px; border-color:#fece99; border-radius:4px; color:white; cursor:pointer; width:58px; text-decoration:none; font-weight:bold;'>Add Event</a>";
               if(isset($_GET['f']))
               {
                 include("eventform.php");
               }
               $sqlEvent = "SELECT * FROM tbl_calendar WHERE eventDate = '".$month."/".$day."/".$year."'";
               $resEvent = $conn->query($sqlEvent);
               $cntEvent = $resEvent->num_rows;   
               
               echo "<br/>";
               if(isset($_GET['f']))
               {
                 echo "<div style='float:left; margin:-150px auto auto 500px;'>";
               }
               else
               {
                 echo "<div style='float:left; margin:-370px auto auto 500px;'>";   
               }
               
                while($row= $resEvent->fetch_array())
                 {
                 echo "<b>Title: </b>". $row['Title']."<br/>";
                 echo "<b>Detail: </b>". $row['Detail']."<br/>";
               }
               echo "</div>";
           }
         ?>
    </body>
    </html>
    
    Code (markup):
    eventform.php
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <form name='eventform' method='POST' action="<?php $_SERVER['PHP_SELF']; ?>?month=<?php echo $month;?>&day=<?php echo $day; ?>&year=<?php echo $year; ?>&v=true&add=true">
       <div style="float:left; margin:-360px auto auto 500px;">
      <table width='400px'>
         <tr>
         <td width='50px' style='font-weight:bold;'>Title: </td>
      <td width='250px'><input type='text' name='txttitle' size='40'></td>
      </tr>
         <tr>
         <td width='50px'  style='font-weight:bold;'>Detail: </td>
      <td width='250px'><textarea name='txtdetail' style='width: 300px; height: 150px;'> </textarea></td>
      </tr>
         <tr>
         <td colspan='2' align='center'><input type='submit' name='btnadd' value='Submit' style='width:80px; height:25px; background-image:url(images/Search-Button.jpg); border-radius:5px; border: 1px solid #616f7c; color:#fff; margin: auto;cursor:pointer;'></td>
      </tr>
       
      </table>
      </div>
    </form>
    </body>
    </html>
    
    Code (markup):
    Thank you
     
    newphpcoder, Aug 30, 2015 IP
  2. wwfc_barmy_army

    wwfc_barmy_army Well-Known Member

    Messages:
    122
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #2
    I suggest you look at Prepared Statements first.

    http://php.net/manual/en/mysqli.prepare.php
     
    wwfc_barmy_army, Aug 31, 2015 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    Ok, there's SO much bad practices in there it's shocking it works. FIRST thing I'd do is get the scripttardery AND the style the **** out of the markup! Has NO business there. Second, STOP using tables for layout particularly on forms, get some blasted LABEL tags in there, and honestly, STOP using JavaScript to do something you could do ahead of time server-side. Even for the thing that SHOULD be a table (the calendar itself) there are more tags that go into a table than just TR and TD. Where's your caption? Your THEAD? the TH with SCOPE?

    Much less you've got mysqli, why isn't it using prepared queries? (as @wwfc_barmy_army suggests)

    ... and you're including the form with full HTTP header and footer, so you'd have TWO sets of HEAD and BODY and DOCTYPE, that's just gibberish markup. (if you're going to make a form include AFTER you've already output all that, don't put that in the include!)

    There's just so much wrong with that -- what outdated tutorial (that was garbage when it was written) is telling you to do ANY of that?

    If I have time tomorrow (pulling a fast fade here) I'll see if I can figure out what in blazes that is trying to do, and see if I can't put together some sample code to show you how we do that in THIS decade.
     
    deathshadow, Sep 3, 2015 IP
    wwfc_barmy_army likes this.