Nitish Bangad
Nitish Bangad

Reputation: 51

Javascript form validation in PHP

I have developed a login page in PHP. I am using PHP code to validate that username and password are not empty. Javascript alert box is popped out on error and page is navigating back to login page. Now suppose if user enters username and does not enter password and clicks on login, password is empty box is displayed and now user I want user to navigate back to login page with username populated with previous value entered by him. I am beginner to PHP, and not getting idea on how to do this. This is my code

if($myusername == null){
echo '<script language="javascript">';
echo 'alert("Username cannot be empty")';
echo '</script>'; 
echo "<meta http-equiv='refresh' content='1; URL=main_login.php'>";
exit();
}

if($mypassword == null){
echo '<script language="javascript">';
echo 'alert("Password cannot be empty")';
echo '</script>'; 
echo "<meta http-equiv='refresh' content='1; URL=main_login.php'>";
exit();
}

Upvotes: 0

Views: 240

Answers (6)

Zeeshan
Zeeshan

Reputation: 619

It's Simple. You can do it by two ways

  1. By query string. Put you username in query string and access that query string in value attribute of username text box. In this approach redirect user to login page. after alertbox is displayed because you cant use header('Location:..') function because header is already sent you need to use javascript to redirect. Put these line of alert('...') method window.location.href = "Your signup URL?uname=Username which user entered"; Then on sigin page use following in value attribute <input tyep="text" name="textbox name" id = "textbox id" value = "<?php if(isset($_GET['uname'])) echo $_GET['uname']; else echo '';?>">

  2. You can store value of username inside session and excess that session in value attribute of username textbox. In this case after validation use following code if form is not validated

    $_SESSION['uname'] = $_POST['name of your username field'];

then on signin page use following <input tyep="text" name="textbox name" id = "textbox id" value = "<?php if(isset($_SESSION['uname'])) echo $_SESSION['uname']; else echo '';?>">

In second approach dont forgot to call session_start(); at the very beginning of you code.

Upvotes: 0

Anand G
Anand G

Reputation: 3200

Javascript inside PHP is not recommended, there must be other way to do that but as I dont know reason why you put this. Try this out

    if($myusername == null){
       echo '<script language="javascript">';
       echo 'alert("Username cannot be empty")';
       echo 'window.location = "/main_login.php?un=myusername";'
       echo '</script>';
       exit();
    }

    if($mypassword == null){
       echo '<script language="javascript">';
       echo 'alert("Password cannot be empty")';
                  echo 'window.location = "/main_login.php?un=myusername";'
       echo '</script>'; 

       exit();
      }

inside main_login.php put this

    <input type="text" name="username" value="<?php echo $_GET['usrname'];?>"/>

Upvotes: 0

Manwal
Manwal

Reputation: 23816

I believe you may go for Custom Script or use Jquery Validaor Plugin. I have created a Sample Jquery Validator Example.

Jquery Validator Plugin With Bootstrap

$(document).ready(function () {

    $('#contact-form').validate({
        rules: {
            name: {
                minlength: 2,
                required: true
            },
            email: {
                required: true,
                email: true
            },
            message: {
                minlength: 2,
                required: true
            }
        },
        highlight: function (element) {
            $(element).closest('.control-group').removeClass('success').addClass('error');
        },
        success: function (element) {
            element.text('OK!').addClass('valid')
                .closest('.control-group').removeClass('error').addClass('success');
        }
    });

});

Note: You can also use this plugin without bootstrap More Details

Demo

Upvotes: 0

Bandeshor Makai
Bandeshor Makai

Reputation: 111

There are lots of ways to achieve this You could store the username in session , it may not be good idea The other way is you could pass the username in url while redirecting to the login page , redirect as to login.php?us=usernameEntered and set the value of the username as

<input type="text" name="username" value="<?php echo $_GET['usernameEntered'];?>"/>

OR you could use form validation in jquery and avoid all these redirection

Upvotes: 0

volter9
volter9

Reputation: 735

If you using HTML5 you can use form validation as described here:

http://www.sitepoint.com/html5-form-validation/

Else you can use some jQuery and plugins for it to validate the form.

Upvotes: 1

Sandeeproop
Sandeeproop

Reputation: 1776

If are using jQuery. Then you can use jQuery plugin a http://jqueryvalidation.org/files/demo/

Upvotes: 0

Related Questions