Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: No empty fields

  1. #1
    New Coder
    Join Date
    Nov 2011
    Location
    Notre Dame, IN
    Posts
    16
    Thanks
    10
    Thanked 0 Times in 0 Posts

    No empty fields

    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 :
    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

    Code:
    <?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.
    Last edited by SuperMode55; 12-19-2011 at 07:10 PM.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,077
    Thanks
    38
    Thanked 498 Times in 492 Posts
    Add to your <form> tag...
    Code:
    <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.

  • Users who have thanked jmrker for this post:

    SuperMode55 (12-20-2011)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.
    Last edited by Philip M; 12-20-2011 at 08:06 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,077
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Exclamation

    Quote Originally Posted by Philip M View Post
    ...
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •