1

My ajax is this:

$.ajax({
    type: "POST",
    url: 'index.php',
    datatype: 'html',
    data: { password : password}
});

Have been trying to store it in $pword. But it doesn't store. PHP:

if(isset($_POST['password']))
{
    // store session data
    $pword=$_POST['password'];
}

HTML is this:

<input type="password" placeholder="password" id="password" name="password"/>

Please help.

ADreNaLiNe-DJ
  • 4,787
  • 3
  • 26
  • 35
Vineet Basantani
  • 182
  • 1
  • 1
  • 10

6 Answers6

2

first of all : there is nothing wrong with server side (PHP) and HTML code.

so for jQuery portion : you need to correct a line like i did below:

$.ajax({
            type: "POST",
            url: 'index.php',
            datatype: 'html',
            data: { 
              password : $("#password").val()
              //here i asked jquery to fetch entire value of my html input(with an id of "password") as text(Not an Object) so it gives me the entered text value.
                  }})
                .success(function(data) {                        alert(data.toString());
        });//optional function:runs when server responses to your request

i can leave more detailed help if you explain what exactly you r going to do.

Siamak
  • 1,689
  • 1
  • 17
  • 26
  • I am simply transferring password entered by user to check from the database if any entry matches there. – Vineet Basantani May 24 '16 at 09:38
  • to transform any thing via ajax you need a server(to run PHP code) and a client(which gets and sends users password ) . how you implemented server part? – Siamak May 24 '16 at 11:03
  • i checked out my snippet and it works. if you put whole file here it'll be easier to solve the problem. – Siamak May 24 '16 at 11:22
1
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#password').on('blur',function(){
    var password = $('#password').val();

     $.ajax({
            type: "POST",
            url: 'index.php',
            datatype: 'html',
            data: { password : password
           },
            success: function(data){
            alert(data);
        }

        });


})
})
</script>

<form method="post">
    <input type="password" placeholder="password" id="password" name="password"/>
</form>
Samyappa
  • 535
  • 2
  • 11
0

You can use javascript setTimeout function to pass the form value into php page as mentioned below,

I have added a function called passData() inside the setTimeout function, so it calls the passData() function mentioned delay.

HTML:

<input type="password" placeholder="password" id="password" name="password"/>

Javascript:

setTimeout(function() { passData() }, 3000);

function passData(){
   var password = $("#password").val(); 
    $.ajax({
        type: "POST",
        url: 'index.php',
        datatype: 'html',
        data: { password : password}
    }); 
}
Krish R
  • 22,583
  • 7
  • 50
  • 59
  • why not a simple button to send the data? – Mihai Matei May 24 '16 at 07:08
  • @Vineet Basantani, can you please check your error console, do you get any errors, check jquery library added into your page. – Krish R May 24 '16 at 07:09
  • What does the use of `setTimeout` accomplish here? The actual passing of data in your example is accomplished using a function that you don't even mention in your answer. –  May 24 '16 at 07:10
  • 1
    @duskwuff because OP is expected like "without submitting form or refreshing the page?" – Krish R May 24 '16 at 07:11
  • @Vineet Basantani, I have updated my answer, please check again. – Krish R May 24 '16 at 07:14
0

use form.serialize() to send data.

$.ajax({
            type: "POST",
            url: 'index.php',
            datatype: 'html',
            data: form.serialize()
});
0

From jquery documentation the $.ajax request should look like this:

$.ajax({
  method: "POST",
  url: "some.php",
  dataType: 'json',
  data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
  alert( "Data Saved: " + msg );
});

Another suggestion would be to create a button. When pressing it the data will be sent to the PHP file.

<input type="password" placeholder="password" id="password" name="password"/>
<input type="button" id="submit" name="submit" value="submit" />

The jQuery code would be:

$(document).ready(function(){
    $('#submit').click(function(){
        $.ajax({
          method: "POST",
          url: "my-php-file.php",
          dataType: 'json',
          data: { password: $('#password').val() }
        })
        .done(function( msg ) {
          alert( "Data sent: " + msg );
        });
    });
});
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
  • Depends on what do you mean by `store session data`. If you want to store it in a session variable to be used later in other php files then you should `start_session()` at the top of the file and store the password in the session variable: `$_SESSION['password'] = $_POST['password']`. Be aware that doing it like this, without encrypting the password, is very dangerous, so take the encryption in consideration. – Mihai Matei May 24 '16 at 07:20
  • I have started the session. Don't worry about the security, I don't have launch codes in there. I just want it to work. – Vineet Basantani May 24 '16 at 07:30
  • But even `$_SESSION['password'] = $_POST['password']` is not getting the value – Vineet Basantani May 24 '16 at 07:31
  • Try it with the additional parameter `dataType: 'json'`. See my updated answer – Mihai Matei May 24 '16 at 07:36
0

HTML

<input type="password" placeholder="password" id="password" name="password"/>
<input type="button" onclick="sendData()" value="Ok">

Javascript

function sendData(){
   
    $.ajax({
        type: "POST",
        url: 'index.php',
        datatype: 'html',
        data: { password : $("#password").val()}
    }); 
}
dutta
  • 71
  • 1
  • 2