5 Replies - 443 Views - Last Post: 16 August 2010 - 04:13 PM Rate Topic: -----

#1 gymangel812  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 116
  • Joined: 15-February 10

POST[submit] Problem?

Posted 16 August 2010 - 01:54 PM

I'm having a problem with using POST[submit]. I have a page that has a side bar with a box that runs a search. I also have a page where a user can log in. The problem is when I try to log in (login.php), the search.php runs when I click the log in button. How can I fix this? Is there a way to name seperate _POST to check which button was clicked (or which text box was submitted by pressing enter)?
Code for Login.php
<?php # login.php
// This is the login page for the site.

// Include the configuration file for error management and such.
require_once ('includes/config.inc'); 

// Set the page title and include the HTML header.
$page_title = 'Login';
include ('includes/header.html');

if (isset($_POST['submit'])) { // Check if the form has been submitted.

	require_once ('mysqli_connect.php'); // Connect to the database.
	
	if (empty($_POST['username'])) { // Validate the username.
		$u = FALSE;
		echo '<p><font color="red" size="+1">You forgot to enter your username!</font></p>';
	} else {
		$u = escape_data($_POST['username']);
	}
	
	if (empty($_POST['password'])) { // Validate the password.
		$p = FALSE;
		echo '<p><font color="red" size="+1">You forgot to enter your password!</font></p>';
	} else {
		$p = escape_data($_POST['password']);
	}
	
	if ($u && $p) { // If everything's OK.
	
		// Query the database.
		$query = "SELECT id, username FROM users WHERE username='$u' AND password=PASSWORD('$p')";	

		$result = mysqli_query ($dbc, $query);
		$row = mysqli_fetch_array ($result); 
		
		if ($row) { // A match was made.
				
				// Start the session, register the values & redirect.
				$_SESSION['username'] = $row[1];
				$_SESSION['id'] = $row[0];
				
				ob_end_clean(); // Delete the buffer.
				
				header ("Location:  http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/index.php");
				exit();
				
		} else { // No match was made.
			echo '<p><font color="red" size="+1">The username and password entered do not match those on file.</font></p>'; 
		}
		
		mysqli_close($dbc); // Close the database connection.
		
	} else { // If everything wasn't OK.
		echo '<p><font color="red" size="+1">Please try again.</font></p>';		
	}
	
} // End of SUBMIT conditional.
?>

<h1>Login</h1>
<p>Your browser must allow cookies in order to login.</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<p><b>User Name:</b> <input type="text" name="username" size="10" maxlength="20" value="<?php if (isset($_POST['username'])) echo $_POST['username']; ?>" /></p>
<p><b>Password:</b> <input type="password" name="password" size="20" maxlength="20" /></p>
<div align="center"><input type="submit" name="submit" value="Login" /></div>
</fieldset></form><!-- End of Form -->
<br>
<p>Not a registered user yet? <a href="add_user.php">Click here to sign up for an account</a></P>
<?php // Include the HTML footer.
include ('includes/footer.html');
?>


Header.html
<?php # Script 17.2 - header.html

// Start output buffering and initialize a session.
ob_start();
session_start();

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ultimate Video Game Connection</title>
<link rel="stylesheet" media="all" type="text/css" href="css/styles.css" />
<style type="text/css">
<!--
body,td,th {
	color: #080C58;
}
-->
</style></head>

<body>
<div id="outer">
<div id="nav_bar"><img src="images/header1.jpg" width="950" height="107" alt="logo" />
  <div class="container">
    <ul class="nav">
		<li><a href="#url">Home</a></li>
	</ul>
	<ul class="nav">
		<li class="sub-li"><a class="sub-a" href="#url">Nintendo</a>
			<ul>

				<li><a href="browse_selections.php">NES</a></li>
				<li><a href="browse_snes.php">Super Nintendo (SNES)</a></li>
				<li><a href="#url">Gamecube</a></li>
				<li><a href="#url">Game Boy Advance (GBA)</a></li>
			</ul>
		</li>
	</ul>
    	<ul class="nav">
		<li class="sub-li"><a class="sub-a" href="#url">Microsoft</a>
			<ul>

				<li><a href="#url">Email</a></li>
				<li><a href="#url">Telephone</a></li>
				<li><a href="#url">Online Form</a></li>
				<li><a href="#url">Snail Mail Address</a></li>
			</ul>
		</li>
	</ul>
    	<ul class="nav">
		<li class="sub-li"><a class="sub-a" href="#url">Sony</a>
			<ul>

				<li><a href="#url">Email</a></li>
				<li><a href="#url">Telephone</a></li>
				<li><a href="#url">Online Form</a></li>
				<li><a href="#url">Snail Mail Address</a></li>
			</ul>
		</li>
	</ul>
        <ul class="nav">
		<li class="sub-li"><a class="sub-a" href="#url">Sega</a>
			<ul>

				<li><a href="#url">Email</a></li>
				<li><a href="#url">Telephone</a></li>
				<li><a href="#url">Online Form</a></li>
				<li><a href="#url">Snail Mail Address</a></li>
			</ul>
		</li>
	</ul>
    	<ul class="nav">
		<li class="sub-li"><a class="sub-a" href="#url">Other</a>
			<ul>

				<li><a href="#url">Email</a></li>
				<li><a href="#url">Telephone</a></li>
				<li><a href="#url">Online Form</a></li>
				<li><a href="#url">Snail Mail Address</a></li>
			</ul>
		</li>
	</ul>
	    	<ul class="nav">
		<li class="sub-li"><a class="sub-a" href="#url">About</a>
			<ul>

				<li><a href="#url">About Us</a></li>
				<li><a href="#url">FAQ</a></li>
			</ul>
		</li>
	</ul>
        <ul class="nav">
		<li><a href="#url">Contact</a></li>
	</ul>
            <ul class="nav">
		<li><a href="#url">View Cart</a></li>
	</ul>


</div></div><br />
<div id="main_content">
<table width="875" border="0" cellspacing="2" cellpadding="5">
  <tr valign="top">
  <td width="35%" valign="top"> 
  <div id="xsnazzy">
<b class="xtop"><b class="xb1"></b><b class="xb2"></b><b class="xb3"></b><b class="xb4"></b></b>
<div class="xboxcontent">
<h2>Enter A Game Name to Search</h2>  
<center><p>
<form method="post" action="search.php">
                        <input type="text" name = "select_name" id="search" class="search_box_font" value="Search for a game!" onfocus="if (this.value == 'Search for a game!') this.value = '';" onblur="if (this.value == '') this.value = 'Search for a game!';" maxlength="255" />
            <input type="image" class="button" src="images/btn_search.png" />
          </form></p>
</div>
<b class="xbottom"><b class="xb4"></b><b class="xb3"></b><b class="xb2"></b><b class="xb1"></b></b></center>
</div>

<div id="xsnazzy">
<b class="xtop"><b class="xb1"></b><b class="xb2"></b><b class="xb3"></b><b class="xb4"></b></b>
<div class="xboxcontent">
<p>
<?php
// Welcome the user (by name if they are logged in).
if (isset($_SESSION['username'])) {
        echo '<h2>Welcome, ';
	echo "{$_SESSION['username']}!</h2>";
	echo '<p><br /><a href="logout.php">Logout</a>';
	echo '<br /><a href="discounts.php">Available Coupon Codes</a>';
}
else {
        echo '<h2>Welcome, Guest ';
	//echo "{$_SESSION['username']}!</h2>";
	echo '<br /><a href="login.php">Click here to sign in</a> ';

}
?> </p>

</p>
</div>
<b class="xbottom"><b class="xb4"></b><b class="xb3"></b><b class="xb2"></b><b class="xb1"></b></b>
</div>

<div id="xsnazzy">
<b class="xtop"><b class="xb1"></b><b class="xb2"></b><b class="xb3"></b><b class="xb4"></b></b>
<div class="xboxcontent">
<h2>Sign Up for the Newsletter</h2>
<form method="post" action="search.php">
                        <center><input type="text" name = "select_name" id="search" class="search_box_font" value="Enter Your E-Mail" onfocus="if (this.value == 'Enter Your E-Mail') this.value = '';" onblur="if (this.value == '') this.value = 'Enter Your E-Mail';" maxlength="255" />
            <input type="image" class="button" src="images/mail-icon.png" /></center>
</div>
<b class="xbottom"><b class="xb4"></b><b class="xb3"></b><b class="xb2"></b><b class="xb1"></b></b>
</div>

</td>
    <td width="65%">


Search.php
<?php # Script 17.5 - browse_selections.php
// This page displays the available selections (products).

// Set the page title and include the HTML header:
$page_title = 'Browse the Video Games';
include ('includes/header.html');

$searchNotFound  = false;

$name         = '';

if (!isset($_POST['select_name'])) {

  //page is opened directly - set not found flag on true
  $searchNotFound  = true;

} else {

$name = trim($_POST['select_name']);

require_once ('mysqli_connect.php');

// Default query for this page:
$q = "SELECT productid, productName, price, image_name, systemName
          FROM products, systems
          WHERE systems.systemid = products.systemid
          AND productName LIKE '%".$name."%'
          ORDER BY systemName ASC;";

$num_results  = 0;

// Are we looking at a particular selection?
if (isset($_GET['productid']) && is_numeric($_GET['pid']) ) {
	$pid = (int) $_GET['pid'];
	if ($pid > 0) { // Overwrite the query:
		echo "PID: $pid";
		$q = "SELECT productid, productName, price, image_name, systemName FROM products, systems, genre
		WHERE productid = $pid AND systems.systemid = products.systemid";
	}
}

// Create the table head:
echo '<table border="0" cellspacing="2" cellpadding="4">';

// Display all the selections, linked to URLs:
$result = mysqli_query ($dbc, $q);;
 // If the result is not empty
    $num_results = mysqli_num_rows($result);

  if( $num_results ) {
		while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)) {

			// Display each record:
			echo "\t<tr>
				<td width=\"40%\" align=\"middle\"><a href=\"view_selection.php?pid={$row['productid']}\"><p>{$row['productName']}</a></p></td>
				<td rowspan=\"3\" align=\"right\" width=\"60%\"><img src=images2/{$row['image_name']} width=75></td>
				</tr>\n
				\t<tr>
				<td align=\"middle\"><p>{$row['systemName']}</p></td>
				</tr>\n
				\t<tr>
				<td align=\"middle\"><p>\${$row['price']}</p></td>
				</tr>\n
			</tr>\n";


		} // End of while loop.

      // Free up the resources.
      mysqli_free_result($result);

    } else {
      // wanted string not found - set not found flag on true
      $searchNotFound  = true;
    }

    // Close the database connection.
    mysqli_close($dbc);

	if( $searchNotFound ) {
			echo '<p>No result matching the search criterion.</p>';
	}
}
include ('includes/footer.html');
?>



Is This A Good Question/Topic? 0
  • +

Replies To: POST[submit] Problem?

#2 teampoop  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 140
  • Joined: 19-June 08

Re: POST[submit] Problem?

Posted 16 August 2010 - 02:11 PM

If your search.php logic is being called, that means there's some kind of error in your HTML.. Mainly your form action. I would recommend hard coding it, instead of PHP_SELF.

On another note, this post sure would have been nicer to read if you wrapped your code in the proper code tags.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4188
  • View blog
  • Posts: 11,852
  • Joined: 18-April 07

Re: POST[submit] Problem?

Posted 16 August 2010 - 02:22 PM

Keep in mind that you have to change the action attribute for each of your forms. I noticed that your email form where you have the newsletter signup is still using action="search.php" which I don't think is what you want to be doing.

:)
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: POST[submit] Problem?

Posted 16 August 2010 - 02:22 PM

The code is in code tags? Nicely separated into the separate files too?
Was This Post Helpful? 0
  • +
  • -

#5 gymangel812  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 116
  • Joined: 15-February 10

Re: POST[submit] Problem?

Posted 16 August 2010 - 03:30 PM

View Postteampoop, on 16 August 2010 - 01:11 PM, said:

If your search.php logic is being called, that means there's some kind of error in your HTML.. Mainly your form action. I would recommend hard coding it, instead of PHP_SELF.

On another note, this post sure would have been nicer to read if you wrapped your code in the proper code tags.

Hmm I'm not sure what you mean by hard coding instead of using self?
I though I did wrap it in the [*code*] tags? I clicked the little code button and pasted it in there...

View PostMartyr2, on 16 August 2010 - 01:22 PM, said:

Keep in mind that you have to change the action attribute for each of your forms. I noticed that your email form where you have the newsletter signup is still using action="search.php" which I don't think is what you want to be doing.

:)

Yes I have not done anything with the newsletter portion yet.

Actually I just changed it to newsletter.php (without making the page) and it is trying to go there (obviously saying the page is not found).

View PostJackOfAllTrades, on 16 August 2010 - 01:22 PM, said:

The code is in code tags? Nicely separated into the separate files too?

Yes each seperate code section is a different file. Login logs the user in. Header.html displays the header and side bar. Search.php searches for a string that is entered in the text box of the side bar (which is in the header.html).

This post has been edited by gymangel812: 16 August 2010 - 03:33 PM

Was This Post Helpful? 0
  • +
  • -

#6 gymangel812  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 116
  • Joined: 15-February 10

Re: POST[submit] Problem?

Posted 16 August 2010 - 04:13 PM

Hmm, I just removed the newsletter portion of the side bar and now everything works...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1