More than one submit button

I was wondering how one might go about having two submit buttons, and how to control them within the controller.

For example. Let’s say we have a blog PostController, and when creating a post we want to click “Submit” to publish the blog post directly, or click the “Save as Draft” button to do just that.

How can we know in the controller which button was pressed to submit the page rather than perhaps scanning the $_POST variable?

Thanks :slight_smile:

You can check if a button is submitted by calling $form->submitted(‘saveAsDraft’)===true if the array key of your button is ‘saveAsDraft’.

CHtml::button() + JavaScript

The $form->submitted($buttonId) would work great if my Post base model was a CFormModel. My model inherits from CActiveRecord.

I suppose if I want the $form->submitted() functionality, I would have to restructure my form/model. In the meantime though, if I want to determine which button was pressed in the controller, I may just have to check the $_POST data (due to my model’s inherited class). If I am off base, someone let me know.

I think I could use Jerry’s fix by throwing the name of the submit button into the $_POST[‘Post’] on an onClick or onSubmit command and add a variable in my model. Then just ask the model what button was pressed.

Thanks for your help!

In that case you could check if isset($_POST[‘Post’][‘saveAsDraft’])===true, assuming that the Html name is ‘saveAsDraft’ and your model is ‘Post’. I wouldn’t rely on any solution that only works with JavaScript.