Forum Moderators: coopster

Message Too Old, No Replies

Add form

         

franches

8:07 am on Sep 28, 2004 (gmt 0)

10+ Year Member



hi,
i would like to ask for help. actually i'm doing a statuslog form. the user has to login first before he could go to the statuslog form. the login is ok. the statuslog form contain the entered PIN and name of the user . The user has to input all the activities with the corresponding workhours. However, he could only input the activities one at time so i have a Add Hours button. My problem now is after i press the Add Hours button it is returning to the login page.

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>

httpwebwitch

2:39 pm on Sep 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I did something like this in ASP a few years ago. I can't offer the source code (it's in VBscript, therefore useless), but I can offer some strategic guidance.

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!

franches

1:02 am on Sep 29, 2004 (gmt 0)

10+ Year Member



the project i'm doing right now originally been done in ASP(by another person) and I have to do this in PHP. I am a programmer trainee so this is my first time to do a project. I am only doing a self study. Eventhough I find it hard but I really would like to learn.

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.

mincklerstraat

7:31 am on Sep 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



your $username and $name aren't being set, so they won't show anything. You'll have to find a way of setting these with the right values somehow. You seem to expect $pin to come in from $_POST (make this actually happen then - $username = $_POST['pin'] if this is the corresponding value). Once you have these values, you can send them along in hidden input fields. Go to your favorite HTML resource on forms and look for <input type="hidden".

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!

franches

3:11 am on Oct 4, 2004 (gmt 0)

10+ Year Member



I want to show you my codes.

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>

mincklerstraat

9:38 am on Oct 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Franches, it looks like your problem with going back to the login is caused because the action you're giving to your form is $_SERVER['PHP_SELF'] - and since it's the login page that includes tutor.php, this would be the login page. $_SERVER['PHP_SELF'] gives you the name of the page that was called, not the name of a file that was included.

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.

franches

2:04 am on Oct 5, 2004 (gmt 0)

10+ Year Member



i tried your code but i got the error

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.

mincklerstraat

10:51 am on Oct 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



sorry, it's get_magic_quotes_gpc. When you get this kind of error, you can always go over to php.net and do a search to see if the function is misspelled or if there is another function like it. Most people here posting code to help aren't actually going to try it out first, since that takes quite a bit more time.

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.