使用$ .post将序列化数据从jquery mobile传递到PHP

时间:2022-09-25 21:32:29

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:

我使用jQuery mobile将表单数据传递给PHP脚本但我无法访问PHP中的数据。我试过这个:

$.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

检查通过$('form#login_form')传递的数据后,序列化()

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

I get:

username=ihfufh&passwordinput=dfygfyf

The PHP script:

PHP脚本:

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

Gives me this error:

给我这个错误:

Undefined index: username

未定义的索引:用户名

1 个解决方案

#1


2  

Serialise and send your data with something like this:

序列化并使用以下内容发送数据:

jQuery / AJAX

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中接收它,如下所示:

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

#1


2  

Serialise and send your data with something like this:

序列化并使用以下内容发送数据:

jQuery / AJAX

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中接收它,如下所示:

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