I've some problem in cakephp to send/receive an input form
// /View/Services/add_services.ctp
....
<?php echo $this->Form->create('Service', array('action'=>'addServices'))?>
<?php echo $this->Form->input("service_name", array('label'=>false, 'div'=>false, 'class'=>"umstyle5"))?>
<?php echo $this->Form->Submit(__("Add service Type"))?>
<?php echo $this->Form->end();?>
// /Controller/ServicesController
....
public function addServices(){
....
$service_name = $_POST[service_name];
....
}
The problem is that I receive this error:
Undefined index: service_name [APP/Controller/ServicesController]
What is wrong?
Thanks!!
Use $this->request->data.
public function addServices(){
....
$service_name = $this->request->data['Service']['service_name'];
....
}
I suggest you please check the Documentation properly,
There are clearly mentioned that we can fetch data like...
//Controller/RecipesController.php:
public function edit($id = null) {
if (empty($this->request->data)) {
$this->request->data = $this->Recipe->findById($id);
} else {
// Save logic goes here
}
}
Related
I am trying to set up platesphp for one of my projects.
One of the methods in the model checks for the email address supplied by a new user and tells if the email they trying to use exists or not.
Something like
class UserModel extends BaseModel
{
public $errors = [];
public function validate()
{
if (filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL) === false) {
$this->errors[] = 'Invalid email';
}
if ($this->emailExists($this->request->post['email'])) {
$this->errors[] = 'Email already exist';
}
}
protected function emailExists($email)
{
$sql = 'SELECT * FROM user_account WHERE email = :email';
-----
-----
$stmt->execute();
return $stmt->fetch() !== false;
}
}
and in the controller
public function register()
{
$this->load->getModel('UserModel');
if ($this->model_UserModel->registerUser($this->request->post)) {
echo "Success ... load (redirect) second page";
} else {
$data ['error'] = $this->model_UserModel->errors;
echo $this->template->render('home/home', $data);
}
}
If the email exists and I var dump $data ['error'] it says "Email already Exist" as defined in the validate method in the UserModel.
Now, I am trying to get the error message on my home template by adding these lines on the tpl file
<?php if (!empty($this->e($errors))): ?>
<?php foreach($errors as $error): ?>
<li><?=$this->e($error)?></li>
<?php endforeach ?>
<?php endif;?>
But now if I click on the register page the template says
Notice: Undefined variable: errors in C:\xampp\htdocs\vem\App\Views\template\register\registeruser.tpl on line 14
How do I move forward?
I even tried setting the $this->e($error) = '' but naa, shows another error.
On your controller, you're setting variable error, but in template, you're accessing variable errors (with s).
Try with
<?php if (#$error)): ?>
<?php foreach($error as $e): ?>
<li><?=$this->e($e)?></li>
<?php endforeach ?>
<?php endif;?>
You need use error not errors:
<?php if (!empty($this->e($error))): ?>
<?php foreach($error as $error_item): ?>
<li><?=$this->e($error_item)?></li>
<?php endforeach ?>
<?php endif;?>
I've two tables on my database, monitor [pk = idMonitor] and monitor_data [pk = idMonitor_data].
Please click you can see the tables fields here. As you can see i put the array data in table monitor_data.
I want to Update the condition for every idinventory where monitor_data.idMonitor = $id.
But first i want to display the current data of 'monitordate','idinventory', and 'condition' from database to my view.
My controller
public function edit($id=0) {
$dataa = $this->monitor_m->get_record(array('monitor_data.idMonitor'=>$id),true);
$this->data->monitordate = $dataa->monitordate;
$this->data->condition = $dataa->condition; <-line 20
$this->data->detail = $this->monitor_m->get_record(array('monitor_data.idMonitor'=>$id),true);
$this->template->set_title('SMIB | Monitoring')
->render('monitor_edit',$this->data);
}
My View (monitor_edit)
<?php echo form_open(site_url("monitor/ubah"),'data-ajax="false"'); ?>
<?php foreach ($detail as $items): ?>
<h4><?php echo '[ '.$items['idinventory'].' ] '?> </h4>
<?php echo form_label ('Condition : ');
echo form_dropdown('condition', array('good'=>'Good','broke'=>'Broken','lost'=>'Lost'),#$items['condition']);
?>
<?php endforeach; ?>
<?php echo form_close(); ?>
My Model
class Monitor_m extends MY_Model {
public function __construct(){
parent::__construct();
parent::set_table('monitor','idMonitor');
}
public function get_record($id = 0,$get_user = FALSE) {
$this->db->where($id);
if ($get_user){
$this->db->join('monitor_data','monitor_data.idMonitor = monitor.idMonitor');
$this->db->join('inventory','inventory.idinventory = monitor_data.idinventory');
$this->db->join('user','user.id_user = monitor.id_user');
}
$data = parent::get_array();
return $this->improve_data($data);
}
Here is my problem : its work fine for monitordate code in my controller, BUT i keep getting an error for condition code
Maybe because i use 'monitor_data.idMonitor' as my parameter $id not idinventory. how can i use 2 parameters for example like where idMonitor=$id and idinventory=$idiventory.
Do i explain it right ?
Severity: Notice Message: Trying to get property of non-object
Filename: controllers/monitor.php Line Number: 20
Please Please help me, i dont know what is wrong with my controller :( i've searching the solution but none of those work. :(
It's weird if you can get monitordate but can't get condition.
Can you edit your controller to be like this?
public function get_record($id = 0,$get_user = FALSE) {
$this->db->where($id);
if ($get_user){
$this->db->join('monitor_data','monitor_data.idMonitor = monitor.idMonitor');
$this->db->join('inventory','inventory.idinventory = monitor_data.idinventory');
$this->db->join('user','user.id_user = monitor.id_user');
}
// $data = parent::get_array();
$data = $this->db->result_array();
print_r($data);
echo $this->db->last_query();
exit;
return $this->improve_data($data);
}
I have a small issue i have declared a variable of type Boolean in my controller.
$done=False
Now there is a trigger in the controller that would turn it to True and it is at this point i would like to send it to the corresponding view with this controller .. i have used the following.
$done=True;
$this->view->setVar("done", $done);
Now when i try to call it in the corresponding view it does not know anything of this varible.
if($done==True)
{
echo'
<div class="alert alert-success">
Add Another Here!
</div>
';
}
It gives me the following:
Notice: Undefined variable: done in >C:\xampp\htdocs\Blueware\app\views\addNewSkill\index.phtml on line 36
Now is there a better way of sending this varible through to the view or am i making a mistake?
Full Controller/Action Code:
<?php
class addNewSkillController extends \Phalcon\Mvc\Controller{
public function indexAction(){
}
public function confirmAction(){
$this->view->disable();
$done=False;
if($this->request->isPost()) {
$dataSent = $this->request->getPost();
$skill = new Skills();
$skill->technology = $dataSent["technology"];
$skill->skillName = $dataSent["skillName"];
$skill->description = $dataSent["description"];
$savedSuccessfully = $skill->save();
if($savedSuccessfully) {
$done=True;
$this->view->setVar("done", $done);
} else {
$messages = $skill->getMessages();
echo "Sorry, the following problems were generated: ";
foreach ($messages as $message) {
echo "$message <br/>";
}
}
} else {
echo "The request method should be POST!";
}
}
}
Full View Code:
<?php
if($done==True)
{
echo'
<div class="alert alert-success">
Add Another Here!
</div>
';
}
?>
if($savedSuccessfully) {
$done=True;
$this->view->setVar("done", $done);
} else {
You should be setting the variable there, But setting in the view after seems that its not saving and therefore not passing the variable on
similar to
if($savedSuccessfully) {
$done=True;
} else {
...later in code ...
$this->view->setVar("done", $done);
or even just
$this->view->setVar("done", $savedSuccessfully);
I'm working on a CodeIgniter system with the Grocery_CRUD extension. I am trying to integrate a login system with a CRUD application.
Currently I am getting this error when the user logs in, where the CRUD app should be displayed:
Severity: Notice
Message: Undefined variable: output
Filename: views/members_area.php
Line Number: 13
This is the line the error is occuring on: <?php echo $output; ?>
I realise that $output is not being set but I can't see why!! Any ideas?
This is my entire members_area.php file: View
<?php
echo '<span class="italic">'.date("l, F d, Y " ,time()).'</span>';
echo '<h3>Welcome: '.$this->session->userdata('username').' '. anchor('logout/signout', 'logout').'</h3>';
?>
<a href='<?php echo site_url('site/dishes_management')?>'>Dishes</a> |
<a href='<?php echo site_url('site/orders_management')?>'>Orders</a>
<?php echo $output; ?>
This is my site.php file: Controller
<?php
class Site extends CI_Controller{
function __construct(){
parent::__construct();
$this->is_logged_in();
$this->load->database();
$this->load->helper('url');
$this->load->library('grocery_CRUD');
}
function members_area($output = null){
$this->load->view('members_area', $output);
}
function is_logged_in(){
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true){
echo 'You need to login to access this page. Login';
die();
}
function dishes_management()
{
try{
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->set_table('dishes');
$crud->set_subject('Dish');
$crud->required_fields('dish_name');
$crud->columns('dish_name','dish_desc','dish_price', 'dish_cat');
$output = $crud->render();
$this->members_area($output);
}catch(Exception $e){
show_error($e->getMessage().' --- '.$e->getTraceAsString());
}
}
function orders_management()
{
$crud = new grocery_CRUD();
$crud->set_relation('customerNumber','customers','{contactLastName} {contactFirstName}');
$crud->display_as('customerNumber','Customer');
$crud->set_table('orders');
$crud->set_subject('Order');
$crud->unset_add();
$crud->unset_delete();
$output = $crud->render();
$this->members_area($output);
}
}
}
?>
By default I would like the members_area.php to have output set to display items from the dishes table. This does not seem to be working.
Any help, as always is much appreciated!
have you tried enclosing the output inside an array ?
$this->load->view('members_area', array('output'=>$output));
or
$output['output'] = $output;
$this->load->view('members_area', $output);
You must send it on the views as two dimensional array, codeigniter is trying to find an output key from the given data but cannot find it since you have not supplied the proper data for the view to search from.
I m receiving this error.
I don't where should I make the changes to come over this error.
function comment()
{
global $option;
$row=& JTable::getInstance('comment','Table');
if (!$row->bind(JRequest::get('post')))
{
echo "<script>alert('".$row->getError()."');
window.history.go(-1);
</script>\n";
exit();
}
$row->comment_date=date('Y-m-d H:i:s');
$user=& JFactory::getUser();
if ($user->_table_id)
{
$row->user_id=$user->_table_id;
}
if (!$row->store())
{
echo "<script>alert('".$row->getError()."');
window.history.go(-1);</script>\n";
exit();
}
$this->setRedirect('index.php?option='.$option.'&id'.$row->review_id.'&view=review','Comment Added.');
}
thanks in advance
Dave
Obviously it cannot instantiate the JRequest::get('post') object. Probably this static function is returning NULL.
Try the following:
$testObj = JRequest::get('post');
var_dump($testObj);
See what you'll get.