CakePHP form submit by link click
onto the next question about cakePHP :)
In php, I am able to simulate a form submit by for example browsing to开发者_StackOverflow中文版 a url like
<a href="index.php?click=yes&ip=127.0.0.1">submit youre ip</a>
this would submit the form on index.php with the values of click being yes and ip being 127.0.0.1 without having to click a submit form.
How would I achieve the same thing in CakePHP?
Thanks in advance for any help with this!
You would need to setup an index action in a controller.
An example:
If you want to add an user with the above data, you can do the following:
class UsersController extends AppController {
function add($click, $ip) {
$this->User->set(array('click' => $click, 'ipaddress' => $ip);
$this->User->save();
}
}
Now if you go to http://localhost/users/add/yes/127.0.0.1 it should save the data...
You can use jQuery for this something like the following:
$('#my-link').click(function(){
$('#my-form').submit();
});
EDIT: This also seems relevant to your interests
In Cake 2.0 you should create a link this way:
<?php echo $this->Html->link('submit your ip', array(
'controller' => 'users',
'action' => 'index',//this is not necessary since index is the default action
'?' => array('click' => 'yes', 'ip' => '127.0.0.1'))
);?>
and this will create:
<a href="/users/?click=yes&ip=127.0.0.1">submit your ip</a>
Then you get the data in your UsersController through $this->request->query
For better understanding look this and this.
Hope this helps.
精彩评论