简体   繁体   中英

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

The form name is: bckup

In my Ajax code, I'm using var frm = $('form[name="bckup"]').serialize(); for the form.

The name of the input field is: dscrb

As you can see in my PHP code, I'm trying to get the value of dscrb but it does not work. The resulting file name is: 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

So this tells us that the data is being posted to the PHP page.

Why then does $_POST["dscrb"] not work?

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:

$("#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:

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

You will get a string like dcsbr=... . To convert this to an array use parse_str .

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

var_dump($form);

Working example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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