Jonathan Jonathan - 4 months ago 8x
jQuery Question

Cakephp button won't direct to view, "automagic" function to do this?

I have a view which I want to have 3 buttons; Save, Save & Next, Save & Previous. The save button is the standard submit type button and it works. The other two buttons show up as buttons, but won't do any action. I've written a file called save_move_next.js to call an action, and gave the button an id and put all the appropriate things where they should be, but it doesn't do a thing.

Is there some prebuilt way to do that? Basically, user clicks the button, action in controller takes over, saves information then moves to next item.

Would putting it above the form fix the issue? Or changing it so it looks like:

$this->Form->submit('Button text', array(some controller, action);
$this->Form->submit('Button text', array(some controller, action);
$this->Form->submit('Button text', array(some controller, action);

Part of view:

<?php echo $this->Form->end(__('Submit', true));?>
<?php echo $this->Form->button('Save and next drug', array('type'=>'button','id'=>'save_move_next')); ?>

JS function:


var drug_id = $(this).val();
if(drug_id.length > 0)
window.location = "<?php echo Router::url(array('controller' => 'french_translations', 'action' => 'next'), true) ?>/" + drug_id;


In Head of default.ctp:

echo $javascript->link('save_move_next');


I figured this out with the help of Saanch's answer. If cakephp 1.3 is being used, you can determine which button was clicked and simply redirect to the view desired. gvLearner pointed me in the right direction but had some issues with the code he provided.

Buttons in view:

echo $form->submit('Save', array('name'=>'save');
echo $form->submit('Save & Next', array('name'=>'saveNext');

Code to determine the button clicked and redirect:

if (isset($this->params['form']['name-you-gave-the-button-in-the-view']) && $this->params['form']['name-you-gave-the-button-in-the-view'] == 'name-you-gave-the-button-in-the-view') {
  // The button was clicked and you can redirect or whatever here.