2017-09-05 120 views
0

我刚刚开始在Web开发中,我正在尝试使用Ajax在PHP中进行一些操作。我最近下载了一个使用php,jquery和ajax的脚本。它的作用是显示一个日历,当用户点击某个特定的日子时,该日历可以启用输入,并在用户点击“添加”按钮后将数据添加到数据库。

It looks somethings like this

之后,我修改了数据库表,其中我加为user_id为一列,因为我想只有谁添加数据到它的用户查看日历日报表。现在,我的问题是我无法将我的$ _SESSION ['userSession']用于Ajax请求。我试图把变量添加到mysqli的查询,但不幸的是,它会产生这样那样的错误:

注意:未定义的变量:U_ID在C:\ XAMPP \ htdocs中\报告\上线288的functions.php

我有两个PHP文件,一个用于主页面,另一个用于处理日历的日历和其他功能。

这里是我的reports.php文件看起来是这样的:

<?php 
session_start(); 
include_once 'dbconnect.php'; 
include_once('functions.php'); 

if (!isset($_SESSION['userSession'])) { 
    header("Location: index.php"); 
} 

$query = $DBcon->query("SELECT * FROM users WHERE user_id=".$_SESSION['userSession']); 
$userRow=$query->fetch_array(); 
$DBcon->close(); 

$u_id = $_SESSION['userSession']; //this is the variable that I am trying to pass to the functions.php file 

    ?> 
<!-- The echo below produces the calendar view --> 
<div id="calendar_div"> 
    <?php echo getCalender(); ?> 
</div> 

现在这里的functions.php的样本:

<?php 


if(isset($_POST['func']) && !empty($_POST['func'])){ 
    switch($_POST['func']){ 
     case 'getCalender': 
      getCalender($_POST['year'],$_POST['month']); 
      break; 
     case 'getEvents': 
      getEvents($_POST['date']); 
      break; 
     //For Add Event 
     case 'addEvent': 
      addEvent($_POST['date'],$_POST['title'],$_POST['week'],$_POST['hours']); 
      break; 
     default: 
      break; 
    } 
} 

?> 



<script type="text/javascript"> 
    function getCalendar(target_div,year,month){ 
     $.ajax({ 
      type:'POST', 
      url:'functions.php', 
      data:'func=getCalender&year='+year+'&month='+month, 
      success:function(html){ 
       $('#'+target_div).html(html); 
      } 
     }); 
    } 

    function getEvents(date){ 
     $.ajax({ 
      type:'POST', 
      url:'functions.php', 
      data:'func=getEvents&date='+date, 
      success:function(html){ 
       $('#event_list').html(html); 
       $('#event_add').slideUp('slow'); 
       $('#event_list').slideDown('slow'); 
      } 
     }); 
    } 
    //For Add Event 
    function addEvent(date){ 

     $('#eventDate').val(date); 
     $('#eventDateView').html(date); 
     $('#event_list').slideUp('slow'); 
     $('#event_add').slideDown('slow'); 
    } 
    //For Add Event 
    $(document).ready(function(){ 
     $('#addEventBtn').on('click',function(){ 
      var date = $('#eventDate').val(); 
      var title = $('#eventTitle').val(); 
      var week = $('#eventWeek').val(); 
      var hours = $('#eventHours').val(); 
      $.ajax({ 
       type:'POST', 
       url:'functions.php', 
       data:'func=addEvent&date='+date+'&title='+title+'&week='+week+'&hours='+hours, 
       success:function(msg){ 
        if(msg == 'ok'){ 
         var dateSplit = date.split("-"); 
         $('#eventTitle').val(''); 
         $('#eventWeek').val(''); 
         $('#eventHours').val(''); 
         alert('Daily report has been added.'); 
         getCalendar('calendar_div',dateSplit[0],dateSplit[1]); 
        }else{ 
         alert('Some problem occurred, please try again.'); 
        } 
       } 
      }); 
     }); 
    }); 

function getEvents($date = ''){ 
    //Include db configuration file 
    include 'dbconnect.php'; 
    $eventListHTML = ''; 
    $date = $date?$date:date("Y-m-d"); 
    //Get events based on the current date 
    $result = $DBcon->query("SELECT title,hours FROM ojt_student_report WHERE date = '".$date."' AND status = 1 and user_id = '"$u_id"'"); 

    if($result->num_rows > 0){ 

     $eventListHTML = '<h2>Daily Report on '.date("l, d M Y",strtotime($date)).'</h2>'; 
     $eventListHTML .= '<ul class="list-group">'; 
     while($row = $result->fetch_assoc()){ 
      $eventListHTML .= '<li class="list-group-item"> <b>Daily Report: </b><br>'.nl2br($row['title']). '<br> <b>Working Hours: </b>' .$row['hours']. '</li>'; 
     } 
     $eventListHTML .= '</ul>'; 

    } 
    echo $eventListHTML; 
} 

/* 
* Add event to date 
*/ 
function addEvent($date,$title,$week,$hours){ 
    //Include db configuration file 
    include 'dbconnect.php'; 
    $currentDate = date("Y-m-d H:i:s"); 
    //Insert the event data into database 
    $insert = $DBcon->query("INSERT INTO ojt_student_report (title,date,week,hours,created,modified,user_id) VALUES ('".$title."','".$date."','".$week."','".$hours."','".$currentDate."','".$currentDate."','"$u_id"')"); 
    if($insert){ 
     echo 'ok'; 
    }else{ 
     echo 'err'; 
    } 
} 
?> 

基本上,我只想做的是有使用$ _SESSION变量的每个用户的不同日历和每日报告视图。我希望我能够正确地陈述我的问题,任何帮助将不胜感激。非常感谢你!

+1

如果你想在'javascript'中使用任何函数,你应该在'javascript'而不是'php'中调用它@Master Pogi – Pritamkumar

回答

2

你可以试试下面的代码:

function getCalendar(target_div,year,month){ 
     $.ajax({ 
      type:'POST', 
      url:'functions.php', 
      data:'func=getCalender&year='+year+'&month='+month + '&uid=<?php echo $u_id ; ?>', 
      success:function(html){ 
       $('#'+target_div).html(html); 
      } 
     }); 
    } 
+0

这工作,谢谢!另外,感谢其他答复,我能够从我的问题中获得所有答案中的想法 –

0

有你在calendar_div元素会话的用户ID为隐藏适当class和id。 (即)隐藏输入类型并放置您的会话值。在addEvent点击函数中,您可以使用id/class获取该会话值,并将其作为ajax参数传递。

它会解决你的问题。

1

另一种方法是制作一个返回用户ID的api。

像: returnSessionId.php

echo $_SESSION['userSession']; 

在你的HTML:

function getCalendar(target_div,year,month){ 
    $.ajax({ 
     type:'POST', 
     url:'returnSessionId.php', 

     success:function(sessionID){ 
      console.log(sessionID); 
     } 
    }); 
} 

这样你可以得到任何你想要连ID,如果你的脚本不在同一页面作为你的php文件。

相关问题