1

I'm trying out CodeIgniter (I'm a novice coder) and am having some trouble with POST data from a form.

I have the following code to generate a form:

<div>
	<?php echo form_open('todos/update_completed/'); ?>
	<?php foreach ($todos as $todo): ?>
		<?php echo form_checkbox('completed', $todo->id, $todo->completed); ?>
		<?php echo $todo->task; ?>
		<?php echo "<br />"; ?>
	<?php endforeach ?>
	<br />
	<?php echo form_submit('MySubmit', 'Update ToDos'); ?>
	<?php echo form_close(); ?>
</div>

This generates the following code:

<form action="http://localhost:8091/index.php/todos/update_completed" method="post" accept-charset="utf-8">


  <input name="completed" value="1" type="checkbox">Go to the shops
  <br>

  <input name="completed" value="2" checked="checked" type="checkbox">Pick up camera
  <br>

  <input name="completed" value="5" checked="checked" type="checkbox">Call Joey
  <br>

  <input name="completed" value="6" checked="checked" type="checkbox">Fill in tax return
  <br>

  <br>

  <input name="MySubmit" value="Update ToDos" type="submit">

</form>

When I try to retrieve the POST data using:

$completed_todos = array(); 
$completed_todos[] = $this->input->post_get('completed');

... I allways get an array ($completed_todos) which only contains 1 (one) element - regardless how many checkboxes I checked, and it is always the latest checkbox I checked!

print_r($completed_todos); only returns the following: Array ( [0] => 6 )

Can someone please explain why I am not getting all the checkbox values returned in my array?

ps: I am following a tutorial from https://selftaughtcoders.com/creating-processing-form-codeigniter/

3 Answers 3

3

If there are multiple CheckBoxes with same name you should write as below

Your Check Boxes

<input name="completed" value="1" type="checkbox">

Have to change the name like this name="completed[]"

<input name="completed[]" value="1" type="checkbox">
<input name="completed[]" value="2" checked="checked" type="checkbox">
<input name="completed[]" value="5" checked="checked" type="checkbox">
<input name="completed[]" value="6" checked="checked" type="checkbox">
Sign up to request clarification or add additional context in comments.

3 Comments

I have seen this as a possible solution, but when using <?php echo form_checkbox('completed', $todo->id, $todo->completed); ?> as instructed by the CodeIgniter framework, the brackets aren't added. Would this imply that there is a bug in the CodeIgniter code?
You can use plain HTML tags instead of using codeigniter form.
I have posted this question to the CodeIgniter forum to ask whether the form_checkbox('completed', $todo->id, $todo->completed); can be used for array values as well.
0

You need to pass the checkboxes that is selected right? So make small modifications in your view

<input name="completed[]" value="1" type="checkbox">Go to the shops
  <br>
  <input name="completed[]" value="2" checked="checked" type="checkbox">Pick up camera
  <br>
  <input name="completed[]" value="5" checked="checked" type="checkbox">Call Joey
  <br>
  <input name="completed[]" value="6" checked="checked" type="checkbox">Fill in tax return
  <br>
  <br>
  <input name="MySubmit" value="Update ToDos" type="submit">

Comments

-1

In your controller access the checked values using following code

if(isset($_POST['MySubmit']))
{
    $checkbox_value=$_POST['completed'];
    var_dump($checkbox_value);
}

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.