Jump to content

validating user input...


Derleek

Recommended Posts

This is basically dreamweaver code for login section. Dont be panic if you cant understand most of stuffm, only view basic code that is mark up. This is for login section in which if username and password from mysql table admin are not mathced than it gives error as by get method. I dont write code fully here.

 

 

 

<?php require_once('../Connections/mycon.php'); ?>

<?php

// *** Validate request to login to this site.

if (!isset($_SESSION)) {

  session_start();

}

 

$loginFormAction = $_SERVER['PHP_SELF'];

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

  $_SESSION['PrevUrl'] = $_GET['accesscheck'];

}

 

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

  $loginUsername=$_POST['username'];

  $password=$_POST['password'];

  $MM_fldUserAuthorization = "";

  $MM_redirectLoginSuccess = "donor.php";

  $MM_redirectLoginFailed = "index.php?invalid=1";

  $MM_redirecttoReferrer = false;

  mysql_select_db($database_mycon, $mycon);

 

$LoginRS__query=sprintf("SELECT uername, password FROM admin WHERE uername='%s' AND password='%s'",

    get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password));

 

  $LoginRS = mysql_query($LoginRS__query, $mycon) or die(mysql_error());

  $loginFoundUser = mysql_num_rows($LoginRS);

  if ($loginFoundUser) {

    $loginStrGroup = "";

   

    //declare two session variables and assign them

    $_SESSION['MM_Username'] = $loginUsername;

    $_SESSION['MM_UserGroup'] = $loginStrGroup;      

 

    if (isset($_SESSION['PrevUrl']) && false) {

      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];

    }

    header("Location: " . $MM_redirectLoginSuccess );

  }

  else {

    header("Location: ". $MM_redirectLoginFailed );

  }

}

?>

 

 

<?php if(isset($_GET['invalid']) && $_GET['invalid']==1) { ?>

<table> 

  <tr class="box">

          <td colspan="2" bgcolor="#FFFFCC"><div align="center"><span  style="color:#FF0000; font-size:14px;"  >

  <?php echo "Invalid Username/Password."; ?>  </span></div></td>

        </tr>

</table>

  <?php } ?>

you validate either by javascript or either by php.

 

No.  You always validate with PHP.  You can additionally validate with JavaScript.  But you can never, ever, not in a million years, use only JavaScript for your validation.

 

If you follow the links in my signature, I have provided a couple of simple tutorials on form processing.  As part of the tutorial I show how to re-populate the form fields with previously submitted data.  I only do this for a single text field, but the principle applies easily to other types of fields.

 

For select fields, when creating the options you check if an option's value is the same as the one in $_POST and if it is, add selected to the option tag.

<?php
$opts = array( 1, 2, 3 );
echo '<select name="mySelect">';
foreach($opts as $opt){
  echo sprintf('<option value="%s"%s>', $opt, $opt == $_POST['mySelect'] ? ' selected' : '');
}
echo '</select>';
?>

 

The same thing applies to check boxes, only you use checked instead of selected.

 

For textareas, you just place the default between the tags:

echo sprintf('<textarea name="myText">%s</textarea>', $_POST['myText']);

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.