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
Related
Need assistance with following code. Value for "customer_id" is not posting after hitting submit for the first generated row only. the second and following work as intended.
Other pages use this same format for deleting, but do not redirect to another page like this one does. No issues with deleting.
<html>
<!-- the body section -->
<body>
<main>
<section>
<!-- display a table of customers -->
<h2>Results</h2>
<table>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>City</th>
<th> </th>
</tr>
<?php foreach($customers as $cus) : ?>
<tr>
<td><?php echo $cus['firstName'].' '.$cus['lastName']; ?></td>
<td><?php echo $cus['email']; ?></td>
<td class="right"><?php echo $cus['city']; ?></td>
<td><form action="view_and_update.php" method="post" id="search">
<input type="text" name="customer_id" value="<?php echo $cus['customerID']; ?>">
<input name="submit" type="submit" value="Select">
<?php include "redirect.php";?>
</form></td>
</tr>
<?php endforeach; ?>
</table>
</section>
</main>
</body>
</html>
redirect.php looks like this:
<?php
if(isset($_POST["submit"])){
// To redirect form on a particular page
header("Location: view_and_update.php");
}
?>
Any assistance is greatly appreciated. This is for an assignment, and we are using the languages as specified with XAMPP/myphpadmin. No problem retrieving, editing, adding, or deleting data in any other circumstance. This is the only problem page.
By using the following example code I got the correct post to view_and_update.php
It has to be something else, it is not the code.
<?php $customers = [
['firstName' => 'Alice', 'lastName' => 'Al', 'email' => 'alison#domain.com', 'city' => 'MyCity', 'customerID' => 123],
['firstName' => 'Bob', 'lastName' => 'Bo', 'email' => 'bob#domain.com', 'city' => 'MyCity', 'customerID' => 124]
]; ?>
<html>
<!-- the body section -->
<body>
<main>
<section>
<!-- display a table of customers -->
<h2>Results</h2>
<table>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>City</th>
<th> </th>
</tr>
<?php foreach($customers as $cus) : ?>
<tr>
<td><?php echo $cus['firstName'].' '.$cus['lastName']; ?></td>
<td><?php echo $cus['email']; ?></td>
<td class="right"><?php echo $cus['city']; ?></td>
<td><form action="view_and_update.php" method="post" id="search">
<input type="text" name="customer_id" value="<?php echo $cus['customerID']; ?>">
<input name="submit" type="submit" value="Select">
</form></td>
</tr>
<?php endforeach; ?>
</table>
</section>
</main>
</body>
</html>
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.
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!
<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.
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.