MojtabaSh
MojtabaSh

Reputation: 637

How can store password on browser when html form submiting with $.post

I created a form for SignIn. Since I don't want to refresh the page when the form is submitted, I used the $.post method to send data to a database and receive that.

and I have a problem that browser don't save password if they want save that because form don't be submitted in usual way, I thought maybe cookie is good way but i think it's not safe.

$.post example for login

function formsubmited(form, p, username) {
    $.post("process_login.php", {username: username.value, p: p.value},
            function(response) {
                if(response == 'login')
                {
                      // do something
                }
            }
});

HTML form Sample

<form name="form_login">
  <input name="username"/>
  <input type="password" name="password"/>
  <input type="button" value="login" onclick="formsubmited(this.form, this.form.password, this.form.username);" />
</form>

how can store password on browser when form is submitting in this way? is there any way? or i should use cookies?

Upvotes: 1

Views: 4017

Answers (3)

Notaras
Notaras

Reputation: 717

This is an elaboration of my comment so you can understand.

What you are doing is making a post request to a php script like so:

$.post("process_login.php", {username: username.value, p: p.value},

If you want to do it using javascript, i would personally change it to do this:

function formsubmited() {
    var username = document.getelementsbyname("username").value;
    var p = document.getelementsbyname("password").value;
    $.post("process_login.php", {username: username, p: p},
        function(response) {
            if(response == 'login')
            {
                  // do something
            }
        }
}

And then edit your html so it looks a bit cleaner like this:

<input type="button" value="login" onclick="formsubmited();" />

Now in your PHP script, you need to store the POST data in memory so it can be used at later stages. One of the best ways to do this is by using the $_SESSION[] array.

So your PHP script will look like this

<?php
   session_start();
   //On the server side, you can access data you have sent there in the $_POST[] array. 
   //The key you specify in the array has the exact name as the parameter you pass in 
   //javascript:
   $inputtedUsername = $_POST["username"];
   $inputtedPassword = $_POST["p"];
   //first do a check to determine if the username and password are correct
   //add your code here:
   //....

Once you have added your code to determine if the username and password are correct, you can then proceed to storing your username and password in memory:

    $_SESSION["username"] = $inputtedUsername;
    $_SESSION["p"] = $inputtedPassword;

Now in your javascript code, you are expecting the script to return login keyword to determine that the login process was successful

  //basically you'll need to do something like this
  if($yourloginboolean == true){
      echo "login";
  }else{
     echo "error";
  }
  ?>

Now in future, should you wish to use $_SESSION["username"] or $_SESSION["p"] in any future PHP code, simply just use it as you would any other variable.

Hope this helps

Upvotes: 1

nathan
nathan

Reputation: 149

Use PHP to set a session, this will store the password on the server and can be called each time it is required, e.g. something like..

session_start();
$_SESSION['p']=$_POST['p'];

Upvotes: 0

sanjeev
sanjeev

Reputation: 4631

The case of the problem is that you are using input type="button" and the browser save password don't get triggered on the above input type. Use input type="submit"

Just do one change in this line

<input type="button" value="login" onclick="formsubmited(this.form, this.form.password, this.form.username);" />

Don't use button , use submit

So make it as below :

<input type="submit" value="login" onclick="return formsubmited(this.form, this.form.password, this.form.username);" />

and add return false at the end of the function , so that form don't get sumbitted.

Upvotes: 1

Related Questions