How to reload a form from a drop down - php

I would like to reload the form, or preferably just the values, by using 'onChange'. There are several elements in the form that needs to be updated when changing the project id.
$this->addElement('select', 'project_id', array(
'label' => t('Project'),
'multiOptions' => $data,
'filters' => array('Int'),
'value' => 0,
'required' => true,
'disableTranslator' => true,
'validators' => array(),
'onChange' => 'Javascript:window.location.reload(false)'
));

You can change your onChange to call a custom javascript function instead - which will change any fields you need to change, and then submmit the form.

Related

How do i get xeditable's content in js

I have below code of xeditable:
$this->widget('editable_ori.Editable', array(
'url' => $this->createUrl('index'),
'pk' => Yii::app()->user->id,
'name' => 'index',
'type' => 'text',
'text' => $data,
'placement' => 'top',
'showbuttons' => 'bottom',
'send' => 'auto',
));
I would like to get the content of this field in my js.
I have tried to grab content of this in js using below code. But its not working
$(".editable editable-click").text;
You are missing a . before the class name editable-click.
You have an extra space after the class name .editable.
The method is text() in jquery not text property to get the text.
Change
$(".editable editable-click").text;
to
$(".editable.editable-click").text()
EDIT
You should call the script after the editable has loaded you can use the event onInit to trigger your javascript function from another file which is dependent on the editable input. For instance, if you have a function
function myFunction(){
//do something
$(".editable.editable-click").text()
//do something more
}
then you should try calling that function from the onInit event like below
$this->widget('editable_ori.Editable', array(
'url' => $this->createUrl('index'),
'pk' => Yii::app()->user->id,
'name' => 'index',
'type' => 'text',
'text' => $data,
'placement' => 'top',
'showbuttons' => 'bottom',
'send' => 'auto',
'onInit' => 'js:function(){myFunction();}'
));
Try using onShown option of editable which will execute as soon as the editable form is shown. You can refer the code below.
$this->widget('editable_ori.Editable', array(
'url' => $this->createUrl('index'),
'pk' => Yii::app()->user->id,
'name' => 'index',
'type' => 'text',
'text' => $data,
'placement' => 'top',
'showbuttons' => 'bottom',
'send' => 'auto',
'onShown' => 'js: function() {
var txtVal=$(this).data("editable").value; // This will hold your textbox value as soon as it opens.
someFunction(txtVal); //Call your JS function with textbox value as parameter.
}'
));
Then in your JS function, you can do so:
<script>
function someFunction(txtVal)
{
//Do anything with editable text box value
}
</script>
Hope this helps you.

Magento: How to fill form data after its saved?

I am creating a custom form for my extension. In the form I have few fields that I save in database. My problem is that once I save the fields they get saved in DB but after the page refresh the data in the fields gets empty.
I want these fields data to remain in the fields. How do I do that? Is there a way to do that.
Form code is in this file app\code\community\CompanyNmae\ModuleName\Block\Adminhtml\Suretaxconfig\Edit\Form.php
and save config method is in this file
app\code\community\CompanyNmae\ModuleName\controllers\Adminhtml\ModuleController.php
Code for Form.php is here. There no HTML or PHTML used in the project for my extension.
<?php
class CompanyName_Modulename_Block_Adminhtml_ModuleConfig_Edit_Form extends Mage_Adminhtml_Block_Widget_Form{
protected function _prepareForm() {
$form = new Varien_Data_Form (
array('id'=>'edit_form', 'action'=>$this->getData('action'), 'method'=>'post')
);
$fieldset = $form->addFieldset('base_fieldset', array('legend'=>Mage::helper('modulename')->__('ModuleName Settings'),
'class'=>'fieldset-wide'));
$fieldset->addField('companycode', 'text', array(
'name' => 'companycode',
'label' => Mage::helper('modulename')->__('Client Number'),
'title' => Mage::helper('modulename')->__('Client Number'),
'required' => true,
'after_element_html' => '<small>Enter Client Number</small>'
));
$fieldset->addField('validationkey', 'text', array(
'name' => 'validationkey',
'label' => Mage::helper('modulename')->__('Validation Key'),
'title' => Mage::helper('modulename')->__('Validation Key'),
'required' => true,
'after_element_html' => '<small>Enter Company Validation Key </small>'
));
$fieldset->addField('select_provider_type', 'select', array(
'label' => Mage::helper('modulename')->__('Default Provider Type'),
'class' => 'required-entry',
'required' => true,
'name' => 'providertype',
'onclick' => "",
'onchange' => "",
'value' => '1',
'values' => array('70' => '70', '99' => '99'),
'disabled' => false,
'readonly' => false,
'after_element_html' => '<small>Default Provider Type</small>',
'tabindex' => 1
));
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
you can fetch data from database and fill that data into fields or you can use session and store form post values into it before saving the data into database and when it redirects on your form page you can check if values are exists then you fill them in form
Your question is little confusable, I'm assuming that you want to keep fields data field after the form submit....
Is this is the scenario then you can use PHP Sessions to store data in sessions and check on page load if session is set then display it.
Like:
PHP
session_start();
// you can set session on form submit using if(isset($_POST)){//set session here}
$_SESSION['example'] = "Example Value";
HTML
<input type="text" name="example" value="<?=(isset($_SESSION['example']) ? $_SESSION['example'] : ''); ?>"/>

How to add an action to the button in Controller of magento?

$fieldset->addField('test_connection', 'submit', array(
'label' => Mage::helper('rpos_connect')->__('Test Connection'),
'required' => true,
'value' => 'Test',
'after_element_html' => '',
'tabindex' => 1
));
This is my PHP code to prepare a form with a button in my module block folder.Now I want this to call a function named testAction() in my Controller class.
You can use onclick as follows.
'onclick' => "setLocation('{$this->getUrl('*/controller/action')}')"

Yii Multiple Scenario based client validations

I am creating a form using Yii's activeForm.
All aspects of the form are working except that the client side validation does not take care of scenarios.
I would like it if I can get the client validations working based on scenarios.
Below mentioned is the partial code for the form
$form = $this->beginWidget('CActiveForm', array(
'enableClientValidation' => true,
'enableAjaxValidation' => false,
'errorMessageCssClass' => 'has-error',
'htmlOptions' => array(
'class' => 'form-horizontal',
'role' => 'form',
'id' => 'payment-form',
),
'clientOptions' => array(
'id' => 'payment-form',
'validateOnSubmit' => true,
'errorCssClass' => 'has-error',
'successCssClass' => 'has-success',
'inputContainer' => '.form-group',
'validateOnChange' => true,
),
));
FYI the scenario validation is done when the data is pushed to the model, where we have defined the scenarios for the validation. So if you want to validate on client side, the only way is to use "JAVASCRIPT", Or through the ajax, which you have turned off enable the ajax validation 'enableAjaxValidation' => true, to get the client validation using ajax.

How to get current selection value from cake php FormHelper

I need to modify some code in cakePHP and the last thing that is holding me still is my inability to get current value from input form and send it to my jquery script.
The forms look like this:
<?php echo $this->Form->input('country', array('options' => $countriesOption, 'onchange' => 'getHotels(this.value, $(\'showSingleInput\').checked);', 'style' => 'margin: 10px;', 'div' => false, 'label' => false, 'error' => 'false', 'hiddenField' => false, 'id' => 'countryInput')); ?>
<?php
if($regionOption != null){
echo $this->Form->input('region', array('options' => $regionOption, 'onchange' => 'getHotelsInRegion(this.value,this.value, $(\'showSingleInput\').checked);', 'style' => 'margin: 10px;', 'div' => false, 'label' => false, 'error' => 'false', 'hiddenField' => false, 'id' => 'regionInput'));
}?>
The problems is at second input form 'region'.
I need to pass as first value to JQuery method currently selected value from 'country' form input.
And all the hard stuff I need to do is here:
'getHotelsInRegion(this.value,this.value, $(\'showSingleInput\').checked);'
Something like
$('#ModelNameFieldName').change(function() {
alert($(this).val());
});
should to the trick.

Categories