0

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!

2
  • Sorry formatting cut a bit of my question description off. Commented Oct 14, 2011 at 19:02
  • never use a GET to do a POST's job. Commented Oct 17, 2011 at 2:48

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, thanks, now i see it like that, it was rather simple :)
1

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

6 Comments

Hi 8vius, Sorry because the formatting wasnt correct it actually cut a bit of my question off, I have updated this. I have also gone through and accepted answers from previous questions. Usually forget to do this after I have the answer as i get back to coding :) Thanks for the advice though
You want a purely PHP solution?
Well have you tried the solution I expressed already? Do you use jQuery in your project?
yes, your missing my question, I already use a solution like that, but what do i actually set #my-form to be in a cakephp url? eg when writing my own code #my-form would be index.php?click=yes&ip=127.0.0.1 what would it be for a cakephp valid url to perform the same action?
How are you creating your forms? When you create a form in CakePHP it already points by default to an action in your controller. By submitting it you are doing a POST to the action it points to.
|
0

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.

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.