Jump to content

Validate Form Help


jigsawsoul

Recommended Posts

Here is my page, which checks to see if the email and password fileds are empty which works fine.

 

so what it does is check the username and returns to register.php is its blank.

 

but i want it to check all fields then return which ones are blank.

 

so if both are blank, ill get a message somewhat like;

 

  • A email address is required.
  • A password is required.

 

Anyhelp would be great thanks

 

 

 

 

<?php

session_start();
ob_start();

include('../../resources/config.php');
include($config["paths"]["resources"] . 'opendb.php');
include($config["paths"]["resources"] . '_library/login.php');

$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);

if(empty($_POST['email'])){
	$_SESSION["message"] = 
	   'A email address is required.';
	header ('Location: register.php');
	exit();
}

if(empty($_POST['password'])){
	$_SESSION["message"] = 
	   'A password is required.';
	header ('Location: register.php');
	exit();
}

$result = mysql_query("SELECT email FROM projectlogin WHERE email = '$email'");
$emailcheck = mysql_num_rows($result) or die(mysql_error());

if($emailcheck > 0){
	$_SESSION["message"] = 
	   'This email address is already registered with us. Having trouble, you can <a href="recover.php">reset your password here</a>.';
	header ('Location: register.php');
	exit();
}

include($config["paths"]["resources"] . 'closedb.php');

?>

Link to comment
https://forums.phpfreaks.com/topic/195705-validate-form-help/
Share on other sites

Just so you understand why it does not display them in a line, you do a redirect after each error. If you want to notify someone of all empty, you should collect the messages and then do a redirect if the message variable is not empty.

 

include($config["paths"]["resources"] . '_library/login.php');
    $_SESSION['message'] = "";
    if (empty($_POST['email'])) 
            $_SESSION['message'] .= "A email address is required.<br />";

     if (empty($_POST['password']))
            $_SESSION['message'] .= "A Password is required.<br />";

     if (!empty($_SESSION['message'])) {
             header("Location: register.php");
             exit();
     }

    // moved this below, as there is no need to try and escape empty data
    $email = mysql_real_escape_string($_POST['email']);
    $password = mysql_real_escape_string($_POST['password']);
   
   // rest of the processing

 

Should do what you want.

Link to comment
https://forums.phpfreaks.com/topic/195705-validate-form-help/#findComment-1028173
Share on other sites

What?

 

The message is only appended if the if statements have been ran. So only if one of those fields (or both) are empty, the message will be populated. That is the point of me encasing them in an if statement.

 

Did you test the code?

 

     if (!empty($_SESSION['message'])) {
             header("Location: register.php");
             exit();
     }

 

That portion of the code I posted sees if the message has been filled in (meaning that one of the fields were empty) if it has been filled in, then it will redirect. Else it continues on with the code.

Link to comment
https://forums.phpfreaks.com/topic/195705-validate-form-help/#findComment-1028197
Share on other sites

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.