Retrieve data from multiple select box in Slim framework
Hello friends, welcome to findnerd. Today I am going to tell you how to fetch data in array from multiple select box in Slim framework. Follow the steps below:
Step 1: Lets begin by marking your select box field as follows:
<form action="/admin/fetchdata">
<select name="city[]" class="form-control" id="city" multiple="multiple">
<option value="">Select City</option>
{% for value in cities %}
{% if value.city_name !='' %}
<option value="{{ value.city_name }}">{{ value.city_name }}</option>
{% endif %}
{% endfor %}
</select>
</form>
Adding the attribute 'multiple' to the select box, it will allow the user to select more than 1 options. User will need to hold ctrl or shift command in order to select multiple options. You can set the 'size' attribute of the select box to show larger size , so that more options are visible to the user. So after adding the above html code, following html page will be displayed as shown:
***Note: For selecting multiple options at a time from select box, you need to add square brackets after the name.
Step 2: Now you can add the following code to the controller in order to fetch the data:
$app->post('/admin/fetchdata', function () use ($app) {
echo "<pre>";
print_r($app->request->params('city'));
echo "</pre>";
}
Select the following options as shown below:
print_r($app->request->params('city')):
This will show you the following output:
Array
(
[0] => Bangalore
[1] => Dehradun
[2] => Delhi
)
All done!
Thanks for reading the blog.
0 Comment(s)