...

View Full Version : No empty fields



SuperMode55
12-19-2011, 07:05 PM
Hello
I am building a registration page . If the user completes all the required field i load the info into the database but if there are missing fields I combined some javascript into php to display next to the empty field a note that it should be filled. This is my code :


<?php
if (isset($_POST['submit']))
{
$username =$_POST['username'];
$password =$_POST['password'];
$password2 =$_POST['password2'];
$firstname =$_POST['firstname'];
$lastname =$_POST['lastname'];
$email =$_POST['email'];
$address =$_POST['address'];
$city =$_POST['city'];
$state =$_POST['state'];
$zip =$_POST['zip'];
$dob =$_POST['dob'];

if (empty($username)){
?> <script language="javascript">
var user =true
</script> ;<?
}
if (empty($password)){
?> <script language="javascript">
var pass =true
</script><? ;
}

And it goes on...................
Then next to the inpput box I display a text in case the user didn't fill in the info like this and by the way the above code is in my validate.php page wich i include in the main registration page



<?php include ('includes/validate.php');?>
<form action="registration.php" method= "post" >
<fieldset>
<legend>sitename Registration</legend>
<p>
<label for"username">Username</label>
<input type "text" name=username />&nbsp;&nbsp;&nbsp;&nbsp; <script type="text/javascript">
if (user == true)
{
document.write("Please enter a Username of your choice");
}
</script>
</p>

<p>
<label for"password">Password</label>
<input type "password" name=password />&nbsp;&nbsp;&nbsp;&nbsp; <script type="text/javascript">
if (pass == true)
{
document.write("Please choose a Password");
}
</script>
</p>

<p>
<label for"password2">Re-type Password</label>
<input type "pass2" name=password2 />&nbsp;&nbsp;&nbsp;&nbsp; <script type="text/javascript">
if (pass2 == true)
{
document.write("Please re-type your Password");
}
</script>
</p>

And it goes on......for the rest of the input fields.

I need some help with a solution for when the user fills in the info and pushes the "register" button the page refreshes and it whipes all the info the user already input but it does display the warnings where the user didn't input any data, so what would be the best way to go to keep the data that the user entered.Thank you.

jmrker
12-20-2011, 12:23 AM
Add to your <form> tag...


<form action="registration.php" method= "post" onsubmit="return validation()">


The validation() function should include the checks you need for empty or invalid fields.
It should return false if there are errors and true if all is OK.

Philip M
12-20-2011, 08:02 AM
Form validation of the pattern if (user == true) is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

JavaScript form validation only provides convenience for users, not security. If for example a script verifies that the user agreed to a firm's terms of service, or filters invalid characters out of fields that should only contain numbers, the validation must also be performed server-side, and not just on the client.

document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes
loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write
the original content of your page. It cannot be used to update the content of your page after that page has loaded.


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

jmrker
12-20-2011, 03:23 PM
...
document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes
loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write
the original content of your page. It cannot be used to update the content of your page after that page has loaded.

...

Had not analyzed OP's code that far. Nice catch.
That also would probably result in wiping out user input in addition to <form> tag fix.
:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum