1

Please I am trying to get this structure from a HTML form:

bulk_data: [
        {
            bank_code: "044",
            account_number: "1234567832",
            amount: 69000,
            currency: "NGN",
            meta: [
            {
              beneficiary_name: "Henry Augustus",
              beneficiary_address: "No 5 Corpus Christi Avenue",
              beneficiary_city: "Milano"

             },
            ],
        },
        {
            bank_code: "044",
            account_number: "1234567834",
            amount: 500,
            currency: "USD",
            narration: "Salary payment for April",
        },

    ]

I tried the form elements like thus: But when I dd out the request, I do not get the request parameters as above.

<section>
<input type="text" name="bulk_data[][bank_code]"/>
<input type="text" name="bulk_data[][account_number]"/>
<input type="text" name="bulk_data[][amount]"/>
<input type="text" name="bulk_data[][currency]"/>
<input type="text" name="bulk_data[][meta][][beneficiary_name]" />
<input type="text" name="bulk_data[][meta][][beneficiary_address]" />
<input type="text" name="bulk_data[][meta][][beneficiary_city]" />
</section>

<section>

<input type="text" name="bulk_data[][bank_code]"/>
<input type="text" name="bulk_data[][account_number]"/>
<input type="text" name="bulk_data[][amount]"/>
<input type="text" name="bulk_data[][currency]"/>
<input type="text" name="bulk_data[][narration]"/>

</section>

I want to be able to print out the request to appear in the above structure. And also, how do I access the arrays assuming that I do not know how many arrays are coming into the php server. Plus how do I bulk store the individual array records into mysql database?

Any help?

14
  • 1
    Why not create a simple form, with "normal" names, like name="bank_code", and then create the data structure in PHP, after proper input validation? That is a lot easier to do and maintain. Accessing objects/arrays is a basic language thing, you can easily look up. For storage you would use a query with INSERT. Perhaps it is better to ask a single question, per question? You will get a better answer for each issue. Commented May 29 at 13:45
  • @KIKOSoftware name="bank_code" wouldn't allow the form to contain multiple bank code fields, which is the OP's intention Commented May 29 at 14:09
  • 1
    I want to be able to print out the request to appear in the above structure.... for that you'll need json_encode(), once you've assembled the submitted data in a PHP array Commented May 29 at 14:10
  • how do I access the arrays assuming that I do not know how many arrays...you know what a loop is, I assume? Commented May 29 at 14:11
  • how do I bulk store the individual array records into mysql database...again, use a loop. Run an INSERT query for each record. Commented May 29 at 14:11

1 Answer 1

4

When processing POST data every time php finds [] in name it will create new item in array. If you want to avoid that and insert the data into same item in array you just need to index it yourself like this:

<section>
<input type="text" name="bulk_data[0][bank_code]"/>
<input type="text" name="bulk_data[0][account_number]"/>
<input type="text" name="bulk_data[0][amount]"/>
<input type="text" name="bulk_data[0][currency]"/>
<input type="text" name="bulk_data[0][meta][0][beneficiary_name]" />
<input type="text" name="bulk_data[0][meta][0][beneficiary_address]" />
<input type="text" name="bulk_data[0][meta][0][beneficiary_city]" />
</section>

<section>

<input type="text" name="bulk_data[1][bank_code]"/>
<input type="text" name="bulk_data[1][account_number]"/>
<input type="text" name="bulk_data[1][amount]"/>
<input type="text" name="bulk_data[1][currency]"/>
<input type="text" name="bulk_data[1][narration]"/>

</section>
Sign up to request clarification or add additional context in comments.

2 Comments

So the user has an opportunity to create more form elements as the user desires. So I kinda clone each section the user wants to duplicate. This your answer suggests that I would have to dynamically rename the name attribute for each form element. And the sections are more than 10. Is there no other way to achieve the desired outcome than writting a lot of javascript code to rename the name attribute for each cloned section?
@bugzy it shouldn't be all that much effort. You're not "renaming" the name attribute each time, you're simply using a different number in the square brackets. Make your JS code store the last number it used when a row was added, then it can simply increment that next time and inject it into the generated HTML. If you're struggling to understand how that could work, post a new question where you show the relevant JS and HTML code you've got currently for cloning a row, and then we can see precisely how you'd need to change it. But this post certainly meets your original need re the structure

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.