Welcome Guest, Not a member yet? Register   Sign In
How to pass two variables form view to controller in mvc
#1

[eluser]rash[/eluser]
Hi all,

Can anyone tell me how can pass two variables from view part of a form to controller..
Look my view form is like ..

<table width="100%" border="0" cellpadding="2" cellspacing="1" class="display" id="example">
<thead>
<tr>
<th>Student Name</th>
<th>Book Name</th>
<th>Issue Date</th>
<th>Expiry Date</th>
<th>Time</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
&lt;?php
$slno=0;
foreach($this->issuelist as $key => $value) {
$slno++;
?&gt;
<tr>
<td>&lt;?php echo $value['student_name']; ?&gt;</td>
<td>&lt;?php echo $value['book_name']; ?&gt;</td>
<td>&lt;?php echo CovertDate($value['issue_dt']); ?&gt;</td>
<td>&lt;?php echo CovertDate($value['expiry_dt']); ?&gt;</td>
<td>&lt;?php echo $value['btime'];?&gt;</td>
<td><a href="&lt;?php echo URL;?&gt;library/edit_book_issue/&lt;?php echo $value['book_issue_id'];?&gt;">Edit</a></td>
<td><a href="&lt;?php echo URL;?&gt;library/delete_book_issue/&lt;?php echo $value['book_issue_id'];?&gt;">Delete</a></td>
</tr>
&lt;?php } ?&gt;
</tbody>
</table>

In the line where i wrote edit with a href i want to post another variable to cotroller, but i don't know how??
plz help me.
#2

[eluser]davidMC1982[/eluser]
Firstly, you don't have an html form, so I'm not sure what you're expecting to "post". Generally, to post data to a controller you'd do something like this in your view:

Code:
&lt;form name="input" acti echo base_url('news/add');?&gt;" method="post"&gt;
    &lt;input type="text" name="title"&gt;&lt;br>
    &lt;input type="text" name="body"&gt;&lt;br>
    &lt;input type="submit" value="Add News"&gt;
&lt;/form&gt;

And this in your controller:

Code:
class News extends CI_Controller {

    public function __construct(){
        parent::__construct();
    }

    public function add() {

        if ($this->input->post()) {
            $title = $this->input->post('title');
            $body = $this->input->post('body');

            //Do whatever else you want like saving to a db.
    }
}

If you're looking to pass say, an id, to delete a record or somesuch, you'd do something like this in your view:

Code:
<a href="&lt;?php echo base_url('news/delete/' . $id);?&gt;">Delete</a>

And something like this in your controller:

Code:
function delete($id = null) {

    //Do something here with the $id like check if the record exists and delete it
}




Theme © iAndrew 2016 - Forum software by © MyBB