Jump to content

Including forms


rtpmatt

Recommended Posts

I am making a webpage, where most of the page is static, only one section is changing.  I am trying to keep the one main page with all of the static stuff, and simply have an if statement that includes different php files based on some condition.  This seems to work fine until i include a page with a form on it, at which point the for will not work.

 

I have tried every combination I can think of.  I have set the form action to the form page, and to main page.  i have put the start_session everywhere.  it doesn't seem to matter what i do the form will not work.

 

I am using an object, and am using a MySQL database (i have created a user object that can do things such as login or set password) and it does not seem to matter where i put the php for the form action, or the code for the database interaction, none of it seems to work through the include.

 

i'm sure i'm not the first person to try this.  I was just hoping someone could let me know the best way to do this, or if this is a bad way, what a better way is.

 

thanks for your time

 

-matt

Link to comment
https://forums.phpfreaks.com/topic/43097-including-forms/
Share on other sites

ok, sorry i should have included code the first time.  my general idea is this

 

index.php:

<?php
include 'opendb.php';
include 'userFctns.php';
session_start();

//Setup current user
if($_SESSION['loggedUser'] == null)
	$_SESSION['loggedUser'] = new user();

        mysql_close($conn);
?>
...A bunch of HTML...
<?php 
if ($_SESSION['page'] == "login") 
    include 'loginBox.php';
?>
...lots more html...

 

then in my loginBox.php:

<?php
        session_start();
if (isset($_POST['submit'])){
	$uname=$_POST['uname'];
    		$pwd=md5($_POST['pswd']);
	$loggedUser->loginUser($uname, $pwd);
}
?>	
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
Member Login:
<BR>
Login:<input type="text" name="uname" size="20">
<BR>
Password: <input type="password" name="pswd" size="20">
<BR>
<center>
<input type="submit" value="Submit" name="submit">
<input type="reset" value="Reset" name="ResetButton">
</center>
</form>			

 

and in my userFctn file:

<?php
class user {
	private $username;
	private $validUser;

	public function __construct() {
		$this->validUser = false;
		$this->username = ""
	}

	public function loginUser($uname, $pwd) {		
		var_dump($conn);
		$query = "SELECT UserName, Password FROM userInfo WHERE UserName='$uname';";
		$queryRes = mysql_query($query);
		$row = mysql_fetch_row($queryRes);

		if ($row[1] == $pwd) {
			$this->validUser = true;
			$this->username = $uname;
			$_SESSION['loggedUser'] = $this;
			redirect("blankPage.php?page=home");
		}
		else {
			$this->validUser = false;
			$_SESSION['loggedUser'] = null;
			echo "error loggin in";
		}
	}
}
?>

 

It all works fine if my index file doesn't use an include (instead the actual code is in it).  It doesn't seem to matter where i put the php or set the form action to, it does not want to work.  I would really like to have the code for the login fctn in the login file.  it seems like this should work, am i missing something?

Link to comment
https://forums.phpfreaks.com/topic/43097-including-forms/#findComment-209600
Share on other sites

It seems that my variables are never making it to the form, I am unable to access my DB, or anything else.  I'm sure there is something simple i'm missing.  If anybody has any ideas it would be much appreciated.

 

BTW the error im currently getting is:

 

Warning: mysql_query() [function.mysql-query]: Access denied for user 'www-data'@'localhost' (using password: NO) in /var/www/test/userFctns.php on line 42

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /var/www/test/userFctns.php on line 42

 

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /var/www/test/userFctns.php on line 43

Link to comment
https://forums.phpfreaks.com/topic/43097-including-forms/#findComment-209735
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.