简体   繁体   English

Ajax 序列化表格 - PHP 无法获取单个输入数据

[英]Ajax serialize form - PHP can't get individual input data

I have a JQuery UI dialog that is used for making a backup of a file.我有一个用于备份文件的 JQuery UI 对话框。 It contains an input box so users can add a short description that will become part for the name of the backup file.它包含一个输入框,因此用户可以添加一个简短的描述,该描述将成为备份文件名称的一部分。 So if the user enters "blue", the backup file name will be: file_ blue _2020-08-08-11:10:23 .所以如果用户输入“blue”,则备份文件名为: file_blue_2020-08-08-11 :10:23

The form name is: bckup表单名称为: bckup

In my Ajax code, I'm using var frm = $('form[name="bckup"]').serialize();在我的 Ajax 代码中,我使用var frm = $('form[name="bckup"]').serialize(); for the form.为表格。

The name of the input field is: dscrb输入字段的名称是: dscrb

As you can see in my PHP code, I'm trying to get the value of dscrb but it does not work.正如您在我的 PHP 代码中看到的那样,我正在尝试获取dscrb的值,但它不起作用。 The resulting file name is: file_2020-08-08-11:10:23 .生成的文件名是: file_2020-08-08-11:10:23 However, if I change the PHP code to use $_POST["frm"] instead of $_POST["dscrb"] , the resulting file name is: file_ dscrb=blue _2020-08-08-11:10:23但是,如果我将 PHP 代码更改为使用$_POST["frm"]而不是$_POST["dscrb"] ,则生成的文件名为: file_ dscrb=blue _2020-08-08-11:10:23

So this tells us that the data is being posted to the PHP page.所以这告诉我们数据正在发布到 PHP 页面。

Why then does $_POST["dscrb"] not work?那么为什么$_POST["dscrb"]不起作用?

HTML: HTML:

<div id="dialog-backup" style="display: none;" title="Blah?">
    <p><span class="ui-icon ui-icon-alert" style="float: left; margin: 0px 12px 22px 0px;"></span>Blaha.</p>
    <form name="bckup">
    <p style="margin-left: 28px;"><label for="dscrb">Description: </label><input id="dscrb" type="text" style="z-index:10000" name="dscrb"> (optional)</p>
    </form>
    <p style="margin-left: 28px;">Blah?</p>
</div>

JS: JS:

$("#backup").click(function() {
    $( "#dialog-backup" ).dialog({
            stack: true,
            resizable: false,
            height: "auto",
            width: 400,
            modal: true,
            buttons: {                  
            "Save": function() {
                //$( this ).dialog( "close" );
                $("#saveConf").trigger("click");
            },
            "Create a backup": function() {
                    $( this ).dialog( "close" );    
                var frm = $('form[name="bckup"]').serialize();
                $.ajax({
                        url : "ajax_functions.php",
                        type: "post",
                        data: { action: "backup", frm: frm },
                        //dataType: "text",
                        dataType: 'json',
                        success : function (response) {
                            var response = response[0]
                                if (response && response.toLowerCase().indexOf("success") >= 0) {
                                    $("#dialg-success-msg").text(response);
                                $("#dialg-success").dialog("open");
                            } else {
                                $("#dialg-failed-msg").text(response);
                                $("#dialg-failed").dialog("open");
                            }                               
                    },
                    error : function(response) {
                                    $("#dialg-failed-msg").text(response);
                            $("#dialg-failed").dialog("open");
                                }
                });
                //return false;
                //////////////////
            },
            Cancel: function() {
                    $( this ).dialog( "close" );
                }
        }
    });
});

PHP: PHP:

$dscrpn = isset($_POST["dscrb"]) ? trim(stripslashes($_POST["dscrb"]))."_" : '';    
$backup_file = "backups/File_".$dscrpn.date('Y-m-d-H:i:s');

Since you are sending your post-data as frm: frm you will have to use $_POST['frm'] in PHP.由于您将发布数据作为frm: frm发送,因此您必须在 PHP 中使用$_POST['frm']

You will get a string like dcsbr=... .你会得到一个像dcsbr=...这样的字符串。 To convert this to an array use parse_str .要将其转换为数组,请使用parse_str

$form = [];
parse_str($_POST['frm'], $form);

var_dump($form);

Working example .工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM