Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Determine which submit button has been clicked

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 425
    Comment on it

    If we have two submit buttons and want to detect which one is pressed then simply use post method i.e. $this->request->data.

    <?php echo $this->Form->create(false); ?>
    <?php echo $this->Form->text('input'); ?>
    <?php echo $this->Form->submit('Yes', array('name' => 'submit1')); ?>
    <?php echo $this->Form->submit('No', array('name' => 'submit2')); ?>
    <?php echo $this->Form->end(); ?>
    

    When form is submit then print the response and see the array.

    when the "Yes" button is clicked:

    array(
        'submit1' => 'Yes',
        'input' => 'test'
    )
    

    when the "No" button is clicked:

    array(
        'submit2' => 'No',
        'input' => 'test'
    )
    

    To check which button was clicked:

    if (isset($this->request->data['submit1'])) {
        // yes button was clicked
    } else if (isset($this->request->data['submit2'])) {
        // no button was clicked
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: