2012-11-12 11 views
5

Sto tentando di creare un'interfaccia di eventi MySQL supportata, utilizzando fullCalendar e MySQL. Ho provato a manipolare gli esempi nella documentazione di fullCalendar e ho creato correttamente un feed di eventi dal mio database.fullCalendar post metodo su MySQL

Attualmente sto provando a creare una chiamata eventDrop che invia un ID di evento, titolo e ora di inizio al database. Ho usato il codice da una precedente question per creare la chiamata eventDrop, qui è la JavaScript per l'intera pagina Callendar:

$(document).ready(function() {  
    /* initialize the external events 
    -----------------------------------------------------------------*/ 

    $('#external-events div.external-event').each(function() { 

     // create an Event Object (http://arshaw.com/fullcalendar/docs/event_data/Event_Object/) 
     // it doesn't need to have a start or end 
     var eventObject = { 
      title: $.trim($(this).text()) // use the element's text as the event title 
     }; 

     // store the Event Object in the DOM element so we can get to it later 
     $(this).data('eventObject', eventObject); 

     // make the event draggable using jQuery UI 
     $(this).draggable({ 
      zIndex: 999, 
      revert: true,  // will cause the event to go back to its 
      revertDuration: 0 // original position after the drag 
     });      
    });  

    /* initialize the calendar 
    -----------------------------------------------------------------*/ 

    $('#calendar').fullCalendar({ 
     header: { 
      left: 'prev,next today', 
      center: 'title', 
      right: 'month,agendaWeek,agendaDay' 
     }, 
     editable: true, 
     droppable: true, // this allows things to be dropped onto the calendar !!! 
     drop: function(date, allDay) { // this function is called when something is dropped 

      // retrieve the dropped element's stored Event Object 
      var originalEventObject = $(this).data('eventObject'); 

      // we need to copy it, so that multiple events don't have a reference to the same object 
      var copiedEventObject = $.extend({}, originalEventObject); 

      // assign it the date that was reported 
      copiedEventObject.start = date; 
      copiedEventObject.allDay = allDay; 

      // render the event on the calendar 
      // the last `true` argument determines if the event "sticks" (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/) 
      $('#calendar').fullCalendar('renderEvent', copiedEventObject, true);     

      // if so, remove the element from the "Draggable Events" list 
      $(this).remove();      
     }, 

     // events from mysql database 
     events: "/json-events.php", 

     // submit to database 
     eventDrop: function(calEvent, jsEvent, view) { 
      var method = 'POST'; 
      var path = 'submit.php'; 
      var params = new Array(); 
      params['id'] = calEvent.id; 
      params['start'] = calEvent.start; 
      params['end'] = calEvent.end; 
      params['title'] = calEvent.title; 
      post_to_url(path, params, method); 
    } 
    });  
}); 

Il file PHP speravo avrebbe ricevuto i dati POST e inserirlo nel database con una fine tempo pari al tempo di inizio più 15 minuti (a cura dopo la risposta di seguito):

<?php 

mysql_connect("") or die(mysql_error()); 
mysql_select_db("") or die(mysql_error()); 

$id = $_POST["id"]; 
$title = $_POST["title"]; 
$start = $_POST["start"]; 
$end = date(Y-m-d T H:i:s , strtotime($start)+900); 

$query = "INSERT INTO `events` VALUES (`$id`, `$title`, `$start`, `$end`, ``)"; 
mysql_query($query); 
print $query; 
?> 

il database non riceve i dati dell'evento.

risposta

7

Questo è la conclusione che mi è venuta in mente e non ho problemi a far funzionare questo fuori dal mio test e dal server pubblico. Ho preso il FullCalendar e questo è il formato che uso.

Il database è molto semplice.

id integer 11 chars primary key auto-increment, 
title varchar 50, 
start varchar 50, 
end varchar 50, 
url varchar 50. 

Questo è il file index.php o index.html.

<!DOCTYPE html> 
<html> 
<head> 
<link href='css/fullcalendar.css' rel='stylesheet' /> 
<link href='css/fullcalendar.print.css' rel='stylesheet' media='print' /> 
<script src='js/jquery-1.9.1.min.js'></script> 
<script src='js/jquery-ui-1.10.2.custom.min.js'></script> 
<script src='js/fullcalendar.min.js'></script> 
<script> 
    $(document).ready(function() { 
     $('#calendar').fullCalendar({ 
      header: { 
       left: 'prev,next today', 
       center: 'title', 
       right: 'month,agendaWeek,agendaDay' 
      }, 
      editable: true, 
      events: "json.php", 
      eventDrop: function(event, delta) { 
       alert(event.title + ' was moved ' + delta + ' days\n' + 
        '(should probably update your database)'); 
      }, 
      loading: function(bool) { 
       if (bool) $('#loading').show(); 
       else $('#loading').hide(); 
      } 
     }); 
    }); 
</script> 
<style> 
    body { 
     margin-top: 40px; 
     text-align: center; 
     font-size: 14px; 
     font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif; 
     } 
    #loading { 
     position: absolute; 
     top: 5px; 
     right: 5px; 
     } 
    #calendar { 
     width: 900px; 
     margin: 0 auto; 
     } 
</style> 
</head> 
<body> 
<div id='loading' style='display:none'>loading...</div> 
<div id='calendar'></div> 
<p>json.php needs to be running in the same directory.</p> 
</body> 
</html> 

Questo è il file json.php.

<?php 
mysql_pconnect("localhost", "root", "") or die("Could not connect"); 
mysql_select_db("calendar") or die("Could not select database"); 


$rs = mysql_query("SELECT * FROM events ORDER BY start ASC"); 
$arr = array(); 

while($obj = mysql_fetch_object($rs)) { 
$arr[] = $obj; 
} 
echo json_encode($arr); 
?> 
0

Dopo aver rimosso i nomi delle colonne nella query di inserimento e provare, se non si ottiene, pensiamoci. Fai qualcosa del genere

mysql_query(INSERT INTO `events` VALUES ('$id', '$title', '$start', '$end', '')); 

Basta stampare i valori dei post prima di inserirli, se sembra chiaro, quindi controlla la tua richiesta.

eco la query e di uscita, si può ottenere qualcosa di simile

mysql_query(INSERT INTO `events` VALUES ('23', 'event title', 'start date', 'end date', '')); 

eseguire la query per trovare errori di eventuali

+0

Grazie per la risposta. Penso di aver capito e ho iniziato a testare il mio file di invio con valori diretti e controllando il database senza fortuna. Ho cambiato il file php in: '$ id = $ _POST [" id "]; $ title = $ _POST ["title"]; $ start = $ _POST ["start"]; $ end = date (Y-m-d T H: i: s, strtotime ($ start) +900); $ query = "INSERT INTO' eventi' VALUES ('$ id',' $ title', '$ start',' $ end', '') "; mysql_query ($ query); print $ query 'Sto ricevendo un errore interno del server quando si passano le variabili direttamente attraverso l'url? – SlartyBartfast

0

print $query - ci dovrebbe essere un ; alla fine

+3

per favore inserisci questo tipo di risposte a una riga nel commento –