Geoff
Geoff

Reputation: 6639

Passing serialized data from jquery mobile to PHP using $.post

I am using jQuery mobile to pass form data to a PHP script but I can't access the data in PHP. I have tried this:

$.post('http://127.0.0.1/tum_old/testi.php', $('form#login_form').serialize(), function(data) {
    console.log(data);
});

After checking on the data being passed through $('form#login_form').serialize() by

var param = $('form#login_form').serialize();
console.log(param);

I get:

username=ihfufh&passwordinput=dfygfyf

The PHP script:

<?php
    $username = $_POST['username'];
    echo "$username";
?>

Gives me this error:

Undefined index: username

Upvotes: 1

Views: 109

Answers (1)

user5563910
user5563910

Reputation:

Serialise and send your data with something like this:

jQuery / AJAX

$('#form').on('submit', function(e){
    e.preventDefault();

    $.ajax({
        // give your form the method POST
        type: $(this).attr('method'),
        // give your action attribute the value yourphpfile.php
        url: $(this).attr('action'),
        data: $(this).serialize(),
        dataType: 'json',
        cache: false,
    })

})

Then receive it in PHP like this:

<?php
   // assign your post value
   $inputvalues = $_POST;
   $username = $inputvalues['username'];
?>

Upvotes: 2

Related Questions