Form not updating database table in cakephp code? - php

Hey everyone I am facing this problem the form is not updating when I click submit. It goes in if($this->request->is("post")){....} block but it is not updating.
Here is the code.
PagesController.php
public function admin_edit($id=NULL){
$this->request->data = $this->Page->find("first",array("conditions"=>array("Page.id"=>$id)));
if($this->request->is('post')){
$this->request->data["Page"]["id"] = $id;
if($this->Page->save($this->data)){
echo "Done";
}
}
}
admin_edit.php
URL -> http://localhost/cakephp_practice/admin/pages/edit/4
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="DataTable">
<tr>
<th>Administrator > Edit Page</th>
</tr>
<tr>
<td>
<?php
echo $this->Form->create('Page', array("action" => "edit", "method" => "Post",'enctype' => 'multipart/form-data', 'id' => 'editPage'));
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2"> <span class="require">* Please note that all fields that have an asterisk (*) are required. </span></td>
</tr>
<tr>
<td>Name: <font color="red">*</font></td>
<td align="left"><?php echo $this->Form->text('Page.name', array('maxlength' => '254', 'size' => '20', 'label' => '', 'div' => false, 'class' => "form-inbox required")) ?>
</td>
</tr>
</table>
<table>
<tr>
<td> </td>
<td>
<?php echo $this->Form->submit('Submit', array('maxlength' => '50', 'size' => '30', 'label' => '', 'div' => false)) ?>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<?php echo $this->Form->end(); ?>
</td>
</tr>
</table>
I have used Configure::write('Routing.prefixes', 'admin'); to automatically resolve url like localhost://project_name/admin/pages/edit to admin_edit action of pages controller.
Edit: When I print_r($this->request->data); in if block then the name field is still containing the old value not the new one I entered. Why is that?

This line:-
if($this->Page->save($this->data)){
Should be:-
if($this->Page->save($this->request->data)){
However, the first line of your method is overwriting $this->request->data which should contain your form data!

Related

Codeigniter 3.x, Validation Always Passes Whether Checkbox Checked or Not

I am trying to make selecting one checkbox from a total of four checkboxes required but it's not working. Whether I select a checkbox or not, it always passes validation.
Here is my method in my controller Cargo_inquiry() {}
function step_two() {
$country_to_select = $this->session->userdata('freight_address_country');
$this->data['users'] = $this->My_Users_Model->get_companies_list($country_to_select);
// load up the validation rules for blog Info form
$this->config->load('validate_cargo');
$this->form_validation->set_rules($this->config >item('validate_cargo_create_step_two') );
if ($this->form_validation->run('validate_cargo_create_step_two') === FALSE) {
echo "FALSE";
// die;
$this->load->view('_layouts/main/template1', $this->data);
} else {
echo "TRUE";
// die;
redirect('/Cargo_inquiry/step_three');
}
}
Here is my validation rules located in validate_cargo.php:
$config['validate_cargo_create_step_two'] = array(
'user_id' => array(
'field' => 'user_id',
'label' => '',
'rules' => 'required',
'errors' => array(
'required' => 'Please select at least one company.',
),
),
);
Here is my view/form:
<?php echo form_open('Cargo_inquiry/step_three',array('class'=>'form-horizontal'));?>
<table class="table table-bordered table-striped" id="mytable">
<thead>
<tr>
<th width="3%">Select</th>
<th>company</th>
<th>location</th>
<th>rating</th>
<th>web</th>
<th>Associations/serv_hhgpe</th>
</tr>
</thead>
<tbody>
<?php
foreach ($users as $user) { ?>
<tr>
<td>
<?php // echo form_checkbox('user_id[]', 'user_id[]', set_checkbox("user_id[]"), "Company"); ?>
<input type="checkbox" name="user_id" value="<?php echo $user->id; ?>" />
</td>
<td>
<?php echo $user->company ?>
</td>
<td>
<?php echo $user->city ?>
</td>
<td>
<?php echo $user->state_province ?>
</td>
<td>
<?php echo $user->name ?>
</td>
<td style="text-align:center" width="200px">
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php echo form_submit('cargo_create_step_two', 'Next->', 'class="btn btn-primary btn-lg btn-block"');?>
<?php echo form_close();?>
Arrrgg!!! I was submitting to the wrong form :( So very sorry.

To edit profile before i have to retrieve a data from database in cakephp

<table>
<tr>
<?php echo $this->Form->input('firstname',array('type' => 'text','value'=>$firstname));?>
</tr>
<tr>
<?php echo $this->Form->input('lastname',array('type' => 'text','value' => $lastname)); ?>
</tr>
<tr>
<?php echo $this->Form->input('username',array('type' => 'text','value'=>$username));?>
</tr>
<tr>
<?php echo $this->Form->input('gender',array('type' => 'select','value' => $gender)); ?>
</tr>
<tr>
<?php echo $this->Form->input('email',array('type' => 'text','value'=>$email));?>
</tr>
<tr>
<?php echo $this->Form->input('dateofbirth',array('type' => 'date','dateFormat' => 'YMD','value' => $dateofbirth)); ?>
</tr>
<tr>
<?php echo $this->Form->input('Mobile',array('type' => 'text','value' => $mobile)); ?>
</tr>
<tr>
<?php echo $this->Form->end('Save'); ?>
</tr>
</table>
I retrieved all data and put in the text box, but I don't know how to select a radio button
echo $this->Form->input('gender',array('type' => 'select','value' => $gender)); ?>
And a dropdown box also
echo $this->Form->input('dateofbirth',array('type' => 'date','dateFormat' => 'YMD','value' => $dateofbirth)); ?>
Please Help these two things. I am using Cakephp 2.5.4 version.

How to get the data in the controller with CakePHP?

I have an HTML table with some fields, and each row has a link to save the data. Can I redirect to the controller, but do not know how to get the data in the controller to save.
<table class="table table-bordered">
<thead>
<tr>
<th>User</th>
<th>Value</th>
<th>Category</th>
<th>#</th>
</tr>
</thead>
<tr>
<td>Gleydson</td>
<td><input class="form-control" type="text" name="value"></td>
<td>
<select class="form-control">
<option value="tipo">Type 1</option>
<option value="tipo">Type 2</option>
<option value="tipo">Type 3</option>
</select>
</td>
<td> <?php echo $this->Html->link(__('Register'),'/payments/register/'.$user['User']['id'],array('class' => 'btn btn-success')) ?> </td>
</tr>
<tr>
<td>Emília</td>
<td><input class="form-control" type="text" name="value"></td>
<td>
<select class="form-control">
<option value="tipo">Type 1</option>
<option value="tipo">Type 2</option>
<option value="tipo">Type 3</option>
</select>
</td>
<td> <?php echo $this->Html->link(__('Register'),'/payments/register/'.$user['User']['id'],array('class' => 'btn btn-success')) ?> </td>
</tr>
</table>
You should be using the Form Helper to build your form. Check out the docs here: http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
If you're submitting to a different controller you may have to use "action" or "url" within the ->create method. But the issue you're experiencing is that the post data isn't being submitted, mainly because you're using "link" and you're not posting or creating a form correctly.
Make sure to use debug($this->data); within the correct method of the controller to check the data submission.
The method you're using just produces a link (lorem)
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::link
It may be worth going through the getting started guides to get a better idea of how Cake works. http://book.cakephp.org/2.0/en/getting-started.html
If you're looking to then redirect the user after the form submission you can use code within your controller to redirect them to the correct location.
Your view:
<?php echo $this->Form->create('Register'); ?>
<table class="table table-bordered">
<thead>
<tr>
<th>User</th>
<th>Value</th>
<th>Category</th>
<th>#</th>
</tr>
</thead>
<tr>
<td>Gleydson</td>
<td><?php echo $this->Html->input('value', array('class' => 'form-control', 'label' => false, 'div' => false)); ?></td>
<td>
<?php echo $this->Form->select('types', array('Type 1', 'Type 2', 'Type 3'), array('class' => 'form-control')); ?>
</td>
<td> <?php echo $this->Html->submit(__('Register'),array('class' => 'btn btn-success')) ?> </td>
</tr>
<tr>
<td>Emília</td>
<td><?php echo $this->Html->input('value2', array('class' => 'form-control', 'label' => false, 'div' => false)); ?></td>
<td>
<?php echo $this->Form->select('types2', array('Type 1', 'Type 2', 'Type 3'), array('class' => 'form-control')); ?>
</td>
<td> <?php echo $this->Html->submit(__('Register'), array('class' => 'btn btn-success')) ?> </td>
</tr>
</table>
<?php echo $this->Form->end(); ?>
Your Controller:
Also see:
http://book.cakephp.org/2.0/en/models/data-validation/validating-data-from-the-controller.html
and
http://book.cakephp.org/2.0/en/controllers.html
public function update() {
if ($this->request->is('post')) {
if ($this->Register->validates()) {
return $this->redirect(
array('controller' => 'orders', 'action' => 'thanks')
);
} else {
$this->Session->setFlash(
__('Unable to validate')
);
}
}
}
More info on saving your data:
http://book.cakephp.org/2.0/en/models/saving-your-data.html

Displaying data from database in tabular form in Yii

I have some datas stored in my table Jobs. How can I display them in a tabular form in my view section? I have visited the Yii forums but have not got any specific options to perform the action.
The action for view job:
public function actionViewJob() {
$criteria = "";
$model = new ViewJob();
/* What Should I do Here */
$this->render('viewjob', array(
'model' => $model
));
}
And the corresponding view to list data from database:
/* What should I do Here. */
<h1>View Jobs</h1>
<div class="flash-success"></div>
<div class="form">
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="filtertable" style="float: left;">
<thead>
<tr>
<th width="11%" class="rowtop">No</th>
<th width="24%" class="rowtop">Title</th>
<th width="10%" class="rowtop">Description</th>
<th width="10%" class="rowtop">No_Vacancy</th>
<th width="10%" class="rowtop">Contact Email</th>
<th width="10%" class="rowtop">Edit</th>
<th width="10%" class="rowtop">Delete</th>
</tr>
</thead>
<tbody>
<?php // foreach ($posts as $post): ?>
<tr>
<td width="11%">
<?php ?>
</td>
<td width="24%">
<?php ?>
</td>
<td width="14%">
<?php ?>
</td>
<td width="14%">
<?php ?>
</td>
<td width="14%">
<?php ?>
</td>
</a>
</td>
</a>
</td>
</tr>
<?php //endforeach; ?>
</tbody>
</table>
<!-- form -->
In your action, call your view.
public function actionViewJob() {
$model = new Job('search');
$params =array('model'=>$model,
);
$this->render('viewjob', $params);
}
Get your data as a CActiveDataProvider object.
In your model, create a search function
public function search() {
$criteria=new CDbCriteria;
// TODO: Add other search criteria here
$criteria->compare('username','Tom',true);
return new CActiveDataProvider('Jobs', array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>'username ASC',
),
));
}
Then, in your view
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array(
'name' => 'title',
'type' => 'raw',
'value' => 'CHtml::encode($data->title)'
),
array(
'name' => 'description',
'type' => 'raw',
'value' => 'description',
),
),
));
At first you should get table data in controller via CActiveDataProvider. And then in view you can use widget - CGridView. Using this widget you can set params that you need. Columns, filters etc. Also you can paste your code, and we will try to decide your problem. Yii is very easy :)

Input values are converting into garbage values if validation error message is present for other field

I am using following form but when I am submitting form without entering any mandatory fields, then in other input fields values are converting into garbage values.
<?php echo form_open_multipart($formAction); ?>
// form action is coming from controller
<div id="content">
<table border="0" cellpadding="0" cellspacing="5" width="100%" >
<tr>
<td>First Name<em>*</em></td>
<td>
<?php
$data = array('name' => 'firstName');
echo form_input($data, $firstName);
?>
</td>
</tr>
<tr>
<td>Middle Name<em>*</em></td>
<td>
<?php
$data = array('name' => 'middleName');
echo form_input($data, $middleName);
?>
</td>
</tr>
<tr>
<td>Last Name<em>*</em></td>
<td>
<?php
$data = array('name' => 'lastName');
echo form_input($data, $lastName);
?>
</td>
</tr>
<tr>
<td colspan="2">
<div class="formbuttons">
<?php echo form_submit('submit', "Save", "class='button'"); ?>
</div>
</td>
</tr>
</table>
<?php echo form_close(); ?>
If I enter "test's" in first name field and submit form without entering other mandatory fields then in First Name text box it is showing test's.
Try this..
echo form_input($data, html_entity_decode($firstName,ENT_QUOTES));
this should work.

Categories