gpt4 book ai didi

php - 如何将 $_SESSION 变量添加到 Ajax 请求中并将数据插入 mysql 数据库 PHP?

转载 作者:太空宇宙 更新时间:2023-11-03 11:36:01 25 4
gpt4 key购买 nike

我刚刚开始网络开发,我正在尝试使用 PHP 和 Ajax 进行一些开发。我最近下载了一个使用 php、jquery 和 ajax 的脚本。它的作用是显示一个日历,一旦用户单击特定日期就可以输入,并在用户单击“添加”按钮后将数据添加到数据库中。

It looks somethings like this

后来,我修改了数据库表,在其中添加了一个user_id作为一列,因为我希望日历每日报告仅供添加数据的用户查看。现在,我的问题是我无法将 $_SESSION['userSession'] 用于 Ajax 请求。我试图将变量添加到 mysqli 查询中,但不幸的是,它产生了这种错误:

注意:C:\xampp\htdocs\reports\functions.php 288行 undefined variable :u_id

我有两个 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 变量为每个用户提供不同的日历和每日报告 View 。我希望我能够正确地陈述我的问题,我们将不胜感激。非常感谢!

最佳答案

尝试类似下面的代码:

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);
}
});
}

关于php - 如何将 $_SESSION 变量添加到 Ajax 请求中并将数据插入 mysql 数据库 PHP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46047055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com