点击后将ID发布到PHP

时间:2013-11-21 13:21:12

标签: php mysql ajax database

JS FILE

       // Three buttons here SAVE, DELETE and CANCEL
       // Save works like supossed to be it saves to Database
       // Delete does only $calendar.weekCalendar("removeEvent", calEvent.id); but dont delete from database


    buttons: {
                   save : function() {
                      calEvent.id = id;
                      id++;
                      calEvent.start = new Date(startField.val());
                      calEvent.end = new Date(endField.val());
                      calEvent.title = titleField.val();
                      calEvent.body = bodyField.val();

                      //post to events.php

                      $.post("events.php?action=save&start="+calEvent.start.getTime()/1000+"&end="+calEvent.end.getTime()/1000+"&title="+calEvent.title+"&body="+calEvent.body);

                      $calendar.weekCalendar("removeUnsavedEvents");
                      $calendar.weekCalendar("updateEvent", calEvent);
                      $dialogContent.dialog("close");
                   },,
                "delete"  : function() {
                      calEvent.id = id;
                      id++;

                      // It must send calEvent.id to PHP action=del
                     $.get( "events.php", { action: "del", id: calEvent.id });

                      $calendar.weekCalendar("removeEvent", calEvent.id);
                      $dialogContent.dialog("close");


                    } ,
                   cancel : function() {
                      $dialogContent.dialog("close");
                   }
                }
             }).show();

PHP文件

$action = $_REQUEST['action'];

if (!$link = mysql_connect('host', 'user', 'pass')) {
    echo 'Could not connect to mysql';
    exit;
}

if (!mysql_select_db('agenda', $link)) {
    echo 'Could not select database';
    exit;
}
if ($action === 'del')
{
    $id = $_GET['id']; 

    $del = "DELETE FROM meeting_rooms_calendar WHERE id='$id'";
    $result = mysql_query($del, $link);
    print_r($id);
} 
elseif($action === 'save')
{
    $title = $_REQUEST['title'];
    $body = $_REQUEST['body'];
    $start_time = (int)$_REQUEST['start'];
    $start_time = $start_time + 60*60;
    $end_time = (int)$_REQUEST['end'];
    $end_time = $end_time + 60*60;
    $start = date('c',$start_time);
    $end = date('c',$end_time);
    $sql = "INSERT INTO meeting_rooms_calendar(title,body,start,end) VALUES ('$title','$body','$start','$end')";
    $result = mysql_query($sql, $link);

}

现在,如果我点击保存,保存将请求变量发送到PHP和PHP到数据库,当我点击DELETE它doenst发送请求的ID到PHP所以PHP可以; t从数据库删除它我怎么能使JS file将ID发送给PHP。

如果我通过

手动完成
www.myweb.com/events.php?action=del&id=1

这将从数据库中删除它,这意味着PHP可以工作。 它只是JS部分,它不会自动从数据库中删除它。

0 个答案:

没有答案