Page 1 of 1
Form POST Question (need more help)
Posted: Thu Aug 31, 2006 1:44 pm
by tmaiden
My main page,
http://www.site.com/index.php, is setup like this.
Code: Select all
<?php
include 'templates/header.php';
...
include 'templates/forms/logon.php';
...
include 'templates/footer.php';
?>
The problem I'm having is with this, the
http://www.site.com/templates/forms/logon.php
Code: Select all
<?php
include('config.php');
if(isset($_POST['username'])){
echo "worked!";
}
else{
echo "<form method=\"post\" action=\"logon.php\">
<div id=\"sideform\">
<input type=\"text\" name=\"username\" size=\"15\" class=\"\" maxLength=\"20\">
<input type=\"submit\" name=\"submit\" value=\"Logon\">
</div>
</form>";
}
?>
After clicking the "Logon" button, I never get to the the word "worked!"
Any reason why?
Posted: Thu Aug 31, 2006 1:46 pm
by Luke
looks like you are posting to a template script... you would need to post the form to itself
Posted: Thu Aug 31, 2006 1:53 pm
by tmaiden
I created the template myself.
It's basically just a php file that gets printed out.
And isn't it posting to itself? If not, how come and how can I make it do so?
Posted: Thu Aug 31, 2006 1:58 pm
by Luke
post it to index.php... logon.php is just a template, right?
Posted: Thu Aug 31, 2006 2:00 pm
by Benjamin
Code: Select all
echo '<pre>';
print_r($_POST);
echo '</pre>';
Check your form method, enctype, formnames and name case.
Posted: Thu Aug 31, 2006 2:03 pm
by tmaiden
Worked.
I set where it posts to as "substr($_SERVER['REQUEST_URI'], 1)"; since the logon is on top of all of my pages.
Can you explain why this is so?
Posted: Thu Aug 31, 2006 2:04 pm
by Luke
when you echo out that value, what does it say?
Posted: Thu Aug 31, 2006 3:25 pm
by tmaiden
Another question based on this.....
If my main page,
http://www.site.com/index.php,
AND my news page,
http://www.site.com/news.php, are setup like this.
Code: Select all
<?php
include 'templates/header.php';
...
include 'templates/forms/logon.php';
...
include 'templates/footer.php';
?>
With the modifications I made to
http://www.mysite.com/templates/forms/logon.php
Code: Select all
<?php
include('config.php');
if(isset($_POST['username'])){
//Connect to database
mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$username = $_POST['username'];
$password = md5($_POST['password']);
$query = "SELECT * FROM ff_users WHERE username = '" . $username . "' and password = '" . $password . "'";
$result = mysql_query($query);
if (! mysql_num_rows($result)){
echo " <form name=\"login\" method=\"post\" action=\"" . substr($_SERVER['REQUEST_URI'], 1) . "\">
<div id=\"sideform\">
<input type=\"text\" name=\"username\" size=\"15\" class=\"\" maxLength=\"20\">
<input type=\"password\" name=\"password\" size=\"15\" class=\"\" maxLength=\"20\">
<input type=\"submit\" name=\"submit\" value=\"Logon\">
</div>
</form><br><b>Invalid Username or Password</b>";
}
else{
$_SESSION['username'] = $username;
}
}
if(isset($_SESSION['username'])){
echo "<div id=\"sideform\">Welcome, <b>" . $_SESSION['username'] . "</b></div></form>";
}
else{
echo " <form name=\"login\" method=\"post\" action=\"" . substr($_SERVER['REQUEST_URI'], 1) . "\">
<div id=\"sideform\">
<input type=\"text\" name=\"username\" size=\"15\" class=\"\" maxLength=\"20\">
<input type=\"password\" name=\"password\" size=\"15\" class=\"\" maxLength=\"20\">
<input type=\"submit\" name=\"submit\" value=\"Logon\">
</div>
</form>";
}
?>
Why do I lose $_SESSION['username'] when I switch better the pages news and main?
Posted: Thu Aug 31, 2006 3:40 pm
by tmaiden
For some reason I think im losing the session variable: $_SESSION['username']