PDA

View Full Version : how does one use username in a hdden form field?


husnamina
08-01-2006, 02:56 AM
hello everyone
I am trying to have a user's username to be fed into a hidden form field automatically when the user clicks to fill out the form pls HELP.
I am wondering if i have to use $_GET but at the moment everything of mine is $_POST
Thanks
:confused:

Fumigator
08-01-2006, 03:03 AM
You can use $_POST, just reload the page when submit is clicked and put this in the hidden field area:


<input type="hidden" name="username" value="<?php echo $_POST['username']; ?>" />

husnamina
08-01-2006, 01:03 PM
thanx fumigator,
i have used it
result is not known yet as my login system as gone haywhere:mad:
please look at it
----login form
<h4>Login Form</h4>
<form name="lform" id="lform" method="post" action="authenticate.php">
<table >
<tr><td>
Username:
<input type="text" name="username" id="username" size="36" /></td>
<td>&nbsp;</td>
</tr>
<tr><td>
Password:
<input type="password" name="password" id="password" size="36" /></td><td>&nbsp;</td></tr>


<tr><td><input type="submit" name="lsubmit" id="lsubmit" value="Submit" align="right"/>
<input type="reset" name="lreset" id="lreset" value="Reset" /></td>
<td>&nbsp;</td>
</tr>
</table>
</form>

---authenticating script---

<?php
include('connection.php');
$username=$_POST['username'];
$password=$_POST['password'];
if (isset($submit)) // name of submit button
{
$query = "select (`username`,`password`) from users where username=\'$username\' and password=\'$password\'";
echo "select (username,password) from users where username='$username' and password='$password'";
$result = mysql_query($query) ;
$isAuth = false; //set to false originally
while($row = mysql_fetch_array($result))
{
if($row['username'] === $username)
//above row checks to see if username/password combination exists
{
$isAuth = true;
//session_start();
//session_register('username');
}
}
if($isAuth)
{
print "logged in successfully<br>";
print "<A href='index3.php'>Go to site</a>";
}
else
//if login/pass does not exist
{
print "Wrong username or password or you are not registered";
print " <a href='register2.php> Click to register</a>";
}
}
else
mysql_error();
?>


---register page---
<h4>Create User</h4>

<form name="register" id="register" method="post" action="?id=register">
<table >
<tr><td>
Username:
<input type="text" name="username" id="username" size="36" /></td>
<td>&nbsp;</td>
</tr>
<tr><td>
Password:
<input type="password" name="password" id="password" size="36" /></td><td>&nbsp;</td></tr>
<tr><td>Email
<input type="text" name="email" id="email" size="41" /></td>
<td>&nbsp;</td>
</tr>


<tr><td><input type="submit" name="usubmit" id="usubmit" value="Submit" align="right"/>
<input type="reset" name="ureset" id="ureset" value="Reset" /></td>
<td>&nbsp;</td>
</tr>
</table>
</form>
<?php
if(empty($_POST['username'])){
echo "Please, insert an username.<br>
<a href=?id=register>Go Back</a>.";
die();
}

if(empty($_POST['password'])){
echo "Please, insert a password.<br>
<a href=?id=register>Go Back</a>.";
die();
}
if(empty($_POST['email'])){
echo "Please, insert an email.<br>
<a href=?id=register>Go Back</a>.";
die();
}

include('connection.php');
//checking and filtering input from user
$_POST['username']=trim($_POST['username']); // this trims intial or end white spaces a user might put
$_POST['password']=trim($_POST['password']);
$_POST['email']=trim($_POST['email']);
/*if(!$_POST['username']|| !$_POST['password'])
{
echo '<h4>You have not entered complete information. Please try again.</h4>';
exit;
}*/
if (isset($_POST['usubmit']))
{
//handling of quotes and backslashes in text
if(!get_magic_quotes_gpc())
{
$_POST['username'] =addslashes($_POST['username']);
$_POST['password']=addslashes($_POST['password']);
$_POST['email']=addslashes($_POST['email']);
}
}

//querying the database

//$insert_query="INSERT INTO users (username, password,email) VALUES('".mysql_escape_string($_POST['username'])."','".mysql_escape_string($_POST['password'])."','".mysql_escape_string($_POST['email']."')";
$insert_query="INSERT INTO marks (username, password,email) VALUES('".mysql_escape_string($_POST['username'])."','".mysql_escape_string($_POST['password'])."','".mysql_escape_string($_POST['email']."')";

$result=mysql_query($insert_query);
if($result)
echo mysql_affected_rows($myConn).'row(s) <br>feedback is correctly posted, thank you';
else
echo "something amidst";
//closing the connection
mysql_close();


?>

Thanks

Fumigator
08-01-2006, 03:03 PM
my login system as gone haywhere

Any more detail on what you mean by haywhere?

Error messages, form behavior, any output you're getting, what actions do you take and what happens when you take them, etc.

husnamina
08-01-2006, 03:18 PM
yes
there were a few syntax errors before and i corrected them and now whn i run it
it goes quiet on on; nothing no error, no msge NOTHING!
AAAAAgh its so frustrating

Fumigator
08-01-2006, 03:22 PM
Start echoing variables so you can find out how far it's getting.

husnamina
08-01-2006, 04:41 PM
Thanks for the tip, it helped
I was able to get this error message

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /disks/diskh/zco/cohaa/public_html/tester1/authenticate.php on line 24

while($row = mysql_fetch_array($result)) // line 24

husnamina
08-01-2006, 06:22 PM
hi, i changeed that bit of line 24
so it now shows my error message; which means its not posting to backend and am reusing the code cos itw working in my other script.

$insert_query="INSERT INTO marks (username, password,email) VALUES('".$_POST['username']."','".$_POST['password']."','".$_POST['email']."'";
echo "INSERT INTO marks (username, password,email) VALUES('".$_POST['username']."','".$_POST['password']."','".$_POST['email']."'";
$result=mysql_query($insert_query);
if($result)
echo mysql_affected_rows($myConn).'row(s) <br>feedback is correctly posted, thank you';
else
echo "<br>something amidst";
mysql_error();
//closing the connection
mysql_close(); ----error msge---
INSERT INTO marks (username, password,email) VALUES('coyma','amin','coyma22@hotmail.com'

something amidst


:(:confused:

Fumigator
08-02-2006, 01:41 AM
Your variable $insert_query is missing his dollar sign.

husnamina
08-02-2006, 06:54 PM
Hey
sorted !!! :thumbsup:
i was trying to insert into the wrong table.LOL!
the login is working ok now thanx, i will try the hidden form element and see how we get on thanx people

husnamina
08-06-2006, 09:47 PM
hi fumigator
i tried to used the hidden form element for the usernae as u stated
<input type="hidden" name="username" value="<?php echo $_POST['username']; ?>" />

but this is what gets posted into the table field

---table content---
<br />
<b>Notice</b

hope to read from you soon. thanx.
if i want to you cookies how do i do that?:confused:

Fumigator
08-06-2006, 10:11 PM
echo $insert_query;

What does that display?

husnamina
08-06-2006, 11:33 PM
hi
this is my iisert query


$insert_query="insert into topics(topic_title,mod_id, topic_creator,topic_details) values (\"$title\",\"$mod\",\"$user\",\"$detail\")";


this is the error i get when i run it ie when i echoed

insert into topics(topic_title,mod_id, topic_creator,topic_details) values ("topic title goes here","COP212","
Notice: Undefined index: username in /disks/diskh/zco/cohaa/public_html/tester1/new_topic_form.php on line 20
","thuiknbvcxs")

---new_topic_form line 20----

<tr><td><input type="hidden" name="username" id="username"value="<?php echo $_POST['username']; ?>" /></td></tr>

arne
08-07-2006, 10:47 AM
<tr><td><input type="hidden" name="username" id="username"value="<?php echo $_POST['username']; ?>" /></td></tr>

should have a space between the "username" and the value
so:

<tr><td><input type="hidden" name="username" id="username" value="<?php echo $_POST['username']; ?>" /></td></tr>

kaydara
08-07-2006, 11:43 AM
havent read all but to make a point use:

<?=$_POST['username']?>

instead of:

<?php echo $_POST['username']; ?>

Fumigator
08-07-2006, 04:42 PM
havent read all but to make a point use:

<?=$_POST['username']?>

instead of:

<?php echo $_POST['username']; ?>

Those two statements are functionally identical, but the first one doesn't necessarily work for all installations of PHP, and is more cryptic than using echo. Why would this be an improvement?

husnamina
08-08-2006, 12:28 AM
hello people
thank you for all the suggestions, but i got it sorted using cookies
i set cookie in the login script to get the user name and i used the $_COOKIE super global to retrieve the value of the cookie and insert it into the fom .

---in my form----

<tr><td><input type="hidden" name="username" id="username"value="<?php echo $_COOKIE['UserName']; ?>" /></td></tr>


---in the login script---
<?php setcookie("UserName", $_POST['username'], time()+43200); ?>

thanx for the good support :thumbsup: