2

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();
5
  • Please post your ajax code here Commented May 30, 2018 at 10:41
  • Could you provide a reproducible example? Your JavaScript code that sends the request, and the PHP that receive it. Commented May 30, 2018 at 10:41
  • 1
    if you will not provide the proper code, how could you expect a proper solution, you should provide all the possible code for debug. Commented May 30, 2018 at 10:47
  • Possible duplicate of Pass array to ajax request in $.ajax() Commented May 30, 2018 at 11:01
  • To pass data you'll want to make an ajax request, if you'd like to pass an array then please see the following post: Pass array to ajax request in $.ajax() Commented May 30, 2018 at 11:14

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

5 Comments

OP's asking about jQuery way and you are telling him PHP way. You are far away from the right path.
OP doesn't need jQuery, it's un-needed overhead
OP stated clearly:- This is my array select box which i need to post through ajax to controller
OP wants post its datas using JavaScript, I think you should add some JavaScript code to send an Ajax request.
AJAX is not in the question, perhaps OP needs to edit, then we can better help
0

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 ();

Comments

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.