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

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

本文关键字:数据 PHP 序列化 post jquery mobile 使用      更新时间:2024-01-06

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

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

检查通过$('form#login_form').serialize()的数据后

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

我得到:

username=ihfufh&passwordinput=dfygfyf

PHP脚本:

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

给我这个错误:

未定义的索引:用户名

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

jQuery/AAJAX

$('#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,
    })
})

然后在PHP中接收它,如下所示:

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