Get Value of a hidden form_input using CodeIgniter - php

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();
}
}

Related

Values not passed from ctp file to controller in CakePHP

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

How to pass a value of a variable from view to controller in codeigniter

using form post method i need to pass a value of numIDto a controller
for example assigning a value to numID inside view,
$numID= 25;
i need to use value of numID in a controller and assign that to temp variable. so i use following line of code,
$temp= $_POST[numID];
but its unable to get the exact value. Please help me on this. If have any other alternate way please let me know.
Here is an example of sending info from view to the controller:
View:
<?php echo form_open('invoice'); ?>
<?php echo validation_errors(); ?>
<?php echo form_input('order_num', $this->input->post('order_num')); ?>
<?php echo form_submit('submit','submit'); ?>
<?php echo form_close(); ?>
Controller:
public function invoice()
{
$order = $this->input->post('order_num');
$this->General_Model->get_customer_order($order);
}
Model:
function get_customer_order($order)
{
. . .
$this->db->where('client_orders.id', $order);
$result = $this->db->get('client_orders');
. . .
}
Note we are using the input name and id as = "order_num"
Then we ask the controller to find it as $this->input->post('order_num');

dropdown list with submit in yii

i want to have a drop down list and a submit button, when the button pressed i want to get the text of selected item and store it into db. my drop down code in view is :
<?php
$form=$this->beginWidget('CActiveForm', array(
'enableAjaxValidation'=>true,
'clientOptions'=>array('validateOnSubmit'=>true
),
)); ?>
<?php echo $form->labelEx($model,'actTopic'); ?>
<?php echo $form->error($model,'actTopic'); ?>
<?php
echo CHtml::dropDownList('actTopic', $category,$list,
array('empty' => '... '));
?>
it generates a drop down list fine.
but i don't know how i can get the selected item in my action function.
the action function is:
public function actiongivePoint()
{
$model=new GivePointForm;
if(isset($_POST['GivePointForm'])){
$model->attributes=$_POST['GivePointForm'];
if($model->validate()){
$actTopic=($model->actTopic);
$actPoint=($model->actPoint);
$actId=($model->actId);
$stCode = (int) $_GET['stCode'];
$connection=Yii::app()->db;
$connection->active=TRUE;
$actIdReader =Yii::app()->db->createCommand()
->select ('actID')
->from('refrencepoint')
->where("actTopic=:actTopic")
->queryScalar(array(':actTopic'=>$actTopic));
$search =Yii::app()->db->createCommand()
->select ('count(*)')
->from('point')
->where(array('and', 'actId=:actID', 'stCode=:stCode') )
->queryScalar(array(':actId'=>$actId,':stCode'=>$stCode));
if($search !=0){
$month=CDateFormatter::formatMonth(now());
$year=CDateFormatter::formatMonth(now());
$command =$command->update('point', array(
'year'=>$year,'month'=>$month,
'pcounter'=>new CDbExpression('pcounter + 1'),),
(array('and','actId'=>$actId, 'stCode'=>$stCode))) ;
Yii::app()->user->setFlash('point','....');
}
else{
$month=CDateFormatter::formatMonth(now());
$year=CDateFormatter::formatMonth(now());
$command->insert('point', array('actId'=>$actId,
'stCode'=>$stCode,'year'=>$year,'month'=>$month,
'pcounter'=>new CDbExpression('pcounter + 1')));
Yii::app()->user->setFlash('point','....');
}
}
}
$this->render('givePoint',array('model'=>$model));
}
i should say i don't use active record.
with above codes it looks like everything is ok in view, but when i press submit button nothing happen, no change in db and even no error just the page refresh!!
what is wrong here?
i am so tired of trying....
I think you just use like it.
you first use foreach loop.I am not sure which type of things your are listing. but you need to do work like in below.
It is an example
foreach($model->category as $list) {
$list['id'] = $list->name;
}
echo CHtml::dropDownList($model, 'actTopic', $list);
Now you debug your code.
public function actiongivePoint()
{
$model=new GivePointForm;
if(isset($_POST['GivePointForm'])){
print_r($_POST['GivePointForm']);
exit; //Just check either the form is posted or not and values are coming or not.
$model->attributes=$_POST['GivePointForm'];
if($model->validate()){
$actTopic=($model->actTopic);
echo $actTopic ;
exit ; // Check either your dropdown item id is coming or not.
Hope its help you.
Thanks.

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.
}

CakePHP adding columns to a table

I have a Profile model/controller in my cake app as well as an index.ctp view in /views/profiles. Now, when I go to add a column to my table that is already filled with data, and then add the corresponding code to the view to pick up this column's data, it just gives me an empty result.
My model:
<?php
class Profile extends AppModel
{
var $name = 'Profile';
}
?>
My controller:
<?php
class ProfilesController extends AppController
{
var $name = 'Profiles';
function index()
{
$this->set('profiles', $this->Profile->find('all'));
}
}
?>
My views printing (stripped down):
<?php foreach ($profiles as $profile): ?>
<?php echo $profile['Profile']['id']; ?>
<?php echo $profile['Profile']['username']; ?>
<?php echo $profile['Profile']['created']; ?>
<?php echo $profile['Profile']['thumbnail'];?>
<?php echo $profile['Profile']['account'];?>
<?php endforeach; ?>
Basically, the columns id, username, column, thumbnail always have been printing fine, but when I add a column called accountit returns no information (nothing prints, but no errors). Any suggestions?
I would delete the files in app/tmp/cache (but keep the directories).
For CakePHP 4, clearing tmp/cache/models worked for me.

Categories