Forum Moderators: coopster

Message Too Old, No Replies

Form validation

This almost works

         

Adam5000

2:08 pm on Sep 30, 2010 (gmt 0)

10+ Year Member



I've got something that almost works. It does work when data is entered into the fields. The problem is it also works when the page is loaded.

When the page is loaded, both fields are empty and that means they match. So the echo statement "Passwords match" is displayed on the screen before the user enters any data.


Is there a way to say don't apply the if else statement until the submit button is clicked.

Below is the code I'm using.

Help!

<html>
<head>
<title>No frills regestration form</title>
</head>

<body>

<form action="server_side_validation.php" method="post">

Password:<input type="text" name="psword">
<br>
Reenter Password<input type="text" name="reenter_psword">
<br>
<input type="submit" value="Submit">

</form>

<?php

if ($_POST["psword"] != $_POST["reenter_psword"] )

echo "Passwords don't match.";

else
{
echo "Passwords match";
}

?>

</body>
</head>

Anyango

2:42 pm on Sep 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php

if ($_POST["psword"]=="" || ($_POST["psword"] != $_POST["reenter_psword"]))

echo "Passwords don't match.";

else
{
echo "Passwords match";
}

?>

Adam5000

4:57 pm on Sep 30, 2010 (gmt 0)

10+ Year Member



That's good code Anyango and it works but it doesn't fix the problem.

The problem is the when the page loads for the first time, the if / else statement is automatically applied and the message "Passwords match" is displayed before the user chooses and enters a password into the fields.

It's correct because before any data is entered into the fields the fields are blank and that means they match. Empty = Empty.

Is there a way to prevent the if / else statement from automatically being applied when the page loads for the first time.

What I'm trying to prevent is the situation where the message "Passwords match" is displayed on the screen before any passwords are entered.

Help!

Anyango

5:33 pm on Sep 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?
if(isset($_POST["psword"]) && $_POST["psword"]!="")
{
if ($_POST["psword"] != $_POST["reenter_psword"])
{
echo "Passwords don't match.";
}
else
{
echo "Passwords match";
}
}

?>

Adam5000

10:11 pm on Sep 30, 2010 (gmt 0)

10+ Year Member



Success! Anyango: Exceptional job! This part of the form does what I want it to do now. Terrific job on you part!

Anyango

4:34 am on Oct 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cheers :)