CodeIgniter Forums

Full Version: how to pass array from view to controller
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
how to pass array from view to controller...please help yaa. Huh
You don't pass data from a view to a Controller. You pass data from a Controller to a View.

You do pass input data from a form to a controller.
Do you mean: how to submit a form containing an array, or how to make an ajax request and posting an array, or something else...?
...and then the result will be read by a controller?
(12-31-2015, 07:10 AM)includebeer Wrote: [ -> ]Do you mean: how to submit a form containing an array, or how to make an ajax request and posting an array, or something else...?
...and then the result will be read by a controller?


ya i mean that.
how to pass array on form submition ?
You need to use multiple input with the same name and [] to tell PHP it's an array. The values will be in $_POST["mytext"].

You can find a simple tutorial here : http://www.sanwebe.com/2013/04/capture-a...-using-php

PHP Code:
<form method="post" action="collect_vals.php">
<
div class="input_fields_wrap">
 
   <button class="add_field_button">Add More Fields</button>
 
   <div><input type="text" name="mytext[]"></div>
 
   <div><input type="text" name="mytext[]"></div>
 
   <div><input type="text" name="mytext[]"></div>
 
   <div><input type="text" name="mytext[]"></div>
 
   <div><input type="text" name="mytext[]"></div>
</
div>
</
form
To pass data to view in codeigniter, especially for array, you can do this


PHP Code:
$data = array(
   'title' => 'Title',
   'heading' => 'Heading',
   'message' => ' Message'
);

$this->load->view('cwblogview'$data); 


For multi dimensional array you will have to create loops.

Source: https://www.cloudways.com/blog/how-to-pa...deigniter/
(12-31-2015, 03:06 AM)Martin7483 Wrote: [ -> ]You don't pass data from a view to a Controller. You pass data from a Controller to a View.

You do pass input data from a form to a controller.


Using Ajax I need to pass an array of data to the controller
Although typically setting the traditional property helps with any time you are passing arrays via AJAX calls, you can still run into issues with serialization. You often need to explicitly set the name of the parameter that you are expecting to populate as seen below :

data: { 'ids': ids },

*SEO link redacted*