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.
Related
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
Model:
function search()
{
$cari=$this->db->query("select * from uhd_user_order where user_order_reference ");
return $cari->result();;
}
Controller:
function search_keyword()
{
$this->input->GET('cari', TRUE);
$beda['cari'] = $this->order_test_m->search();
$this->load->view('admin/a',$beda);
}
View:
<?php if(count($cari)>0){
foreach ($cari as $row => $test) {
}?>
<?= $test->sendername ?>
<?php
} ?>
I need to make a search, and the result of the view will show up if I put the right code only. Like, if I only search "s", the data from my database will not show up.
How can this be done?
I am not sure what you want, I have some questions about your code, but I can not comment yet. I have to guess what happend:
I think your code works as: Whenever the 'search' button is clicked, all of your user's name in your database will be displayed, right?
Because your model does not use the 'get' data to search in the database
//Your function does not recieve the search string.
//This function uses $string to search in the 'users' table, to finds the name field == $string, and returns only 'name' row.
function search($string){
$query = $this->db->select('name')
->where('name',$string)
->get('users');
//If the result is empty, return false, do nothing.
if($query->num_rows() === 0){
return false;
}
return $query->result();;
}
Controller
function search_keyword()
{
// Call search function with the input data.
// I think here will have some validation
//for example:
//if($this->form_validation->run() == false){
// $this->load->view('errors/error');
//}
$search_string = $this->input->GET('cari', TRUE);
$beda['cari'] = $this->order_test_m->search($search_string);
$this->load->view('admin/a',$beda);
}
View:
// This will ensure the ul will only display when $cari is not empty.
<?php if(count($cari)>0):?>
<ul>
<?php foreach($cari as $row):?>
<li><?php echo $row->name;?></li>
<?php endforeach;?>
</ul>
<?php endif; ?>>
I am working on a page that can update the sales agent on a specific order.
I made the list of options and a dropdown list is created.
Heres in the controller:
$order = $this->Order->read(null,$id);
$this->set("order",$order);
if ($this->request->is("post")) {
if($this->Order->save($this->request->data)) {
$this->Session->setFlash("Sales Agent Updated");
}
}
Heres the view:
echo $this->Form->create("Order");
echo $this->Form->input("OrderID");
echo $this->Form->input("UserID");
echo $this->Form->submit("Submit");
echo $this->Form->end();
When I submit the data, it appears that the data is saved, (the flash message is set).
However, when I then preset the fields with data that is already in the database, all the sudden it doesn't even post. (I put a debug after the reques->is("post) condition which doesnt show up after I submit).
$order = $this->Order->read(null,$id);
$this->set("order",$order);
if ($this->request->is("post")) {
if($this->Order->save($this->request->data)) {
$this->Session->setFlash("Sales Agent Updated");
}
}
if (!$this->request->data) {
$this->request->data = $order;
}
The input fields are correctly pre filled, but now the form doesn't even post.
Does anybody know whats wrong?
Thanks!
Update your controller code to:
$order = $this->Order->read(null,$id);
$this->set("order",$order);
if ($this->request->is('post') || $this->request->is('put')) {
if($this->Order->save($this->request->data)) {
$this->Session->setFlash("Sales Agent Updated");
}
}
Now put a debug after the reques->is('post') condition which will show what you need to show.
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();
}
}
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.
}