Jump to content

[SOLVED] problems with switch and form


garry

Recommended Posts

Well, I've set up a login function and I wanted to make the url for the login /index.php?action=login so I used a switch statement on the index page. Here's the code I use for the switch:

 

<?php

if (isset($_GET['action'])) {

$action = $_GET['action'];

switch ($action) {

case "login":
include (DOCUMENT_ROOT . 'includes/login.php');
break;

case "register":
include (DOCUMENT_ROOT . 'includes/register.php');
break;

	}
}
else {
echo "Welcome to our website!";
}

 

So that works fine when i go to the url index.php?action=login. But the problem comes with the form.. Here's the login.php code:

 

<?php

if (isset($_POST['submitted'])) {

$username = $_POST['username'];
$password = $_POST['password'];

$query = "SELECT * 
		  FROM users
		  WHERE username = '$username'
		  AND password = '$password'
		 ";

$result = mysql_query($query);

$row = mysql_fetch_array($result);

if (mysql_num_rows($result) == 0) {
	echo "Sorry, no match found!";
	die();
	}

if (mysql_num_rows($result) == 1) {


	$_SESSION['username'] = $row['username'];
	$_SESSION['firstname'] = $row['firstname'];

	echo "Welcome " . $row['firstname'] . "!";
	}

unset($_POST); // Unset the post values so the form can be resubmitted in the future if necessary
}

else if (isset($_SESSION['username'])) { // If the user is already logged in, display a message
echo "Oops! You\'re already logged in";
}


else {
?>
<div class="login">

<fieldset title="Login">
<legend><heading>Login<heading></legend>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<table width="300">
  <tr>
    <td width="120">Username:</td>
    <td width="180"><input name="username" type="text" size="20" maxlength="30"></td>
  </tr>
  <tr>
    <td>Password:</td>
    <td><input name="password" type="password" size="20" maxlength="30"></td>
  </tr>
  <tr>
    <td><input name="submitted" type="hidden" value="1"></td>
    <td><input name="submit" type="submit" value="Login"></td>
  </tr>
</table>
</form>
</fieldset>
</div>
<?php
     ;
 	} 

     ?>

 

So what's happening is the form action is using the index.php page however, instead of running the if (isset($_POST['submitted'])) part, it just outputs: "Welcome to our website". Does anybody have any idea on how I can fix this?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/106049-solved-problems-with-switch-and-form/
Share on other sites

<?php echo $_SERVER['PHP_SELF']; ?>

 

should be

 

<? $PHP_SELF ?>

 

OR

 

<? $SERVER['PHP_SELF'] ?>

 

Dont need to echo it or if you plan to you should use it with entities like

 

echo htmlentities($SERVER['PHP_SELF']);

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.