Values not passed from ctp file to controller in CakePHP - php

I have tried several solution posted in this forum and others as well but it has not helped so far. So I am posting my question finally. BTW, I am using CakePHP 3.6.
I am trying to pass a variable ($product->id) via submit button in view.ctp to my controller action "addit" but I just get "Undefined variable: id " (I have tried addit($id) and addit() either of case I have the same result.)
view.ctp
<p>
<?php echo $this->Form->create('NULL',['url'=>['controller'=>'products','action'=>'addit']]);?>
<?php echo $this->Form->input('id', ['type' => 'hidden', 'value' => $product->id]); ?>
<?php echo $this->Form->button('Add to cart now');?>
<?php echo $this->Form->end();?>
</p>
Controller:Products
public function addit() {
$this->autoRender = false;
if ($this->request->is('post')) {
// $this->Products->addProduct($this->request->data['Cart']['product_id']);
echo "".$this->Products->get($id);//for test
} else {
echo "".$this->Products->get($id);//for test
}
}

According to Cakephp 3.6
All POST data can be accessed using
Cake\Http\ServerRequest::getData(). Any form data that contains a data
prefix will have that data prefix removed. For example:
// An input with a name attribute equal to 'MyModel[title]' is accessible at
$title = $this->request->getData('MyModel.title');
You can get value of $id variable like this:
$id = $this->request->getData('id');
Further Reading: Request Body Data

Is this what you want to do?
$id = $this->request->getData('id');
debug($this->Products->get($id)); //for test

Related

placeholder attribute in zf2

How could insert the value of a field's db in the plcaholder in zf2
<div class="form_element">
<?php
$this->placeholder('name')->data = $this->data;
$name = $form->get('name');
echo $formLabel->openTag().$name->getOption('label')." ";
echo $this->formInput($name);
echo $formLabel->closeTag();
?>
</div>
A placeholder is a ViewHelper and therefore is is designed to help render view content.
In order to use your database data witin a placeholder you will need to ensure that the data is first passed to the view from the controller action.
public function modificaAlumnoAction()
{
//...
return ViewModel('data' => $data); // data passed to the view instance
}
Then within the view script
// modifica-alumno.phtml
$this->placeholder('foo')->data = $this->data;
An finally output the data (such as within the layout)
// layout.phtml
echo $this->placeholder('foo)->data;

Get Value of a hidden form_input using CodeIgniter

I cannot get the hidden value of form if it is hidden.
My form view:
<?php echo form_input(array(
'class'=>'emp_name',
'name'=>'emp_name',
'id'=>'emp_name',
'value'=>'')
);?>
<?php echo form_hidden('emp_id', ''); ?>
I set employee name by using jquery autocomplete and then set the emp_id value to the returned ID with the name.
My Controller:
$data = array(
'emp_id'=>$this->input->post('emp_id')
);
This conroller is the form of my view above. I can get the emp_name properly but not the emp_id because it is hidden, if I do not use hidden it works fine. Any idea how can I hide the ID by getting the value in my conntroler?
It looks like you are not submitting the form to the system.
You need to open/close and submit the form.
Quick little test:
class Test_form extends CI_Controller
{
function __construct()
{
parent::__construct();
//displays the profiler info to make debugging easy
$this->output->enable_profiler(TRUE);
}
function test_form()
{
echo form_open();
echo form_input(array(
'class'=>'emp_name',
'name'=>'emp_name',
'id'=>'emp_name',
'value'=>'')
);
echo form_hidden('emp_id', '');
echo form_submit();
echo form_close();
}
}

Two submit buttons, one form (Yii)

I have two CHtml::submitButton() in my form, namely "Accept" and "Reject". Each of the buttons has a specific ID ... When any of the two are pressed, i would like to trigger an action in my controller. Below are the code snippets.
=========================================================
View
<?php
echo CHtml::beginForm('mycontrollerName/AcceptUserRegistration','get');
?>
<?php echo CHtml::submitButton('Accept', array('id' => 'accept')); ?>
<? echo ' '; ?>
<?php echo CHtml::submitButton('Reject', array('id' => 'reject')); ?>
<?php echo CHtml::endForm(); ?>
========================================================
Controller
public function actionAcceptUserRegistration() {
$value = $_GET['id'];
if($value == "accept"){
//will do something here
}
if($value == "reject"){
//will do something here.
}
}
======================================================================
When i implement it this way. the get value in the controller side is empty. What I'm I doing wrong? Or is there any other way around this?
You forgot to give the submit button a name (unless your framework does some magic which we cannot know about - you should really post the generated HTML code!).
If you do so, the value in $_GET['buttonname'] will be its value (not its id), i.e. Accept or Reject. This becomes pretty messy as soon as you get into i18n, so you might want to use different names for the buttons and then check isset($_GET['buttonname1']) and isset($_GET['buttonname2'])
Here is a code example for what you are trying to achive:
VIEW:
<?php
echo CHtml::beginForm('mycontrollerName/AcceptUserRegistration','get');
?>
<?php echo CHtml::submitButton('Accept', array('id' => 'accept', 'name' => 'accept')); ?>
<? echo ' '; ?>
<?php echo CHtml::submitButton('Reject', array('id' => 'reject', 'name' => 'reject')); ?>
<?php echo CHtml::endForm(); ?>
Note: I added the name parameter. As name is not displayed to the user, you can use accept instead of Accept.
CONTROLLER:
public function actionAcceptUserRegistration() {
if(isset($_GET['accept'])) {
//accepted
} else if(isset($_GET['reject'])) {
//rejected
}
//I recommend using POST method instead of GET for posting the form.
}

Make form remember earlier submitted values with CodeIgniter

I have this contact form with codeigniter, and what I want to do is, when the form is submitted but does not pass validation, I want the fields to contain earlier submitted values.
There's one thing though: when the form is loaded all the fields already have a certain value assigned, so for example the "name field" shows "name" inside the field. And I want this to stay that way, unless "name" is changed and the form is submitted, in that case it should have the new value.
So for the moment I have this:
<?php echo form_input('name', 'Name*');?>
<?php echo form_input('email', 'Email*');?>
But I don't know how to make the form remember any new submitted values.
Anyone any idea?
I'd recommend using CodeIgniter's set_value method.
<?php echo form_input('name', set_value('name', 'Name*')); ?>
I think the answer lies in the controller.
Personally, I started letting the forms controller function handling the validation:
<?php
class Page extends Controller
{
...
function showform()
{
$this->load->helper(array('form', 'url'));
$data = array("name" => "Name*", "email" => "Email*");
$failure = false;
if( $this->input->post("name") )
{
$data["name"] = $this->input->post("name");
$data["email"] = $this->input->post("email");
if( !valid_email($data["email"]) )
{
$failure = true;
$data["error_message"] = "E-mail couldn't validate";
}
if( !$failure )
redirect('/page/thankyou/', 'refresh');
}
$this->load->vars($data);
$this->load->view("theform");
}
...
}
And in your view, you would do something like this:
<?php echo form_input('name', $name);?>
<?php echo form_input('email', $email);?>
If you are submitting to the same controller, you can get the previously submitted variables through $_POST (or $_GET depending on which form method you chose).
// use $_POST['name'] if set, else use 'Name*'
<?= form_input('name', (!empty($_POST['name']) ? $_POST['name'] : 'Name*'); ?>

PHP data array variable not defined in view

I am using codeIgniter and I am trying to pass an array of data. I have written like this:
$data['username']="Dumbo";
I also wrote this:
$data['shouts']=$this->Musers->getShout(); // retrieve data from table
Then I write:
$this->load->view("welcome_message", $data);
In view page, I wrote:
<?php echo $username;
foreach ($shouts as $shout)
{
echo $shout->shout;
echo '<br>';
echo $shout->timeStamp;
}
?>
Problem is that while the view did retrieve data from table and display results in view page, an error came up for $data['username'] saying:
"Undefined variable: username"
Why is that? The $data['username'] is already defined! Or what did I do wrong?
<?php echo $data['username']; ?>
If you wrote this, the error will occur.
Correct way is to write like
<?php echo $username; ?>
'username' is the index in the data array, which is passed to the view using the load method
$this->load->view("welcome_message", $data);
If you need to pass an array...
$data['usernames'] = $username_array;
$this->load->view("welcome_message", $data);
Then in the view,
<?php print_r($usernames); ?>
In your view, do this..
$data = array();
$data['username'] = "something";
$data['shouts']=$this->Musers->getShout();

Categories