Forum Moderators: coopster
What i really want to happen is that after pressing the "Add Hours" it should return to the same page and the PIN and Name of the user is still there and the corresponding remaining hours and work hours and total work hours are also there inorder for the user to input another activity for the day.
Is there someone who could help me with the code?
Thanks.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Workhours form</title>
<script type="text/javascript">var total = <?php echo (isset($_POST['work_hours_total'])? $_POST['work_hours_total'] : "0");?>;
function update()
{
document.getElementById("work_hours_total").value = total + Number(document.getElementById("work_hours").value);
document.getElementById("remaining_hours").value = 7.5 - Number(document.getElementById("work_hours_total").value);
}
</script>
</head>
<body>
<?php
mysql_connect("localhost", "root")
or die( "Unable to connect\n". mysql_error() );mysql_select_db("TEST")
or die("Unable to select db ".mysql_error()."\n");if (isset($_POST['pin']))
{
$sql= "insert into StatusTable (PIN, Activity,RegHours) values ('" . $_POST['pin'] . "','" . $_POST['activity'] . "','" . $_POST['work_hours'] . "')";
mysql_query($sql) or die('error making query: ' . mysql_error());
}?>
<form action="" method="post">
<fieldset>
<div>
<label for="pin">PIN :</label><? echo $username?><br>
<label for="name">Name : </label><? echo $name?> <br>
</div>
<div>
<label for="activity">Activity</label>
<input id="activity" name="activity" type="text" size="20">
</div>
<div>
<label for="work_hours">Work Hours</label>
<input id="work_hours" name="work_hours" type="text" size="5" onchange="update()">
</div>
<div>
<label for="work_hours_total">Total Work Hours</label>
<input id="work_hours_total" name="work_hours_total" type="text" size="5" value="<?php echo (isset($_POST['work_hours_total'])? $_POST['work_hours_total'] : "");?>" readonly="true">
</div>
<div>
<label for="remaining_hours">Hours Remaining</label>
<input id="remaining_hours" name="remaining_hours" type="text" size="5" value="<?php echo (isset($_POST['work_hours_total'])? (7.5 - $_POST['work_hours_total']) : "7.5");?>" readonly="true">
</div>
<div>
<input type="submit" value="Add hours" name="Add">
</div></fieldset>
</form></body>
</html>
In my time tracking program, when the form was initially loaded, there was 1 space for entering a new item.
[ input ]
--------- (calculated total)
[ add hours ][ finished ]
The "add hours" and "finished" buttons are basically identical, except that "finished" moves on to the reporting page, while "add hours" reloads the same page. This can be done by making the ACTION of your form PHP_SELF, creating two submit buttons with different values, and checking that when the page reloads.
<?php
// write stuff to the db.
if ($_POST['submit']=='finished'){
header("Location:somewherelese.php");
}
?>
<form>
<submit type="submit" name="submit" value="add hours">
<submit type="submit" name="submit" value="finished">
</form>
So if someone entered 5 items for that day, the page would load like this:
text [edit][del]
text [edit][del]
text [edit][del]
text [edit][del]
text [edit][del]
[ input ]
--------- (calculated total)
[ add hours ][ finished ]
Basically you look at the database first, and display anything found as plain text. Put your form at the end.
Good luck!
thanks. by the way if you have any idea or tutorials or sample codes please let me know. I've already browsed the internet for tutorials and I haven't found any which is closed with what i'm doing.
Learning PHP can be hard, but it's usually only really hard if you try to jump over some of the basics. I'm guessing that you could do with a good refresher when it comes to variables in general, and get and post variables. Scripts don't work when variables aren't set properly (or when they're not set at all). Good luck!
I badly need help with these codes. I'm doing a statuslog form. Before the user could go the form he/she has to log in.
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
</head>
<body>
<form action="validationtest.php" method=post>
<table align=center style="font-family:arial; font-size:12; border:1 solid #000000;">
<tr><td colspan=2 align=center bgcolor=#123dd4>LOGIN</td></tr>
<tr><td align=right>Username: </td><td><input type=text name=username size=15></td></tr>
<tr><td align=right>Password: </td><td><input type=password name=password size=15></td></tr>
<tr><td align=center colspan=2><input type=submit value=Login></td></tr>
</table>
</form>
</body>
</html
then i have a validationtest.php
<?
session_start();if ($username=="" ¦¦ $password=="")
{
echo "You have to enter your username and password";
include ('login.php');
}else
{
include ('db.php');
$result=mysql_query("select * from StaffTable where PIN='$username'")
or die ("cant do it");while ($row=mysql_fetch_array($result))
{
if ($row["Password"]==$password )
{
$name=$row["Name"];
session_register('Name');
$_SESSION['Name'] = $Name;include('tutor.php');
}
else
{
print("Please enter your valid Username and Password!");
include ('login.php');
}
}
}
?>
after the person is validated then it will directed to tutor.php and this is where my problem started. The problem is after i press the add hours it goes directly to the login form again. what i really want to happen is that after pressing the add hours it will still go on the same page so that the user will be able to log his/her other activities. I hope someone could help me with the code. i'm doing this for the first time.
thank you very much!
<?
session_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Workhours form</title>
<script type="text/javascript">var total = <?php echo (isset($_POST['work_hours_total'])? $_POST['work_hours_total'] : "0");?>;
function update1()
{
document.getElementById("work_hours_total").value = total + Number(document.getElementById("work_hours").value);
document.getElementById("remaining_hours").value = 7.5 - Number(document.getElementById("work_hours_total").value);
}</script>
</head>
<body>
<?php
mysql_connect("localhost", "root")
or die( "Unable to connect\n". mysql_error() );mysql_select_db("TEST")
or die("Unable to select db ".mysql_error()."\n");if (isset($_POST['pin']))
{
$sql= "insert into StatusTable (PIN, Activity,RegHours) values ('" . $_POST['pin'] . "','" . $_POST['activity'] . "','" . $_POST['work_hours'] . "')";
mysql_query($sql) or die('error making query: ' . mysql_error());
}
?><form action="<?php echo $_SERVER["PHP_SELF"]?>" method="post">
<fieldset>
<div>
<label for="pin">PIN :</label> <? echo $username?><br>
<label for="name">Name : </label><? echo $name?> <br>
</div>
<div>
<label for="activity">Activity</label>
<input id="activity" name="activity" type="text" size="20">
</div>
<div>
<label for="work_hours">Work Hours</label>
<input id="work_hours" name="work_hours" type="text" size="5" onchange="update()">
</div>
<div>
<label for="work_hours_total">Total Work Hours</label>
<input id="work_hours_total" name="work_hours_total" type="text" size="5" value="<?php echo (isset($_POST['work_hours_total'])? $_POST['work_hours_total'] : "");?>" readonly="true">
</div>
<div>
<label for="remaining_hours">Hours Remaining</label>
<input id="remaining_hours" name="remaining_hours" type="text" size="5" value="<?php echo (isset($_POST['work_hours_total'])? (7.5 - $_POST['work_hours_total']) : "7.5");?>" readonly="true">
</div>
<div>
<input type="submit" value="Add hours" name="Add">
</div>
</fieldset>
</form>
</body>
</html>
When it gets to this login page, then, it's finding the values for logging in haven't been set so asks the user to give them again.
I'd try to add this username and password to your sessions.
Notes posted in comments below.
<?
session_start();
if(isset($_SESSION['username'])) $username = $_SESSION['username'];
if(isset($_SESSION['password'])) $password = $_SESSION['password'];
/* variables that come in via a post form are best referenced from the $_POST array for security reasons -- you can later turn off 'register globals' */
if(isset($_POST['username'])) $username = $_POST['username'];
if(isset($_POST['password'])) $password = $_POST['password'];
/* variables which come from user input, like forms, must be treated very carefully for security reasons. One step is to remove slashes if magic_quotes_gpc is on */
if(magic_quotes_gpc()) {
$username = stripslashes($username);
$password = stripslashes($password);
}
if ($username =="" ¦¦ $password=="")
{
echo "You have to enter your username and password";
include ('login.php'); // parentheses for include are optional
}
else
{
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
include ('db.php');
$result=mysql_query("select * from StaffTable where PIN='".mysql_escape_string($username)."'")
or die ("cant do it");
/* we use mysql_escape_string and quotes around this value since it's coming in from user input, again, a security thing */
These are some notes which might help you get your code working; try before actually deploying.
Fatal error: Call to undefined function: magic_quotes_gpc() in /var/www/html/rhodora/statuslog/trial/process2.php on line 10.
i would like to attach my code so that you will be able to examine it and make some corrections but i think it's not available here in this forum.
If I had more time I'd want to look at your code - unfortunately I don't. The forum here really isn't for 'fix my code' kind of posts (see charter), even though a lot of people here look the other way and go ahead and try helping with code fixing. Not that it always works! Though I can't sink my time in your code, I can offer you: * best wishes! * though that's a rather cold comfort, I suppose.