Tony
Tony

Reputation: 825

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 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!

Upvotes: 0

Views: 3944

Answers (3)

Esteban Cacavelos
Esteban Cacavelos

Reputation: 753

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.

Upvotes: 0

8vius
8vius

Reputation: 5836

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

Upvotes: 1

JanWillem
JanWillem

Reputation: 352

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...

Upvotes: 1

Related Questions