I don't see anything wrong with my code. But it does not save data:
<?php
class ProductsController extends AppController{
var $name = 'Products';
//var $helpers = array('Form');
//var $scaffold;
function index(){
$this->Product->recursive = 1;
$products = $this->Product->find('all');
$this->set('products',$products);
//pr($products);
}
function add(){
$categories = $this->Product->Category->find('list',array(
'field'=>array('Category.categoryName')
));
$this->set('categories',$categories);
if(!empty($this->data)){
if($this->Product->save($this->data)){
$this->Session->setFlash('Saved');
}
}
}
}
?>
it flashes "Saved" but nothing is being inserted in my table. What could possibly be wrong when it should be functioning properly. :(
Below is my add.ctp model:
<h2>ADD</h2>
<?php echo $this->Form->create('Product',array('action'=>'add')); ?>
<?php
echo $form->input('ProductName');
echo $form->input('categories');
echo $form->end('DONE');
?>
You have to use the create() method before to save
function add(){
$categories = $this->Product->Category->find('list',array(
'field'=>array('Category.categoryName')
));
$this->set('categories',$categories);
if(!empty($this->data)){
$this->Product->create();
if($this->Product->save($this->data)){
$this->Session->setFlash('Saved');
}
}
}
Related
I'm currently making a newsletter that let users subscribe and unsubscribe. Subscribing is a success but not on unsubscribing. can you help me out guys, here's my codes...
Controller
class AddsController extends AppController {
public $helpers = array('Html','Form','Session','Flash');
public $components=array('Session');
public function index() {
if($this->request->is('post')) //posting to db
{
$this->Add->create();
$this->Add->save($this->request->data);
$this->Flash->set('The user has been saved.', array('element' => 'success'));
return $this->redirect('index'); //redirects to index.ctp
}
}
/*public function delete($email = null) {
if($this->request->is('post')) {
$this->Add->exists($email) {
$this->Add->delete($email);
$this->Session->Flash('Unsubscribed');
return $this->redirect('index');
}
}
function delete(){ //delete html
$email = $this->request->data['email'];
if($this->Add->exists($email)){
$this->Add->delete($email);
$this->Session->Flash('Unsubscribed');
//$this->Flash->set('Unsubscribed', array('element' => 'danger'));
return $this->redirect('index');
}
} */
function delete() {
$email = $this->request->data['email'];
if($this->Add->exists($email)){
$this->Add->delete($email);
$this->Session->Flash('Unsubscribed');
//$this->Flash->set('Unsubscribed', array('element' => 'danger'));
return $this->redirect('index');
}
}
}
View
<?php echo "<section>";
echo $this->Form->create('Add'); //<form > 'modelname'
echo "<label>E-mail:</label>";
echo $this->Form->input('email',array('label'=>false , 'placeholder'=>'your email')); //email input
echo $this->Form->submit('Subscribe'); //subscrib button
echo $this->Form->end();//</form>
echo "</section>";
echo "<section>";
echo $this->Form->delete('Add',array('action'=>'delete')); //<form >
echo "<label>E-mail:</label>";
echo $this->Form->input('email',array('label'=>false , 'placeholder'=>'your email')); //email input
echo $this->Form->submit('Unsubscribe',array('action'=>'index'),array('style'=>'background:red'));
echo $this->Form->end();//</form>
echo "</section>";
My view is just "entering data into db and deleting existed data out the db
Model
<?php
App::uses('AppModel' , 'Model');
class Add extends AppModel
{
public $name = "Add";
public $useTable = 'request';
public $primaryket = 'id';
public $useDbConfig = 'default';
}
db tablename: request; Just the delete row
Thank you in advance!!!
try to use below method (pass unique id of user) hope it will work:
$user = $this-> Add->findByEmail($email);
$this-> Add->delete($user['User']['id'], false);
Alternative way
$userId = $this->User->field('id', array('User.email' => $email));
if($userId) {
$this->User->id = $userId;
$this->User->delete();
$this->log('User of '.$email.' record deleted successfully');
} else {
$this->log('There is no record present of particular email = '. $email);
}
I have a this
**Fatal_Error:
Error: Unsupported operand types
File: C:\wamp\www\newsletter\lib\Cake\View\Helper\FlashHelper.php
Line: 90**
every time I clicked the submit button. here's my code..
**AddsController.php //Controller**
<?php
class AddsController extends AppController {
public $helpers = array('Html','Form','Session');
public $components=array('Session');
public function index() {
if($this->request->is('post'))
{
$this->Add->create();
$this->Add->save($this->request->data);
$this->Session->setFlash('Success');
return $this->redirect(array('action'=>'index'));
}
}
}
?>
**Add.php //Model/**
<?php
App::uses('AppModel' , 'Model');
class Add extends AppModel
{
public $name = "Add";
public $useTable = 'request';
public $primaryket = 'id';
public $useDbConfig = 'default';
}
?>
**index.ctp //View/Adds/index.ctp**
<?php
echo $this->Form->create('add');
echo $this->Form->input('email');
echo $this->Form->submit('submit');
echo $this->Form->end();
?>
dbname: exercise; table: request;
goal: all inputted data must be in the db.
Thank you in advance!
Use FlashHelper to set yor flash messages, not Session->flash
https://book.cakephp.org/2.0/en/core-libraries/helpers/flash.html
// In your Controller
public $helpers = array('Html','Form','Session','Flash');
$this->Flash->set('The user has been saved.', array(
'element' => 'success'
));
home.ctp
<?php
echo $this->element('distromob/featured');
?>
WebsitesController.php
<?php
class WebsitesController extends AppController {
public $components = array('Paginator');
public function index(){
$images = $this->paginate('Website');
if (isset($this->params['requested'])) {
return $images;
} else {
$this->set('images', $images);
}
}
featured.ctp
<?php
$images = $this->requestAction('/Websites/index');
?>
<ul>
<?php
foreach($images as $image): ?>
<?php $domain = $image['Website']['domain'];?>
<li><?php echo $this->Html->image('websites/' . $image['Website']['image'],array('width'=>'234px','height' =>'208px','class' => 'random'));
?>
</li>
<?php endforeach;?>
</ul>
<?php echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled')); ?>
<?php $this->Paginator->counter(); ?>
<?php echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled')); ?>
AppController.php
class AppController extends Controller {
public function beforeFilter(){
$this->Paginator->settings=array(
'limit'=>4
);
}
}
Im new to cakephp I found some tutorial on the web but it seems not fit on my needs. My question was, why is it the previous and the next pagination data is not clickable, It seems that the pagination data is base on the limit i set on
public function beforeFilter(){
$this->Paginator->settings=array(
'limit'=>4
);
}
whenever i change the limit it will also display data but i cannot click the next and the previous
make the pagination data available in the element i.e $this->params['paging']
//index method
if ($this->params['requested'])
return array('images'=>$this->paginate('WebSite'), 'paging' => $this->params['paging']);
$this->set('images', $this->paginate('WebSite') );
then in your home.ctp do this
$images = $this->requestAction(array('controller'=>'websites','action'=>'index'));
// if the 'paging' variable is populated, merge it with the already present paging variable in $this->params. This will make sure the PaginatorHelper works
if(!isset($this->params['paging'])) $this->params['paging'] = array();
$this->params['paging'] = Hash::merge( $this->params['paging'] , $images['paging'] );
Try replacing ->counter() with ->numbers() to see if you have any page numbers
I get the contents of my form to write to the database. My code is as follows:
View (index.ctp):
<div class="modal fade" id="test_modal">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h1>Create Customer</h1>
</div>
<div class="modal-body">
<?php
echo $this->Form->create('Contact');
echo $this->Form->input('type');
echo $this->Form->input('name');
echo $this->Form->input('company');
echo $this->Form->input('phone');
echo $this->Form->input('mobile');
echo $this->Form->input('email');
echo $this->Form->input('vatNumber');
echo $this->Form->input('mainAddressLine1');
echo $this->Form->input('mainAddressLine2');
echo $this->Form->input('mainAddressTown');
echo $this->Form->input('mainAddressCounty');
echo $this->Form->input('mainAddressPostCode');
echo $this->Form->input('mainAddressCountry');
echo $this->Form->input('notes', array('rows' => '5'));
echo $this->Form->end('Save Customer');
?>
</div>
</div>
Controller (ContactController.php)
class ContactsController extends AppController {
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');
public function index() {
$this->set('contacts', $this->Contact->find('all'));
}
public function view($id) {
if (!$id) {
throw new NotFoundException(__('Invalid contact'));
}
$contact = $this->Contact->findById($id);
if (!$contact) {
throw new NotFoundException(__('Invalid contact'));
}
$this->set('contact', $contact);
}
public function add() {
if ($this->request->is('post')) {
$this->Contact->create();
if ($this->Contact->save($this->request->data)) {
$this->Session->setFlash('Your contact has been saved.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to add your contact.');
}
}
}
}
Model (Contact.php)
class Contact extends AppModel {
}
Any help appreciated.
Regards,
Stephen
If your view file is index.ctp, that means that by default the logic thta is handled for it is in the index() method of your Controller. That method only sets a variable. I'm assuming you're mixing up the view/action. Try adding your form in the add.ctp view instead and call the contacts/add URI.
Alternatively you can override the view to be rendered by adding a $this->render call to your add() method:
public function add() {
$this->autoRender = false;
// Rest of your logic
$this->render('index');
}
That way the contacts/add URI will serve the index.ctp view instead of add.ctp.
My error code is:
Notice: Undefined variable: form in
c:\AppServ\www\applogic\app\views\users\index.ctp on line 1
Fatal error: Call to a member function create() on a non-object in
c:\AppServ\www\applogic\app\views\users\index.ctp on line 1)))
(index.ctp)
<?php echo $form->create(null, array('action' => 'index'));?>
<fieldset>
<legend>Enter Your Name</legend>
<?php echo $form->input('name'); ?>
</fieldset>
<?php echo $form->end('Go');?>
(users_controller.php)
<?php
class UsersController extends AppController {
var $name = 'Users';
var $uses = array();
function index() {
if (!empty($this->data)) {
//data posted
echo $this->data['name'];
$this->autoRender = false;
}
}
}
?>
Did you set the $helpers in app_controller or users_controller? You need to include 'Form' in it.
If you are using 2.0, I think you need to use $this->Html (not $html)