hello,
I am building a mysql/php/javascript based online timesheet system which allows users to assign themselves predefined projects and then select one of the projects assigned to them and add hours for a particular date to the database - i can get the add to work fine, but i need to be able to amend the hours once they have been entered (i.e. go back to the same form, read the existing hours in and then allow the user to edit them via the form then update the database record)
I have a javascript based calendar which changes the content of an anchor tag without refreshing the page i.e.
initially:-
<a id="tag">12/12/2007</a>
then click "change date" the calendar pops up, you can click a date and then it closes the calendar popup and updates the content of the <a> tag WITHOUT refreshing the page. The fact that it doesnt refresh the page is important and needs to stay this way.
I need to be able to read the content of the <a> tag when it changes and read it into a php variable (which in turn affects an SQL statement)
it would be nice to ba able update the $date variable (which is initially set using $_GET . onchange() of the <a> tag's content. but i dont think you can do this!
Here is the code for my page, im sure its pretty messy and does things in an over complicated way, but it works (so far)!:
<?php
ob_start();
session_start();
$connect = mysql_connect('localhost', 'root', '');
if(!$connect){
die("sql connection error");
}
$username = $_SESSION['username'];
mysql_select_db('timesheet');
$sqlUser = "select * from user where Username='".$_SESSION['username']."';";
$queryUser = mysql_query($sqlUser);
?>
<html>
<head>
<title>Impetus online project timesheet</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script>
function readDate(elem){
var ele = document.getElementById(elem);
var childNodes = ele.childNodes[0];
var element = childNodes.nodeValue
document.hours.date.value=element;
}
function getDate(){
var todaysDate;
var currentTime = new Date();
var day = currentTime.getDate();
var month = currentTime.getMonth()+1;
var year = currentTime.getFullYear();
todaysDate= window.location ="addHours.php?date="+day+"/"+month+"/"+year;
return todaysDate;
}
</script>
</head>
<body>
<script type='text/JavaScript' src='scw.js'></script>
<!-- start of container table -->
<table class="tablestyle" width="800px" align="center" border="1">
<tr>
<td align="center" class="cellstyle"><img src="img/impetuslogo.png"></td>
</tr>
<tr><td class="cellstyle">
<!-- start of content table -->
<table align="center" width="100%" border="0">
<tr>
<td align="center" colspan="2"><h1>Welcome to the Impetus Online Timesheet</h1></td>
</tr>
<tr>
<td rowspan="5" width="20%" class="tablestyle1" valign="top">
<!-- start of nav table -->
<table width="100%">
<?php if(isset($_SESSION['username'])){?>
<tr>
<td class="cellstyle1" align="center">
<?php
echo 'Hello '.$username;
?>
</td>
</tr>
<tr>
<td align="center" class="cellstyle1">
<a href="yourProjects.php">Your Projects</a>
</td>
</tr>
<tr>
<td align="center" class="cellstyle1">
<a href="#">Your Hours</a>
</td>
</tr>
<tr>
<td align="center" class="cellstyle1">
<a onclick="return getDate();" href="#">Add Hours</a>
</td>
</tr>
<tr>
<td align="center" class="cellstyle1">
<a href="#">Create a Report</a>
</td>
</tr>
<tr>
<td align="center" class="cellstyle1">
<a href="addProject.php">Add a new Project</a>
</td>
</tr>
</table>
<!-- end of nav table -->
</td>
<td class="tablestyle1">
<!-- start of inner content table -->
<table width="100%" border="0">
<tr>
<td colspan="4">
<p>Select the project you wish to add hours for - Please note you can change the projects you are enrolled on in <b>"Your Projects"</b>.</p>
</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td width="10%">
<p><b>Project:</b></p>
</td>
<form name="projName" action="addHours.php" method="get">
<input type="hidden" id="hiddendate" name="hiddendate">
<?php
$projectName= Array();
$counter=0;
$date=$_GET['date'];
while($row = mysql_fetch_array($queryUser)) {
for($i=0;$i<15;$i++){
$projectName[$i] = $row['Project'.($i+1)];
if($projectName[$i]!=="" && isset($projectName[$i])){
if($counter>=3){
echo'
</tr><tr><td></td>
<td>
<a href="addHours.php?projectName='.$projectName[$i].'&date='.$date.'">'.$projectName[$i].'</a>
</td>
';
$counter=0;
}
else if($counter<3){
echo'
<td>
<a href="addHours.php?projectName='.$projectName[$i].'&date='.$date.'">'.$projectName[$i].'</a>
</td>
';
$counter++;
}
}
//else{echo'empty';}
}
}
$explodedGet = explode("%20", $_GET['projectName']);
for($t=0;$t<count($explodedGet);$t++){
$trim= $trim.$explodedGet[$t]." ";
}
$pName= rtrim($trim);
if(isset($pName) && $pName!==""){
?>
</tr>
<tr>
<td colspan="2" align="center">
<p>Add your hours for <a id='aTest'><?php echo $date; ?></a></p>
</td>
<td colspan="2" align="center">
<a href='javascript:scwShow(scwID("aTest"),scwID("aTest"));' :mrgreen:***THIS IS WHRE THE CALENDAR IS CALLED ***:mrgreen: onclick=>Click here to change the date</a>
</td>
</tr>
<tr>
<td colspan="4">
<table width="100%">
<tr>
<td colspan="2" height="30px">
Currently Select Project: <b><?php echo $pName; ?></b>
</td>
</tr>
<tr>
<td width="30%">
<p>Type of work</p>
</td>
<td width="30%">
<p>Hours</p>
</td>
<td width="40%">
<p>Notes</p>
</td>
</tr>
</form>
<form name="hours" method="get" action="addHoursDB.php">
<input type="hidden" name="date" value="bolocks">
<?php
:mrgreen:***this function checks to see if a record already exists for the username, date and project that are currently selected ***:mrgreen:
function exists(){
$sqlexists = "select Date from day where Username='".$_SESSION['username']."';";
if($existsQuery = mysql_query($sqlexists) or die("there was an error")){
//echo'inside if statement';
while($check = mysql_fetch_array($existsQuery)){
$datecheck = $check['Date'];
//echo'inside while loop';
if($date == $datecheck){
$exists = true;
//echo'the record already exists';
break;
}
else if($date !==$datecheck){
$exists = false;
}
}
}
return exists;
}
if(exists()==false){
echo'<tr><td colspan="4">You have not added any hours for this project on this date yet, so you have been taken into the "normal mode"</td></tr>';
$sqlProject = "select * from project where ProjectName like '%".$pName."%';";
$queryProject = mysql_query($sqlProject) or die(mysql_error());
$workType= Array();
while($proj = mysql_fetch_array($queryProject)){
//echo'here';
for($r=0;$r<10;$r++){
//echo "WorkType: ".$proj['WorkType1'];
$workType[$r] = $proj['WorkType'.($r+1)];
if(isset($workType[$r]) && $workType[$r]!==""){
echo'
<tr>
<td>
<p>'.$workType[$r].'</p><input type="hidden" name="WorkType'.($r+1).'" value="'.$workType[$r].'">
</td>
<td>
<p>Hrs: <select name="hoursHours'.($r+1).'"><option>0</option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option></select> Mins: <select name="hoursMins'.($r+1).'"><option>0</option><option>15</option><option>30</option><option>45</option></select></p>
</td>
<td>
<p><textarea rows="4" cols="30" name="notes'.($r+1).'"></textarea></p>
</td>
</tr>
';
}
else{break;}
}
}
}
else if(exists()==true){
echo'<tr><td colspan="4">You have already added hours for this project on this date, so you have been taken into the "amend mode"</td></tr>';
$_SESSION['dayexists']='true';
$sqlProject = "select * from day where Date='".$Date."' AND ProjectName like '%".$pName."%' AND Username='".$_SESSION['username']."';";
$queryProject = mysql_query($sqlProject) or die("EXISTS ERROR ".mysql_error());
mysql_query($sqlProject) or die("EXISTS ERROR ".mysql_error());
$workType= Array();
while($proj = mysql_fetch_array($queryProject)){
echo'here';
for($r=0;$r<10;$r++){
//echo "WorkType: ".$proj['WorkType1'];
$workType[$r] = $proj['WorkType'.($r+1)];
if(isset($workType[$r]) && $workType[$r]!==""){
echo'
<tr>
<td>
<p>'.$workType[$r].'</p><input type="hidden" name="WorkType'.($r+1).'" value="'.$workType[$r].'">
</td>
<td>
<p>Hrs: <select name="hoursHours'.($r+1).'"><option></option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option></select> Mins: <select name="hoursMins'.($r+1).'"><option></option><option>15</option><option>30</option><option>45</option></select></p>
</td>
<td>
<p><textarea rows="4" cols="30" name="notes'.($r+1).'"></textarea></p>
</td>
</tr>
';
}
else{break;}
}
}
}
?>
<tr>
<td>
<input type="hidden" name="projectName" value="<?php echo $pName; ?>"><input type="submit" value="Submit" name="submit" onclick="readDate('aTest')">
</td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<!-- end of inner content table -->
<?php
}
}
else{echo "<tr><td>You are not logged in. Click <a href='index.php'>here</a> to log in.</td></tr>";}
echo'
</td>
</tr>
</table>
<!-- end of content table -->
</td></tr>
</table>
<!-- end of container table -->
</body>
</html>
';
?>
The problem at the moment is becuase when the page loads it gets todays date (a date has to be supplied to the javascript calendar before you pick a different date, so i pass todays date in the url from the previous page)
So once you have added some information for a particular date with a particular username and a particular project title, the script always thinks you have already added hours for that day and goes into the "amend/edit" block of code. I need to be able to change the date using the calendar then the method exists() to be called and passed the new date (which will then mean the method wont find an existing record and will jump to the "new hours" type block)
I know this is pretty long and maybe too complex, but i have been trying to find a solution for a while now and its getting really annoying!
I hope this makes sense!
Please help!!!!!
Thanks,
Mike