Form POST Question (need more help)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tmaiden
Forum Commoner
Posts: 64
Joined: Fri Feb 24, 2006 3:15 pm
Location: Philadelphia
Contact:

Form POST Question (need more help)

Post 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?
Last edited by tmaiden on Thu Aug 31, 2006 3:39 pm, edited 2 times in total.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

looks like you are posting to a template script... you would need to post the form to itself
User avatar
tmaiden
Forum Commoner
Posts: 64
Joined: Fri Feb 24, 2006 3:15 pm
Location: Philadelphia
Contact:

Post 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?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

post it to index.php... logon.php is just a template, right?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Code: Select all

echo '<pre>';
print_r($_POST);
echo '</pre>';
Check your form method, enctype, formnames and name case.
User avatar
tmaiden
Forum Commoner
Posts: 64
Joined: Fri Feb 24, 2006 3:15 pm
Location: Philadelphia
Contact:

Post 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?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

when you echo out that value, what does it say?
User avatar
tmaiden
Forum Commoner
Posts: 64
Joined: Fri Feb 24, 2006 3:15 pm
Location: Philadelphia
Contact:

Post 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?
Last edited by tmaiden on Thu Aug 31, 2006 3:41 pm, edited 1 time in total.
User avatar
tmaiden
Forum Commoner
Posts: 64
Joined: Fri Feb 24, 2006 3:15 pm
Location: Philadelphia
Contact:

Post by tmaiden »

For some reason I think im losing the session variable: $_SESSION['username']
Post Reply