Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

php - serializeArray with indexed input names

I have a form what contains a repeater field set with 4 inputs. These inputs (text + number fields) names is structured as follows: "stay[1][type-room]" with the number being changes as people add more field sets.

The end goal is to pass all the forms inputed data through Ajax to PHP.

Have been looking at using serializeArray to gather and process the data. Here is my JQuery script i have at the moment.

JQuery

var stay_data = {};
$.each($('#check-availability').serializeArray(), function(i, field) {
 stay_data[field.name] = field.value;
});

PHP Output

Array
(
    [check-in-date] => January 7, 2021
    [check-out-date] => January 9, 2021
    [stay[0] => Array
        (
            [num-adults] => -1
            [num-children] => 
            [num-infants] => 
            [type-room] => ANY
        )

    [stay[1] => Array
        (
            [num-adults] => 1
            [num-children] => 
            [num-infants] => 
            [type-room] => TW
        )

    [property_opt_code] => BELACSAF002??????
    [night] => 2
)

This output is close to what I need but I need the "stay" key values to be a array and not having it display individually.

so in the end having all the repeater fields under a single key called "stay" then bing able to get the end result as a associative array in PHP


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...