Cakephp form has no data on submit - php

Using cakephp 1.3 I fill out a form, and upon debugging the submit, I notice that
data:$("#submit-478065271").closest("form").serialize()
is empty. Why would something like that happen? I've checked that the form actually has data, which I can serialize manually. Here is the submit code:
echo $this->Js->submit(
'Sign up',
array(
'class' => 'btn btn_submit fr register_submit register_btn_align',
'url' => array('controller' => 'email_guides', 'action' => 'subscribe'),
'before' => '$(".error-message").remove();' . $this->Js->get('#loading')->effect(
'fadeIn',
array('buffer' => false)
),
'complete' => $this->Js->get('#loading')->effect(
'fadeOut',
array('buffer' => false)
) . 'debugger;',
'success' => 'if(data.success) {
$("#CustomUserFirstName").val("");
$("#CustomUserEmail").val("");
$("#EmailGuidesUserStartDate").val("' . date('d/m/Y', strtotime('+1 Weekday')) . '");
$("#EmailGuidesUserTerms").attr("checked", false);
$("#signupModal").hide();
} else {
$("#signupModal").hide();
}
'type' => 'json'
)
);
?>
<?php echo $this->Form->end(); ?>
UPDATE: I've noticed that the form is not a parent of the submit element ... which is very bizarre. This would explain why .closest("form") returns an empty array.

Related

session based multistep form cakephp

Basically i have 3 forms.
I have one action in my form and i want create 3 steps form using session.
I want when i click the next button it validates the form1,if successful then redirects to form2..
like wise for form3 but keep all the data in session and in third step the action goes runs and add all the data to database.
I only need to check conditions from session before my insert goes run in my action addjob.
Plz help me to do this.
My main question is how to get all the post data in one session variable and check step by step before insert..
i am learner in cakephp and i have not enough idea about session.
if you are a expert then plz do this.
public function addjob($id = NULL) {
$this->layout = "layout_registration_old";
$this->loadcategory();
$this->loadcargo();
//$this->loadsubcategory();
$this->getCountries();
$this->getstates();
if ($this->request->is('put') || $this->request->is('post')) {
if (isset($id)) {
$this->Job->id = $id;
} else {
$this->request->data['Job']['status'] = 1;
$this->request->data['Job']['job_type'] = 1; //this is used to update the job type private or public.
$this->Job->create();
}
$this->Job->set($this->request->data);
if ($this->Job->AddEdit()) { // ADDEdit is the validation name in model
if ($this->Job->save($this->request->data['Job'], false)) {
if (isset($id)) {
$this->Session->setFlash(__('Job has been updated sucessfully.'));
} else {
$this->Session->setFlash(__('Job has been added succesfully.'));
}
$this->redirect(array('controller' => 'jobs', 'action' => 'index'));
}
} else {
$errors = $this->Job->validationErrors;
$this->Session->setFlash(__('Please check your entry.'), 'flash_error');
}
}
if (isset($id)) {
$this->request->data = $this->Job->find('first', array('conditions' => array('id' => base64_decode($id))));
}
}
form - 1
<?php echo $this->Form->create('Job', array('url' => array('controller' => 'jobs', 'action' => 'addjob')));?>
<?php echo $this->Form->input('customer_name',array('div' => false, 'label' => false));?>
<?php echo $this->Form->input('customer_no',array('div' => false, 'label' => false));?>
<?php echo $this->Form->input('customer_email',array('div' => false, 'label' => false));?>
<?php
echo $this->Form->input('transport_type', array(
'type' => 'select',
'label' => false,
'class' => 'select',
'options' => array(
1 => 'Road'
),
));
?>
<?php echo $this->Form->submit('Save', array('class' => "navigation_button btn btn-primary btn-sm", 'div' => false)); ?>
<?php echo $this->Form->end(); ?>
form - 2
<?php echo $this->Form->create('Job', array('url' => array('controller' => 'jobs', 'action' => 'addjob')));?>
<?php echo $this->Form->input('fname',array('div' => false, 'label' => false));?>
<?php echo $this->Form->input('lname',array('div' => false, 'label' => false));?>
<?php echo $this->Form->input('email',array('div' => false, 'label' => false));?>
<?php echo $this->Form->submit('Save', array('class' => "navigation_button btn btn-primary btn-sm", 'div' => false)); ?>
<?php echo $this->Form->end(); ?>
form - 3
<?php echo $this->Form->create('Job', array('url' => array('controller' => 'jobs', 'action' => 'addjob')));?>
<?php echo $this->Form->input('shop',array('div' => false, 'label' => false));?>
<?php echo $this->Form->input('business',array('div' => false, 'label' => false));?>
<?php echo $this->Form->input('status',array('div' => false, 'label' => false));?>
<?php echo $this->Form->submit('Save', array('class' => "navigation_button btn btn-primary btn-sm", 'div' => false)); ?>
<?php echo $this->Form->end(); ?>
Instead of saving the data from first 2 forms in a session, you should implement a multi part form using any javascript solution (Form wizard). Validate the data of each step using javascript or via ajax (if needs to be done dynamically) before moving to the next step and submit form at the end so that you don't have to play with sessions.
Example link
PS - This could go as a comment to the question but I haven't gained enough reputations to post comments yet :(

Cannot update textfield based on dropdown list Yii

I have 3 dependent dropdowns, and a textfield being dependent to the last dropdown. If one of the value in the last dropdown, I want the textfield value dynamically changes based on the selected value (it's from the same Database table).
This is the view of the third dropdown:
<div class="row" id="id_subkeg">
<?php echo $form->labelEx($model, 'id_subkeg'); ?>
<?php
echo $form->dropDownList($model, 'id_subkeg', array(), array(
'style' => 'width: 100%',
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('dynamicSatuan'),
'update' => '#' . CHtml::activeId($model, 'satuan'),
//'update'=>'#seksi',
'data' => array('id_subkeg' => 'js:this.value'),
)
)
);
?>
<?php echo $form->error($model, 'id_subkeg'); ?>
</div>
This is the textfield view:
<div class="row" id="satuan">
<?php echo $form->labelEx($model, 'satuan'); ?>
<p class="hint" style="font-size: 80%">Contoh: Dokumen.</p>
<?php echo $form->textField($model, 'satuan', array('style' => 'width: 98%;', 'readonly' => FALSE)); ?>
<?php echo $form->error($model, 'satuan'); ?>
</div>
And this is the action in the controller:
public function actionDynamicSatuan() {
//$data = Subkegiatan::model()->findByPk($_POST['id_subkeg']);
$data = Subkegiatan::model()->findByPk('id_subkeg=:id_subkeg', array(':id_subkeg' => (int) $_POST['id_subkeg']));
echo $data->satuan;
}
But it's not been working for days. I don't know which part I've missed. My guess is that the dropdown is a dependent dropdown to the one above it. So I must have missed at some part.
Any help is highly appreciated.
UPDATE:
After days of searching, finally got it:
public function actionDynamicSatuan() {
$param_country = (int) $_POST['id_subkeg'];
$maxOrderNumber = Yii::app()->db->createCommand()
->select('satuan')
->from('subkegiatan')
->where('id_subkeg = ' . $param_country)
->queryScalar();
echo '<b>SATUAN: '. $maxOrderNumber.'</b>';
//echo CHtml::tag('input', array('type' => 'text', 'value' => $maxOrderNumber));
}
Instead of update, use success callback:
echo $form->dropDownList($model, 'id_subkeg', array(), array(
'style' => 'width: 100%',
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('dynamicSatuan'),
'data' => array('id_subkeg' => 'js:this.value'),
'success'=> 'function(data) {
$("#your_id_here").empty();
$("#your_id_here").append(data);
}'
)
)
);

CakePHP 2.4: Unwanted pre-filled form data

I have a form to add a new user. Only an admin who is logged in may access this form. Unfortunately, the username and the password of the admin are filled into the form fields which are expected to be completely clear. And one really strange thing is: The username is printed into the birthday field!
I really cannot explain myself how it works. And I could not found in the WWW any post from a person who has got the same problem - I only found questions and answers about pre-filled form data that is wanted.
This is the View /Users/add.ctp
<h1>Add a new Member</h1>
<?php echo $this->Form->create('User', array('url' => BASE_URL.'/users/add', 'action'=>'post')); ?>
<table class="form">
<tr><td>Username:</td><td><?php echo $this->Form->input('User.username', array('label' => false, 'div' => false, 'value' => ''));?></td></tr>
<tr><td>Name:</td><td><?php echo $this->Form->input('User.name', array('label' => false, 'div' => false, 'value' => ''));?></td></tr>
<tr><td>Lastname:</td><td><?php echo $this->Form->input('User.lastname', array('label' => false, 'div' => false, 'value' => ''));?></td></tr>
<tr><td>E-Mail:</td><td><?php echo $this->Form->input('User.email', array('label' => false, 'div' => false, 'value' => ''));?></td></tr>
<tr><td>Birthday:</td><td><?php echo $this->Form->input('User.birth', array('label' => false, 'div' => false, 'value' => ''));?></td></tr>
<tr><td>Password:</td><td><?php echo $this->Form->input('User.password', array('label' => false, 'div' => false, 'value' => ''));?></td></tr>
</table>
<?php
echo $this->Form->submit('Submit', array('formnovalidate' => true));
echo $this->Form->end();
?>
And here is the Controller /UsersController.php
public function add() {
$this->layout = 'admin';
if ($this->request->is('post')) {
// Saving the data
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('Data saved.'));
return $this->redirect(array('action' => 'view'));
}
$this->Session->setFlash(__('Data could not be saved.'));
}
}
By the way: Saving works fine.
Of course, the admin is of Object User, as is the new member to be added. I think, here lies the problem, but I really do not know... I am thinking about this problem the whole day :( Does anybody know what to do?
Thanks in advance.
Is not your browser? (saved username/password when you type for the first time)
So, you can turn of the autocomplete.
<?php echo $this->Form->create('User', array('url' => BASE_URL.'/users/add', 'action'=>'post', 'autocomplete' => 'off')); ?>
This option => 'autocomplete' => 'off'
Check your $this->data.
CakePHP autocompletes forms with data found there because it guesses that is data already submitted by the user.
In you example, if you have some value in $this->data['User']['birth'] it should show that value in the Birthday input.

Cakephp form has to be submitted twice to work

I am trying to sort out an issue with a form submit that I have been unable to understand. When I first submit the form, after changing the value of a dropdown, the $this->request->data array is empty. If I submit again I see what I would expect. This happens every time I change either of the dropdowns on the form.
Here is the form:
<?php
echo $this->Form->create('Refine', array('url' => '/ServiceDirectoryResults/refine'));
echo $this->Form->input('state', array(
'type' => 'select',
'label' => 'State',
'options' => $all_states,
'selected' => array('state_selected', $state_selected),
'id' => 'state',
));
echo $this->Form->input('solution', array(
'type' => 'select',
'label' => 'Solution',
'options' => $solutions,
'selected' => array('selected', $solution),
'id' => 'solutions',
));
echo $this->Form->input('region', array(
'before' => '<fieldset id="Region">',
'multiple' => 'checkbox',
'options' => $regions,
'selected' => $reg_selected,
'after' => '</fieldset>'
));
echo $this->Form->input('tags', array(
'before' => '<fieldset id="TagBox">',
'multiple' => 'checkbox',
'options' => $narrow,
'selected' => $tag_selected,
'after' => '</fieldset>'
));
echo $this->Form->end('Refine Search');
?>
The form is rendering fine. If the states or solutions dropdowns are changed and the form is submitted the $this->request->data array is empty. If I submit a second time, without changing anything, the array contains what I would expect to see.
In my Controller I have
if(isset($this->request->data['Refine']['state']))
{
$state = $this->request->data['Refine']['state'];
}
Obviously if the array is empty I get nothing in the state variable the first time the form is submitted.
I would appreciate it if anyone could shed some light on this behaviour. Have I done something wrong in my form creation?
As requested here is the js that is used with this form. The idea is that it just takes care of setting or clearing the checkboxes if the "All" checkbox, which is the first checkbox created for both regions and tags in the controller.
$(document).ready(function(){
$("#RefineRegion0").click(function(){
if ($("#Region #RefineRegion0").is(':checked')) {
$("#Region input[type=checkbox]").each(function (e) {
$(this).prop("checked", true);
});
} else {
$("#Region input[type=checkbox]").each(function (e) {
$(this).prop("checked", false);
});
}
});
$("#RefineTags0").click(function(){
if ($("#TagBox #RefineTags0").is(':checked')) {
$("#TagBox input[type=checkbox]").each(function (e) {
$(this).prop("checked", true);
});
} else {
$("#TagBox input[type=checkbox]").each(function (e) {
$(this).prop("checked", false);
});
}
});
$("#RefineViewForm").submit(function(){
if($('#state').val() == "" || $('#solutions').val() == ""){
alert("Please select a State and Solution before continuing")
}
});
});
Hope that helps
I noticed two things:
1) The form's url: controllers names are lowercase and underscored: service_directory_results. See the cakephp names convetions: http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html . But I think its better to use the array for the url so your routes
can be matched:
echo $this->Form->create('Refine', array('url' => array('controller' => 'service_directory_results', 'action' => 'refine')));
2) On your Js if these fields are empty don't send the post adding return false; (also missing a ;)
$("#RefineViewForm").submit(function(){
if($('#state').val() == "" || $('#solutions').val() == ""){
alert("Please select a State and Solution before continuing");
return false;
}
});

Dynamically Change the Row Color Based on the Column value in CGridView

First, actually I'm not using CGridView, but I'm using TbExtendedGridView from YiiBooster. I use CGridView on the title because it's more familiar, but still these two things works in quite the same way.
I'm enabling inline edit on my TbExtendedGridView (TbJEditableColumn). TbExtendedGridView is using jquery's Jeditable for this functionality. This Jeditable also applicable to CGridView.
Thanks to this SO's question: Change the Row Color Based on the Column value in CGridView
I know how to change the row color. But, it's still not working with the inline edit functionality. So, my question is, how can that rowCssClassExpression be updated everytime I edit some value on a column?
This is my view's code on [root]/protected/views/transaction/admin.php
<?
$this->widget('bootstrap.widgets.TbExtendedGridView', array(
'id'=>'transaction-grid',
'rowCssClassExpression'=>'$data->getCssClass()',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name'=>'date',
'value'=> 'date("j M Y", strtotime($data->date))',
'htmlOptions' => array('style'=>'width:52px')
),
array(
'name' => 'amount',
'value' => 'number_format($data->amount, 0, ",", ".")',
'htmlOptions' => array('class'=>'currency', 'style'=>'width:72px')
),
array(
'name' => 'category_name',
'value'=>'$data->category->name',
'header'=>'Category',
'sortable' => 'true',
'htmlOptions' => array('style'=>'width:131px'),
'class'=>'bootstrap.widgets.TbJEditableColumn',
'jEditableOptions' => array(
'type' => 'optgroup',
'loadurl' => Yii::app()->baseUrl . '/index.php/transaction/getCategory',
'submitdata' => array('attribute'=>'category'),
'cssclass' => 'form',
'width' => '180px',
'submit' => 'save changes'
)
),
array(
'name'=>'desc',
'value'=>'$data->desc',
'htmlOptions' => array('class'=>'desccell'),
'class'=>'bootstrap.widgets.TbJEditableColumn',
'jEditableOptions' => array(
'type' => 'text',
// very important to get the attribute to update on the server!
'submitdata' => array('attribute'=>'desc'),
'cssclass' => 'form',
'width' => '180px',
)
),
array(
'htmlOptions' => array('nowrap'=>'nowrap'),
'class'=>'bootstrap.widgets.TbButtonColumn',
)
)
)
And this is my getCssClass code on [root]/protected/models/Transaction.php:
public function getCssClass(){
$categoryType = Category::model()->findByPk($this->categoryId)->getAttribute("type");
$categoryName = Category::model()->findByPk($this->categoryId)->getAttribute("name");
$class = "";
if($categoryName == "Uncategorized Income"){
$class = "darkgreen";
}
else if($categoryName == "Uncategorized Expense"){
return "darkred";
}
else if($categoryType == "INCOME"){
return "green ";
}
else if($categoryType == "EXPENSE" || $categoryType == "COST OF GOODS"){
return "red ";
}
else if($categoryType == "WITHDRAW" || $categoryType == "DEPOSIT" ){
return "blue ";
}
else{
return "grey ";
}
return $class . " " . $categoryName . " " . $categoryType;
}
Use the 'afterAjaxUpdate' attribute to fire a javascript function on any update.
'afterAjaxUpdate' => ' function(){ //enterJScode }',

Categories