简体   繁体   中英

Post An Array input to PHP

I am trying to post an array input to PHP through Javascript but when I print the posted array it does not show anything in the controller I am attaching my code below

submitHandler: function(form) {
    $('input[name="val_profession_id[]"]').val();
}

any help is appreciable.

This is my array select box which i need to post through ajax to controller

<select class="select_box form-control" multiple="multiple" name="val_profession_id[]" id="val_profession_id"> 
</select> 

and ajax code is :

val_profession_id = $('input[name="val_profession_id[]"]').val();

I am not aware of what happens when array will pass through HTML to the controller function, But at least I can tell you how to pass data from HTML to the controller function

HTML code needs to be something like this:-

<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    </head>
    <body>
        <form>
            <select class="select_box form-control" multiple="multiple" name="val_profession_id[]" id="val_profession_id">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
            </select>
            <input type="button" value="Submit" />
        </form>
        <script>
            $('input[type=button]').click(function(){
                $.ajax({
                    url: "1.php", // pass controller function Url here
                    type:'POST',
                    data: $('form').serializeArray(),
                    success:function(res){
                        console.log(res);// response come from server
                    }
                });
            });

        </script>
    </body>
</html>

And on PHP side:-

<?php

echo "<pre/>";print_r($_POST['val_profession_id']); 
// print value and then do further code what you want

You don't need to amend the values being passed, if you have the following form in a table for example;

<form action="myaction.php" method="POST">
    <table>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
        </tr>
        <tr>
            <td><input name="firstname[]" /></td>
            <td><input name="lastname[]" /></td>
        </tr>
        <tr>
            <td><input name="firstname[]" /></td>
            <td><input name="lastname[]" /></td>
        </tr>
        <tr>
            <td><input name="firstname[]" /></td>
            <td><input name="lastname[]" /></td>
        </tr>
    </table>
</form>

These have the [] in the input names, when the form is submitted, you can do the following;

<?php

for ($i = 0; $i < count($_POST['firstname']); $i++)
{
    $firstname = $_POST['firstname'][$i];
    $lastname = $_POST['lastname'][$i];
    // Do something with this
}

You can use this to loop the data and get the info into a "usable format" that you can access easily

I'll try to explain with some examples, please let me know if it's not enough for you.

<!-- file.html -->
<form>
    <select id="val_profession_id" multiple name="val_profession_id[]">
        <option selected value="2">2</option>
        <option selected value="3">3</option>
    </select>
</form>

Above your form as it is on your HTML file. I use some random values, but it's not important here.

<!-- file.js -->
//Get an array, all the selected values
var val = $('#val_profession_id').val();
$.ajax({
    method: "POST",
    url:    "http://example.com/file.pp",
    data: {
        val_profession_id: val
    }
}).then(function(res) {
    //You got the server's response
    console.log(res);
});

Above the script that send the Ajax request. You can execute it on a button click of whenever you want. It only read the selected values, and then it posts them to file.php

<?php
//file.php
var_dump($_POST);

The above should show something like:

array(1) {
    ["val_profession_id"]=>
    array(2) {
    [0]=>
    string(1) "2"
    [1]=>
    string(1) "3"
  }
}

As you can see, PHP gets it as an array.

See jQuery.ajax

When you set a name of an input-field to something[] it will be automatically changed to an array when you receive it in PHP through $_POST['something'] . Read here for more information.

A little example:

<form id="your_form">
    <input name="something[]" value=val_1>
    <input name="something[]" value=val_2>
</form>

in PHP then you get:

print_r ($_POST['something']);

will give you:

//Array ([0] => 'val_1', [1] => 'val_2')

you can simply submit the form via javascript:

document.getElementById('your_form').submit ();

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