1

I have two CHtml::submitButton() in my form, namely "Accept" and "Reject". Each of the buttons has a specific ID ... When any of the two are pressed, i would like to trigger an action in my controller. Below are the code snippets.

=========================================================

View

<?php
echo CHtml::beginForm('mycontrollerName/AcceptUserRegistration','get');
?>

<?php echo CHtml::submitButton('Accept', array('id' => 'accept')); ?>
<? echo '&nbsp;&nbsp;&nbsp;'; ?>
<?php echo CHtml::submitButton('Reject', array('id' => 'reject')); ?> 
<?php echo CHtml::endForm(); ?>

========================================================

Controller

public function actionAcceptUserRegistration() {

$value = $_GET['id'];

if($value == "accept"){
//will do something here
}

if($value == "reject"){
//will do something here.
}

}

======================================================================

When i implement it this way. the get value in the controller side is empty. What I'm I doing wrong? Or is there any other way around this?

2
  • Please post your HTML code instead of the PHP code that generates it. Apr 20, 2012 at 7:15
  • Why don't you try $_REQUEST in place of $_GET?
    – Dhruvisha
    Apr 20, 2012 at 7:17

2 Answers 2

4

You forgot to give the submit button a name (unless your framework does some magic which we cannot know about - you should really post the generated HTML code!).

If you do so, the value in $_GET['buttonname'] will be its value (not its id), i.e. Accept or Reject. This becomes pretty messy as soon as you get into i18n, so you might want to use different names for the buttons and then check isset($_GET['buttonname1']) and isset($_GET['buttonname2'])

4
  • Thanks ThiefMaster for your quick response.
    – kevintich
    Apr 20, 2012 at 7:15
  • so should I do something like this <?php echo CHtml::submitButton('Accept', array('id' => 'accept', 'name' => accept)); ?>
    – kevintich
    Apr 20, 2012 at 7:16
  • Ok... and in the $_GET[], should it be $_GET['Accept'] or $_GET['accept']..?
    – kevintich
    Apr 20, 2012 at 7:28
  • 1
    print_r($_GET); would be a good thing for you to do... The key in that array is the field name, so it's lowercase accept/reject. Apr 20, 2012 at 7:57
3

Here is a code example for what you are trying to achive:

VIEW:

<?php
echo CHtml::beginForm('mycontrollerName/AcceptUserRegistration','get');
?>

<?php echo CHtml::submitButton('Accept', array('id' => 'accept', 'name' => 'accept')); ?>
<? echo '&nbsp;&nbsp;&nbsp;'; ?>
<?php echo CHtml::submitButton('Reject', array('id' => 'reject', 'name' => 'reject')); ?> 
<?php echo CHtml::endForm(); ?>

Note: I added the name parameter. As name is not displayed to the user, you can use accept instead of Accept.

CONTROLLER:

public function actionAcceptUserRegistration() {

if(isset($_GET['accept'])) {
     //accepted
} else if(isset($_GET['reject'])) {
     //rejected
}
//I recommend using POST method instead of GET for posting the form.
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.