2 Replies - 4936 Views - Last Post: 28 May 2011 - 08:54 AM Rate Topic: -----

#1 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Separating PHP and HTML problem [SOLVED]

Posted 23 May 2011 - 02:24 PM

I'm attempting to follow good web site design by separating my HTML layout from my PHP code.

I'm experiencing a problem though:
I have a variable $error in my HTML which is suppose to print an error message if the user leaves either his first name or password blank upon signing up for the site. The PHP is working fine. I'm debugging it, and all the right echo statements I'm using to debug are printing. The problem is in the HTML. It's not showing $error at all.

Here's the code:

HTML:
<div id="signsection">
			<pre>
			<form method='post' action='testApple.php' class="signup">
			<fieldset>
			<legend>Create an Account</legend>
			<div>
				<span class="names">First Name</span>	<input class="signForm" type='text' name='fName' /><br />
				<span class="names">Last Name</span>	<input class="signForm" type='text' name='lName'  /><br />
				<span class="names">Email Address</span>	<input  class="signForm" type='text' name='emailA'  /><br />
				<span class="names">Password</span>	<input class="signForm" type='password' name='pass'  /><br />
			     
				<input class="signButton" type='image' src="GuestbookAi3.gif" value='Create Account' />
			</div>
			</fieldset>
			</form>
			</pre>
		</div>
		<div id="error">
			<?php echo $error ?>
		</div>



And here's the corresponding PHP:
<?php // Page that allows users to join the network
include_once("index_layout.php");// used to include the basic header that is on every page, and to use any functions we may need from znfunctions.php
include_once("appleFunctions.php");
$error = $user = $pass = "";

if(isset($_SESSION['user'])) destorySession();// if the session variable $_SESSION['user'] has been set, destroy it

if(isset($_POST['fName']))// if the form has been submitted $POST['user'] will have a value
{
echo "hello";
	$user = sanitizeString($_POST['fName']);// so if it's been submitted, set $user to the sanitized version of whatever was in the user field
	$pass = sanitizeString($_POST['pass']);// if it's been submitted, set $pass to the sanitized version of whatever was in the user field
	
	if($user == "")
	{
		echo "sdfhskjfhs";
	}
	echo $user ;
	// username and password validation
	if($user == "" || $pass == "")// if either of the fields were left blank..
	{
	echo "here";
		$error = "Not all fields were entered<br /><br />";// set $error to an error message
	}
	else// if neither of the fields were left blank...
	{
		$query = "SELECT * FROM znmembers WHERE user='$user'";// a query to find a user with the name that was submitted in the form that wasn't blank
		
		if(mysql_num_rows(queryMysql($query)))// call queryMysql to get the results of the above query. If there aren't zero rows the name has been taken
		{
			$error = "That username already exists<br /><br />";// set $error to an $error message
		}
		else// if the username entered wasn't already taken..
		{
			$query = "INSERT INTO znmembers VALUES('$user', '$pass')";// a query to inset the submitted username and password into the znmembers table
			queryMysql($query);// perform the query
			die("<h4>Accout created</h4>You may now Log in.");
		}
	}
}

 
 ?>




Can't seem to figure out what's going wrong.

Thank you!

EDIT: Sorry. Stupid me forget to include the php page in the HTML page.

This post has been edited by eZACKe: 23 May 2011 - 02:30 PM


Is This A Good Question/Topic? 1
  • +

Replies To: Separating PHP and HTML problem [SOLVED]

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Separating PHP and HTML problem [SOLVED]

Posted 23 May 2011 - 04:41 PM

Now you're on your way to better coding practices. The next thing you may want to do is omit the use of die and redirect users to a page with proper information whether it be about a failed or successfully completed process or errors.
Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2836
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Separating PHP and HTML problem [SOLVED]

Posted 28 May 2011 - 08:54 AM

You really can't include a PHP file in an HTML file. What's really happening is that your HTML file is treated as a PHP file with breaks for HTML:

Server receives request --> runs PHP interpreter --> PHP interpreter dumps HTML code out to browser, occasionally executing a PHP command or two (ie, <?php echo $error ?> ).

This is confusing and not as helpful as it could be. It's generally better to let PHP read in the HTML as it would any text file and then make changes to the text using functions like str_replace() and preg_replace() before sending it to the browser. That way you never have to wonder where things are: PHP is in the PHP file and HTML is in the HTML file!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1