0

I am writing a code from which values stored into multi-dimensional array but the problem is I am unable to do it when I submit the form it makes a single array like this.

Array (
    [0] => inputbox value
    [1] => 1
)

But I want a result like this

Array (
    [0] => Array (
        'nameField' => 'inputbox value',
        'PageField' => 1
    ),
    [1] => Array ( ... ),
    [2] => Array ( ... ) ... and so on
)

I am unable to figure out what I am doing wrong here my simple code

<?php
    if ($_POST) : 
        echo "<pre>";
            print_r($_POST['mc']);
        echo "</pre>";
    endif;
?>

Here is my HTML

<form action="#" method="post">
<table style="width:100%">
    <thead>
        <tr>
        <td style="width:70%"><strong>Display Title Name:</strong></td>
        <td style="width:30%"><strong>Select Sub Page:</strong></td>
        </tr>
    </thead>
    <tbody>
    <tr>
    <td style="width:70%"><input name="mc[]nameField" value="" style="width:100%" type="text"></td>
    <td style="width:30%">
        <select name="mc[]PageField" style="width:100%">
            <option selected="selected" disabled="disabled" value="">Select page</option>
            <option value="1">04:00 PM Result Page</option>
            <option value="2">08:00 PM Result Page</option>
            <option value="3">11 55 AM Result Page</option>
            <option value="4">About Us</option>
            <option value="5">Contact Us</option>
        </select>
    </td>
    </tr>
    </tbody>
</table>
<div style="width:100%; padding:5px;">
    <input class="add_new_media button button-primary button-large" id="publish" value="Add" type="button">
    <input class="remove button button-primary button-large" id="publish" value="Remove" type="button">
</div>
<input name="SubmitForm" type="submit" value="SubmitForm" />
</form>

Here is my Jquery Code

<script>
jQuery(document).ready(function($){
var count = $("tbody tr").length;
myFunction(count);
    $('.add_new_media').on('click', function() {
        $('tbody tr:first-child')
        .clone()
        .appendTo($('tbody'))
        .find('input')
        .val('')
        var count = $("tbody tr").length;
        myFunction(count);
    });
    $('.remove').on('click', function() {
        $('tbody tr:last').remove();
        var count = $("tbody tr").length;
        myFunction(count);
    });
    function myFunction(count) {
        if (count > 1) {
            $('.remove').removeAttr("disabled");    
        } else {
            $('.remove').attr("disabled", "disabled");
        }
    }
});
</script>
11
  • what do you mean? from your html, looks like it should just do two post values which are $_POST['mc[]namefield'] and $_POST['mc[]PageField]`. Commented Jan 21, 2018 at 16:20
  • @HuyVo I have a clone form function which makes these two fields into multiple fields, using jquery thats why I needed to go through the arrays Commented Jan 21, 2018 at 16:22
  • should post your jQuery code. Commented Jan 21, 2018 at 16:23
  • @HuyVo Plz check the jquery Commented Jan 21, 2018 at 16:25
  • Too long, didn't read. Why are you cloning? Commented Jan 21, 2018 at 16:41

1 Answer 1

2

You want to send an array of dictionaries to the server.

First, create an array that will contain dictionaries say var arrays = [] as your global variable.

var arrays = [];

The general idea is every time when a user has finish inputting data, you need to add that data to arrays.

Add a button to let jQuery know when to add to the arrays:

<button id="abutton" type="button" >Click Me!</button>

Here is how you add the data to the arrays in jQuery:

 $("#abutton").click(function(){

       var nameFieldData = // get data from name field 
       var pageFieldData = // get data from page field

       arrays.push(
          {'nameField': nameFieldData,
           'PageField': pageFieldData});
 });

Then send arrays back to the server when you are ready.

Sign up to request clarification or add additional context in comments.

5 Comments

as i am already getting data from the code the only problem is its not properly covering into array with column names
jQuery is mainly a UI library. Looks like what you are is mixing data with UI. My way was to try to split UI and data.
No its actaully works fine the only problem I am getting this its splitting all the values then create an array of out of them, I have no issue to change the filed name into nameField[] PageField[] but i want to combine them into multi dimensional array on php end
did my post really help you?
yes i little change my strategy using your features and it solved my data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.