I am starting to learn PHP framework, particulary cakephp. I know how to insert into database, but I am curious about displaying the entries on the same page. I don't want to reload or redirect the page just to display the results.
I have only three four fields in the database. id, name, email, and the date. I have used the same validation in the model page.
Controllers:
<?php
function index(){
if($this->request->is('Post')){
$this->Save->save(data);
}
//display the entries from the database
$this->set('saves', $this->Save->find('all'));
}
?>
Index.ctp:
<?php
echo $this->Form->create('Save');
echo $this->Form->input('name', array('class'=>'name', 'required' => true));
echo $this->Form->input('email', array('class'=>'email', 'required' => true));
echo $this->Form->input('message', array( 'required' => true));
echo $this->Form->submit('submit', array('formnovalidate' => true));
//i want to display the entries here
foreach($saves as $row){
echo $row['Save']['name'];
echo $row['Save']['comment'];
}
?>
The problem is that , it affects the textarea. It reduces the size to half. Need help. thanks a lot. I am new to cakephp and I have been googling about it, but have not find the any results. Thanks
From what I understand it seems that your problem is more CSS issue than CakePHP issue. I don't think printing the data will cut the field into half.
Try put after echo $this->Form->submit('submit', array('formnovalidate' => true)); because the submit method does not include afterwards.
You can this $this->Form->end if you don't want to add the form closing tag manually.
Related
Im working on a piece of code to alter permissions for users of an app. Currently, each permission is added one at a time, Im revising it to be added in a faster way, being able to select multiple permissions to add at once.
The checkboxes aren't going to be the same number, since they are only the permissions the user doesn't yet have. It needs to pass an id with the permissions.
The problem is, my checkbox form doesn't appear to be sending any data to the controller. Upon submitting, it just loads a blank page with the url of the controller.
Heres the view code, that generates the checkbox form
<?php
if (!empty($lstAvailablePermissions)) {
$c=0;
echo $form->create('Administrator', array('action'=>'addPermission'));
echo $form->input('id');
foreach($lstAvailablePermissions as $key){
echo "<br>";
echo $form->input(
'permission',
array(
'id'=>$key,
'label'=>$key,
'type'=>'checkbox',
'multiple'=>'checkbox',
'value' => $key,
'name' =>'data[Administrator][permission]['.$c.']'
));
$c=$c+1;
}
echo $form->button(__('Add', true), array('type'=>'submit', 'class' => 'button', 'style'=>'padding: 2px; font-size: 12px;'));
echo $form->end();
}
?>
and the method in the controller
function AddPermission() {
if (empty($this->data)) { $this->RedirectWithFlash(__("Only POST Requests", true), "/administrators"); }
ErrorLogWarning("This is the form data sent to the controller", $this->data);
$length=count($this->data['Administrator']['permission']);
for ($i = 0; $i < $length; $i++){
$this->Acl->allow(
array('model' => 'Administrator', 'foreign_key' => $this->data['Administrator']['id']),
$this->data['Administrator']['permission'][$i]
);
}
$this->RedirectWithSuccessFlash(__("Permission added", true), array('action'=>'edit', $this->data['Administrator']['id']));
}
can anyone help me figure out whats going on? I assume ill need to loop through the data once its in the controller, but it doesn't even send the error message as of right now.
EDIT: problem appears to be fixed by adding the name property at the bottom of the form echo, which send it as an array which can be looped through in the controller.
I think the problem might be that you specify 'action'=>'addPermission' when you create the form, but your controller action is named AddPermission (capital A). Try changing to 'action'=>'AddPermission'.
Also, I think you might only get the last permission input because you're adding the permission inputs in a loop. Try indexing the inputs like this:
foreach($lstAvailablePermissions as $i => $key) {
echo "<br>";
echo $form->input(
"Administrator.{$i).permission",
I am new to CakePHP i tried to submit a form, which has an array input field.
Below is the code i used.
<?php echo $this->Form->checkbox('statusid.', array('value'=>$o['Order']['id'])); ?>
When i tried to submit the form i got a blackhole security error.
How can i fix it ?
I noticed that when i submitted the code below it works.
<?php echo $this->Form->checkbox('statusid', array('value'=>$o['Order']['id']));?>
Is there a way to make an array checkbox in CakePHP please guide me.
You can use the FormHelper and make a checkbox array named statusid this way.
<?php
echo $this->Form->create();
echo $this->Form->input('statusid.0', array('type' => 'checkbox', 'value' => $o['Order']['id']));
echo $this->Form->end();
?>
The issue with blackhole appears when 'hiddenField' is set to false. Although it's set to true by default, it's possible that OP has it defined somewhere globally for entire Form object.
<?php echo $this->Form->checkbox('statusid.', array(
'value' => $o['Order']['id'],
'hiddenField' => true)); ?>
so I've been trying to get select2Row to work for me forever and I just can't seem to get a hang of it. What I'm trying to do is provide the user with Tags that list a school/university name, while at the same time storing the value of said tag when I save the form. I've noticed that I cannot use both data and tags, else my drop down wont populate so that's not an option. Tags only seem to want text, rather than text and matching values. Any ideas?
<div class="row">
<?php
echo $form->select2Row($model, 'school_id', array(
'asDropDownList'=>false,
'options'=>array(
'tags'=>School::model()->schoolNames,
//'maximumSelectionSize'=>1,
'width'=>'297px',
'tokenSeparators'=>array(','),
),
));
?>
<?php echo $form->error($model,'school_id'); ?>
</div>
And here is the function for schoolNames
public function getSchoolNames()
{
$schools = array();
$result = $this->findAllBySQL("SELECT DISTINCT name FROM School");
foreach($result as $school){
$schools[] = $school->name;
}
return $schools;
}
I've tried using this instead, but the tags won't populate
public function getSchools()
{
$query = "SELECT id,name FROM School";
$results = $this->findAllBySQL($query);
return CHtml::listData($results, 'id', 'name');
}
So at the moment, select2Row is generating a list of tags using getSchoolNames() and that all works fine, except once you submit the form using those tags you'll get this
Please fix the following input errors:
School must be an integer.
Sorry for all the trouble, I'm still a little new to this. I'm sure the answer is probably right in front of me. Thanks either way =)
try this:
echo $form1->select2Row($model, 'school_id', array(
'data' => School::model()->schoolNames,
'multiple' => 'multiple',
'options' => array(
"width" => '70%',
),
));
Before adding admin prefixes/routing, everything was working fine...
Currently, I have a QuestionsController.php file with the following function:
public function admin_add() {
if ($this->request->is('post') ) {
$this->Question->create();
if ($this->Question->save($this->request->data)) {
$this->Session->setFlash('Your question has been saved.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to add your question.');
}
} else {
$this->Session->setFlash('Not post.');
}
}
Here is the contents of /views/Questions/admin_add.php:
<h2>Add a question</h2>
<?php
echo $this->Form->create('Question');
echo $this->Form->input('nickname');
echo $this->Form->input('content');
echo $this->Form->input('option1');
echo $this->Form->input('option2');
echo $this->Form->input('option3');
echo $this->Form->end('Save question');
echo $this->Html->link('Cancel', array('controller' => 'questions', 'action' => 'index'));
Notice the setFlash("Not post.") at the bottom of the controller? Every time I click the "Save question" button I see that message? Why?
UPDATE
We've determine that the request method is get, which explains why it's not working. But now the real question is why is it get. I'm pretty sure it was post before adding the admin prefix.
<?php echo $this->Form->create('Question', array( 'type' => 'POST' ) ); ?>
Try that :)
You can also add other options to that $options array, such as action, encoding, defaults, url, etc.
UPDATE
From your comments, I think you are telling us that the GET is determined from the controller. Examine your FORM in your source code to see if type="post" is there, or if it says type="get".
If it is posting, then you are being redirected on post, similar to a PRG pattern. This is where you are losing it. What URL do you ultimately end up on after POST'ing your form?
If it's hitting the second half of the if block, the the request isn't post.
To find out WHAT it is, just add this code just before the if block:
debug(CakeRequest::method());
(assuming your debug level is 2 for development mode)
Then, once you know what kind of request is happening, check against that.
I'm a little bit of a beginner to CakePHP and PHP in general, but I have OOP experience.
I'm trying to make a mini Twitter to get used to the Cake framework.
I have a PostsController class that handles all creating blog posts, editing deleting etc, but I'm having trouble adding and add post form to the same page above the View Posts.
i.e. adding posts works fine when I link to a new page
<p><?php echo $this->Html->link('Add Post', array('action' => 'add')); ?></p>
but while trying to put a form in the same page as the view I don't know how to call the 'add' action to save and use the data taken in in the form.
echo $this->Form->create('Post',array('action' => 'add'));
echo $this->Form->input('title');
echo $this->Form->input('body', array('rows' => '3'));
echo $this->Form->end('Save Post');
echo $this->Form->create('Post', array('action' => 'add'));
echo $this->Form->input('title');
echo $this->Form->input('body', array('rows' => '3'));
echo $this->Form->end('Save Post');
putting random arrays in your code will not do anything.
Missing semicolon after the array
Not assigning or using the array in anything.
Trying putting your array as the second parameter in the form create method.