...

View Full Version : forms



franches
09-28-2004, 05:57 AM
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 add another activities. it will only return to the login page if he presses the logout button.

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>

raf
09-28-2004, 10:24 AM
hmm. not sure about your setup.

normally, this is what you do:
- on top of the formpage, you see if a sessionvariable is set (al loginflag) and if this flag isn't set, you redirect to the loginform. Like


// at the top of the page
session_start();
if (!$_SESSION['logedin']){
header('Location: login.php');
die();
}
- in your loginpage, when you proces the submitted user and pin, you set the loginflag


// at the top of the page
session_start();
//some other code to validate the login etc
$_SESSION['logedin']=True;

You can also store the value of the pin etc in sessionvariables
- when you then proces the form where they input the hours etc, you then check if the form was submitted or if the pagfe is requested for the first time. Like


if ($_POST['Add']=='Add Value'){
mysql_connect("localhost", "root")
or die( "Unable to connect\n". mysql_error() );
...
mysql_query($sql) or die('error making query: ' . mysql_error());

}

Note --> better put the connection and db-selectionstrings in a seperate file that you then include.

franches
09-29-2004, 01:15 AM
i really need your instruction bout this one. this is the very first time i'm doing this and i'm on the process of learning it by myself.

thanks. hoping for your response. :)

actually this is my login page

<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>

this is my validationtest.php

<?
if ($username=="" || $password=="")
{
echo "You have to enter your username and password";
include ('login.php');
}

else
{
mysql_connect("localhost", "root") or
die ("Could not connect to database");

mysql_select_db("dals") or
die ("Could not select database");

$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"];
include('tutor.php');
// printf("Successfully Logged In!");
}

else
{
print("Please enter your valid Username and Password!");
include ('login.php');
}
}
}
?>

hemebond
09-29-2004, 01:29 AM
What version of PHP are you using?

franches
09-30-2004, 01:06 AM
php 4.2.2.

i really need help with this one. i'm trying but having difficulty.

thanks

boeing747fp
10-01-2004, 08:47 PM
try this?


<?
if($username != "" && $password != ""){
$con = mysql_connect("localhost", "root", "password") or die("Could not connect to database");
mysql_select_db("dals",$con) or die("Could not select database");

$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"];
include('tutor.php');
// printf("Successfully Logged In!");
}
else{
echo "Please enter your valid Username and Password!";
include('login.php');
}
}
}else{
echo "You have to enter your username and password";
include ('login.php');
}
?>

If that doesnt work, try putting this above the first if(



$username = $_POST['username'];
$password = $_POST['password'];

franches
10-02-2004, 04:33 AM
thanks but it still goes back to the login form after pressing the add hours which is supposed to go back to the same page for logging another activity.

any other idea how would i do it? i really don't know what's missing with my code. i'm on a step by step learning it.

hoping for some more help guys. thanks

boeing747fp
10-02-2004, 12:27 PM
you dont have an action="" set or method="" set in the HTML form for the Add Hours

franches
10-04-2004, 04:00 AM
you dont have an action="" set or method="" set in the HTML form for the Add Hours

I've already put this on my form for Add hours

<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">

but still it goes back to my login form after pressing the add hours.

thanks a lot for spending sometime on my query. If you still have any ideas pls. tell me.

hoping for your response.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum