0

post the value of selected checkbox from php form to view in codeigniter framework

php form

<input type='checkbox' value="<?php echo $row_id ?>">
<input type='hidden' name='asap[]' value="1 " >
<input type='hidden' name='asap[]' value="2 " >
<input type='hidden' name='asap[]' value="3 " >

controller

$asap=> $this->input->post('asap')

view

echo $asap;

the value of checkbox does not show in view

6
  • What's the question? $asap would be an array(I think) Commented Apr 15, 2019 at 3:04
  • ohh i forgot about that .. view does not echo the post i input Commented Apr 15, 2019 at 3:08
  • echo $asap[0]; Maybe? Commented Apr 15, 2019 at 3:09
  • Undefined index: asap but i already define it as $this->load-view('view'); Commented Apr 15, 2019 at 3:14
  • still can't get it here is my full controller public function view() { $this->layout->buffer ( array( 'asap' =>$this->input->post('asap') )); $this->load->view('view'); } Commented Apr 15, 2019 at 3:30

2 Answers 2

1

view:

<input type='checkbox' name='cname' value="<?php echo 'cvalue'; ?>">//changes
<input type='hidden' name='asap[]' value="1" >
<!--removed right space from value-->
<input type='hidden' name='asap[]' value="2" >
<!--removed right space from value-->
<input type='hidden' name='asap[]' value="3" >
<!--removed right space from value-->

controller:

//here you have syntax error use `=` instead `=>`
$postData = $this->input->post();
//you get all your post data, if you added `name` attribute only
print_r($postData);
//it will return you first hidden value
print_r($postData['asap'][0]);
echo '------------';
echo 'loop values';
$asap = $this->input->post('asap');
foreach($asap as $row){
       echo $row.'<pre>';
}

output:

Array
(
    [cname] => cvalue
    [asap] => Array
        (
            [0] => 1 
            [1] => 2 
            [2] => 3 
        )

)
1
----------
loop values
1
2
3
Sign up to request clarification or add additional context in comments.

6 Comments

He is using CodeIgniter :)
@Eddie, Am i missing something?
I'm not familiar with CI at all, but I'm pretty sure it has a proper way of getting post data and not the $data = $_POST
ok updated code, i assumed he, read all things from user guide, Thanks @Eddie
thanks sir i can get the value now but how can i echo them by row ? <td><?php echo $asap> or foreach ($asap as sap) {the echo 1 by 1 or array by row
|
0

you want to get the value of checkbox so you need to put in a array.

In controller

public function update() {
   $completed_asap = $this->input->post('asap');
   // No need to echo $completed_asap. If you want then please do below line.
   // var_dump($completed_asap);
   foreach ($completed_asap as $asap_id) {
      echo 'The asap with id = ' . $asap_id. ' is marked as completed.<br>';
   }
}

In your view

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Simple CodeIgniter</title>
    <link rel="stylesheet"
    href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
    <div class="container">
        <h1>Todos</h1>
        <?php echo form_open('controller_name/update'); ?>
            <div class="list-group">
                <?php foreach ($todos as $todo) { ?>
                    <div class="list-group-item clearfix">
                        <?php echo form_checkbox('asap[]', $asap->id, $asap->completed); ?>
                        <?php echo $asap->task; ?>
                    </div>
                <?php } ?>
            </div>
            <button type="submit">Submit</button>
        <?php echo form_close();
    </div>
</body>
</html>

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.