Jump to content

Recommended Posts

I am using this code to build a form:

echo '<form method="post" action="mysql.php">';

echo '<table>';

echo '<tr><td>UserID:</td>';

echo '<td><input type="text" name="username"></td></tr>';

echo '<tr><td>Password:</td>';

echo '<td><input type="password" name="password"></td></tr>';

echo '<tr><td colspan="2" align="center">';

echo '<input type="submit" value="Log In"></td></tr>';

echo '</table></form>';

 

Alone, this form works, but when I add script above it to make it more functional, everything loads properly and I get no errors, but the form will not submit. The submit button is like a dead button.

Anyone know why this could be happening?

Link to comment
https://forums.phpfreaks.com/topic/246262-form-not-working/
Share on other sites

<?php

session_start();

if (isset($_POST['username']) && isset($_POST['password']))
   {
      $username = $_POST['username'];
      $password = $_POST['password'];

      $link = mysql_connect('localhost', 'root', 'root', 'users');
         if (!$link)
            {
               die("Cannot Connect".mysql_error());
            }

   


$query = "select * from users where username='$username' and password=sha1('$password')";
$result = mysql_query($query);
if ($result->num_rows)
{
$_SESSION['valid_user'] = $username;
}
$link->close();
}
?>



<html>
<body>

<?php

if (isset($_SESSION['valid_user']))
   {
      echo 'You are logged in as: '.$_Session['valid_user'].' <br>';
      echo '<a href="logout.php">Log out</a><br>';
   }
   else
   {
      if (isset($username))
      {
         echo 'Could not log you in.<br>';
      }
      else
      {
         echo 'You are not logged in.<br.';
      }


echo '<form method="post" action="mysql.php">';
echo '<table>';
echo '<tr><td>UserID:</td>';
echo '<td><input type="text" name="username"></td></tr>';
echo '<tr><td>Password:</td>';
echo '<td><input type="password" name="password"></td></tr>';
echo '<tr><td colspan="2" align="center">';
echo '<input type="submit" name=submit value="Log In"></td></tr>';
echo '</table></form>';

}

?>
<a href="members_only.php">Members Section</a>
</body>
</html>

 

MOD EDIT: code tags added.

Link to comment
https://forums.phpfreaks.com/topic/246262-form-not-working/#findComment-1264701
Share on other sites

When posting code, enclose it within the forum's

 . . . 

BBCode tags.

 

Variable names are case sensitive.

echo 'You are logged in as: '.$_Session['valid_user'].' <br>'; // <---- $_SESSION, not $_Session

Link to comment
https://forums.phpfreaks.com/topic/246262-form-not-working/#findComment-1264704
Share on other sites

I like to get into the habit of setting variables with indices into alternative variables to avoid syntax errors, and for the overall appeal of the code..

 

something like

 

$valid_user = $_SESSION['valid_user'];//proceed using $valid_user

Link to comment
https://forums.phpfreaks.com/topic/246262-form-not-working/#findComment-1264716
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.