homepage Welcome to WebmasterWorld Guest from 23.21.9.44
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Page loading for the first time
This is almost working
Adam5000




msg:4210834
 10:39 pm on Oct 3, 2010 (gmt 0)

I'm trying to validate a registration form and I'm having trouble with blank fields. The form I'm using has several fields and if the user leaves one of them blank I'd like to echo a message that says "At least one of the fields is blank." or "All fields are required." or something like that.

The code posted below works but the problem is, when the page loads for the first time, the "At least one of the fields is blank." message is displayed before the user enters any data.

And this is correct because before the user enters any data, all the fields are blank.

Is there a way to prevent the "At least one of the fields is blank." message from displaying on the screen when the page loads for the first time?

Help!


<html>
<head>
<title>Validation form workspace</title>
</head>

<body>

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

Username: <input type="text" name="e_mail">
<br>
Username: <input type="text" name="u_name">
<br>
<br>
<input type="submit" value="Submit">

</form>


<?php

if ($_POST["e_mail"] == "" || $_POST["u_name"] == "")

echo "At least one field is blank.";

else
{
echo "Both fields have value.";
}

?>

</body>
</head>

 

StoutFiles




msg:4210840
 11:31 pm on Oct 3, 2010 (gmt 0)

<html>
<head>
<title>Validation form workspace</title>
</head>

<body>

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

Username: <input type="text" name="e_mail">
<br>
Username: <input type="text" name="u_name">
<br>
<br>
<input type="hidden" name="check" value="1">
<input type="submit" value="Submit">

</form>


<?php

if ($_POST["check"] != "1")
{}//do nothing
else
{

if ($_POST["e_mail"] == "" || $_POST["u_name"] == "")

echo "At least one field is blank.";

else
{
echo "Both fields have value.";
}

}
?>

</body>
</head>

SevenCubed




msg:4210841
 11:48 pm on Oct 3, 2010 (gmt 0)

Maybe you may want to consider validating client side instead of server side, which is typically more efficient. If yes consider validating it with JavaScript with something like this...

<html>
<body>
<form action="test.php" method="post" name="login">
Email: <input type="text" name="e_mail">
<br>
Username: <input type="text" name="u_name">
<br>
<br>
<input onclick="javascript:validate()" type="button" value="Submit" />
</form>

<script type="text/javascript">
function validate()
{
var conforms=true
with (document.login){
if (e_mail.value=="" && conforms){
conforms=false
alert("Please Enter Your Email Address")
e_mail.select()
}
if (u_name.value=="" && conforms){
conforms = false
alert ("Please Enter Your Username")
u_name.select()
}
if (conforms==true){
submit();
}
}
}
</script>
</body>
</html>

And, for the above, there is probably a more efficient way of writing it too but that's the whip it together in 5 minute version. I would also add a hidden field and/or some challenges to it to prevent auto-submissions. And of course there is nothing in that to parse the data entered. Also note that I added a name for the form for it to work, it's in bold.

Adam5000




msg:4210858
 12:43 am on Oct 4, 2010 (gmt 0)

Stout! You're magnificent! That fixed the problem and that part of the form works good now. You're fabulous!

Seven. That's good code and I got some advice that said if I'm inserting the user input into a database it's best to validate with PHP. Javascript is a good language and you can do a lot of cool things with it and I think PHP is a better choice for this particular application.

SevenCubed




msg:4210859
 12:50 am on Oct 4, 2010 (gmt 0)

...it's best to validate with PHP...


I agree, I validate with PHP too when the form contents are passed to my handler. My example was simply to ensure that contents needed by the PHP handler are there in the first place and typically it's better to avoid the request to the server when it can be handled by the client itself first.

Anyway, what's important is that you have the solution you need. Cheers.

Matthew1980




msg:4210940
 7:42 am on Oct 4, 2010 (gmt 0)

Hi all,

Maybe you may want to consider validating client side instead of server side, which is typically more efficient. If yes consider validating it with JavaScript with something like this...

Well, be that as it may, but consider this: To run java script, the users browser needs to be able to use JS, and as a lot of people now turn it off amid security fears (there has been a security exploit recently with a new worm that around) either that or they have the "prompt me before executing" option enabled.

A lot of these checks can quite easily be done with JS, but personally I prefer to use the php engine to handle all validation, it's not too difficult to check for submission's (from form or the command line, that's if someone wants to circumvent the form of course) whether the fields are correctly filled in, and there are some pretty nifty things you can do with regex that can sift through data sent to see if it matches the criteria that you want to stop from being submitted.

And above all, as it's server side there is no compatibility issue, this is only my opinion, the solution's posted will work, I just thought I would stand up for php!

Oh, just one point to make here too:-

if (($_POST['e_mail'] == "") || ($_POST['u_name'] == ""))

If you have condition one and are checking against condition two and you want to check either is evaluating to true, which in this case you are, to express this in a more readable manner, parenthesize the evaluating components so that it is easier to read, so that you are basically saying if( (this equals true) || (this equals true) ) then you can see much clearer what's going on. Also have a read of this [php.net] to see the differences between || and or.

Happy coding!

Cheers,
MRb

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved