【问题标题】:Passing serialized data from jquery mobile to PHP using $.post使用 $.post 将序列化数据从 jquery mobile 传递到 PHP
【发布时间】:2016-04-05 19:08:53
【问题描述】:

我正在使用 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";
?>

给我这个错误:

未定义索引:用户名

【问题讨论】:

    标签: javascript php jquery jquery-mobile


    【解决方案1】:

    用这样的方式序列化并发送您的数据:

    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,
        })
    
    })
    

    然后像这样在 PHP 中接收它:

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

    【讨论】:

    • cordova 是否支持 $.ajax,因为此应用程序稍后将在 cordova 上构建?
    • 可以,但您需要先将域列入白名单。在您的配置中:&lt;access origin="http://domain" /&gt;
    • $('#form') 这是表单ID吗?
    • 是的,它是表单 ID。
    猜你喜欢
    • 1970-01-01
    • 2011-04-29
    • 2018-01-06
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 2018-07-25
    • 2017-12-16
    • 1970-01-01
    相关资源
    最近更新 更多