I am trying to connect a button in the view to the db. I know I have to use java script/ Ajax or jQuery and the onclick(). But what should I do next? How do I connect the outcome of button click to a php variable which I can then save to the DB?
Update: (ANS)
What I was missing was the fact that controller can be accessed from view. Just do this:
<?php echo form_open('form/submit'); ?>
<br><br>
Title<br>
<input type="text" name="title"><br>
Body<br>
<input type="text" name="body">
<input type="submit" value="New">
This will submit the form back to the controller’s submit function and you can do all you want with it. Thanks for the responses!
Create a view that handles ajax requests properly (probably just has JSON output)
and then make a request to it via AJAX from the view with the button.
Inside your ajax.php you can setup the appropriate code to access the db.
You’re jquery / javascript ajax request is simply going to be in the View, it doesn’t have it’s own place in the MVC structure of codeigniter, from CI’s perspective it’s just two views.