I am seeking help to flesh out a tutorial for a pretty common action, which is adding a button to a form which results in an ajax-update, meaning the input value results in new output without refreshing the screen.
protected $txtNewCode;
protected $btnAddCode;
I am going to have a text field in my template defined as $txtNewCode and the button that will submit that new code is $btnAddCode.
// Add a new Code
$this->txtNewCode = new QTextbox($this); //textfield
$this->btnAddCode = new QButton($this); //button
$this->btnAddCode->Text = QApplication::Translate('Add Code');
$this->btnAddCode->CssClass = 'button';
$this->btnAddCode->AddAction(new QClickEvent(), new QAjaxAction('btnAddCode_Click'));
This is all that is needed to actually needed to create the controls in the template.
We define btnAddCode_Click with the following:
protected function btnAddCode_Click($strFormId, $strControlId, $strParameter) {
$objUser = unserialize($_SESSION['User']); // grab user from session
$groupCode = GroupCode::LoadBySignupCode($this->txtNewCode->Text);
if (!$groupCode) {
// If not found, create new group code and save it in DB
$groupCode = new GroupCode();
$groupCode->SignupCode = $this->txtNewCode->Text;
$groupCode->Save();
}
$objUser->AssociateGroupCode($groupCode); //associate User with the entered code
$this->objUser->Save();
}
I use the existing LoadBySignupCode for the model GroupCode by passing the value I just entered in the txtAddCode field.
If there's nothing there, I create a new one with the value and save it.
Then I associate it using AssociateGroupCode to the User.
Please provide additions and modifications for this before I go to step 2, which is to update the panel with the new value.
To notify the user that it has been a successful action, I added after the $this->objUser->Save() the following:
$this->lblMessage->Text ="Successfully entered code:" . $this->txtNewCode->Text;
$this->lblMessage->Display = True;
I define $this->lblMessage earlier in the QForm with:
// Notification Label
$this->lblMessage = new QLabel($this);
$this->lblMessage->Display = false;
$this->lblMessage->CssClass = "message";
This hides the message, but allows me to take care of all styling with the css class.
In the template, I use the following in the appropriate location and markup:
<?php $this->lblMessage->Render()?>
<?php $this->txtNewCode->RenderWithError(); ?>
<?php $this->btnAddCode->Render(); ?>
Each respectively with create the HTML for the message, the form field for the new code, and the button.
Questions for the tutorial:
1) Have I covered all possible edge-cases for a code that is entered?
2) How do I output in the template all the associated codes with that user?