$this->input->post() and $_POST is NULL in codeigniter - forms

I am getting a problem in codeigniter where i have a view file called view.php.
<?php echo form_open('');?>
<table class="table">
<thead>
<tr>
<th>Full Name</th>
<th>Username</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo form_input('fullname1',$row_data['fullname']);?> </td>
<td><?php echo form_input('username1',$row_data['username']);?></td>
<td><?php echo anchor("welcome/save/".$row_data['rid'],form_button('button',"Save"));?></td>
</tr>
</tbody
</table>
<?php echo form_close();?>
and here is my save method
public function save($rid){
$arr=array('fullname'=>$this->input->post('fullname1'),
'username'=>$this->input->post('username1')
);
var_dump($_POST);
var_dump($arr);
}
both array varable giving me NULL values while the textboxes have the values Please Help.... Thanks

you need to add the url to your form_open method. Try
echo form_open("your_url");
give the path of your save method instead of your_url.
Also check documentation here
The form_open method demands a target url.

In View
# Syntax <?php echo form_open('controllerName/MethodName');?>
<?php echo form_open('welcome/save');?>
// remove this
<td><?php echo anchor("welcome/save/".$row_data['rid'],form_button('button',"Save"));?></td>
// Add this
echo form_submit('mysubmit', 'Submit Post!');
In Controller
public function save(){
$fullname= $this->input->post('fullname1');
$username = $this->input->post('username1');
echo "MY name is ".$fullname." UserName Is : "$username;
}
Read Form Helper Codeigniter and form-validation-using-codeigniter example

Related

Clear the filled data of form in Phalcon Framework

I am new to phalcon and trying to learn this framework. I have created a simple form. Now, When I fill up the form, I am able to store the data in database. After submitting the form I want to be on the same page. Now the issue is, form is still have the filled data it is not clearing up. I have googled and found the clear() method to clear the form data but it seems that its not working. What to do?
Here is my code
//Controller
use Phalcon\Mvc\Controller;
class ProfileController extends Controller
{
public function indexAction()
{
// Add some local CSS resources
$this->assets->addCss("css/styles.css");
$this->assets->addCss("css/bootstrap.min.css");
// And some local JavaScript resources
$this->assets->addJs("js/jquery.js");
$this->assets->addJs("js/bootstrap.min.js");
}
public function createAction(){
//create object of model class
$profile = new Tbl_profile();
$profile->fname= $this->request->getPost('firstname');
$profile->lname= $this->request->getPost('lastname');
$profile->email= $this->request->getPost('email');
$success=$profile->save();
if($success) {
$this->flash->success("Profile created!");
//redirect to same page.
$this->dispatcher->forward(['action' => 'index']);
}else {
}
}
}
//views
<html>
<head>
<title>Title</title>
<?php $this->assets->outputCss(); ?>
</head>
<body>
<h4 class="ml-5 mt-2"> Create Profile</h4>
<?php echo $this->getContent(); ?>
<?php echo $this->tag->form("profile/create") ?>
<table class="table">
<tbody>
<tr scope="row">
<td>First Name</td>
<td><?php echo $this->tag->textField("firstname"); ?></td>
</tr>
<tr scope="row">
<td>Last Name</td>
<td><?php echo $this->tag->textField("lastname"); ?></td>
</tr>
<tr scope="row">
<td>Email</td>
<td><?php echo $this->tag->textField("email"); ?></td>
</tr>
<tr scope="row">
<td></td>
<td><?php echo $this->tag->submitButton("Create");?></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
Once you save the data to database, redirect to the controller instead of forwarding the request because forwarding the request won't reload the page.
$this->request->redirect('profile/index');
Read this what's the difference between redirect and dispatch in phalcon? for more information
You must create form class to have ability using $form->clear() method.
See the doc https://docs.phalconphp.com/cs/3.4/forms

Access an id of a user in a table from a controller

PHP, Zend Framework, Apache, MySql.
I want to edit a user in a list by clicking its corresponding edit button.
when i click the edit button, the corresponding users id should be send to the controller from where it should be accessed.
But I cant seem to get the user id in the controller.
After getting the id , i want to populate the fields in edit.phtml with the data retrieved view model.
Since i cant access the id, i cant populate the fields.
The url is like /Sample/user/edit/2 where 2 is id of a user.
UserController.php
<?php
class UserController extends Zend_Controller_Action
{
protected $_user;
public function init()
{
/* Initialize action controller here */
$this->_user = new Application_Model_User();
}
public function indexAction()
{
// action body
}
public function listAllAction()
{
// action body
$this->view->users = $this->_user->listUsers();
}
public function registerAction()
{
// action body
if($this->getRequest()->isPost())
{
$data = array(
'user_uname' => $this->_request->getParam('uname'),
'user_pwd' => $this->_request->getParam('paswd'),
'user_address' => $this->_request->getParam('address')
);
$this->_user->insert($data);
}
}
public function editAction()
{
// action body
**$u_id = $this->_request->getParam('user_id');**
// print_R("Hi ".$u_id);
// exit;
if($this->_request->isPost())
{
$u_id = $this->_request->getPost('user_id');
//print_R("Hi ".$u_id);
//exit;
}
else
{
**$this->view->user = $this->_user->getUser($u_id);**
}
}
}
Model class
<?php
class Application_Model_User extends Zend_Db_Table_Abstract
{
protected $_name="tbl_user";
public function listUsers()
{
// action body
$sql = "select * from tbl_user";
$result = $this->_db->query($sql);
return $result->fetchAll();
}
public function getUser($id)
{
$query = "select * from tbl_user where user_id = ?";
return $this->_db->fetchRow($query,array($id));
}
}
ListUser.phtml
<html>
<head></head>
<body>
<b><center>List of Users</center></b>
<form name="list_users" method="post" action="">
<table>
<tr><th>ID</th>
<th>Name</th>
<th>Password</th>
<th>Address</th>
<th>Action</th>
</tr>
<?php foreach ($this->users as $usr): ?>
<tr>
<td><?php echo $usr['user_id'] ?></td>
<td><?php echo $usr['user_uname'] ?></td>
<td><?php echo $usr['user_pwd'] ?></td>
<td><?php echo $usr['user_address'] ?></td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php endforeach; ?>
<tr>
<td colspan=2>Add More Users</td>
</tr>
</table>
</form>
</body>
</html>
edit.phtml
<html>
<head></head>
<body>
<form name="user_edit" method="post" action="<?php print $this->baseUrl(); ?>/user/edit">
<b><center>Edit Profile</center></b>
<table>
<tr>
<td>Username</td>
<td><input type="text" name="uname" id="uname1" value="<?php print $this->user['user_uname'] ?>"/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="paswd" id="paswd1"/></td>
</tr>
<tr>
<td>Address</td>
<td><textarea type="text" name="address" id="address1"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type='submit' name='edit_user' value='Update User'/></td>
</tr>
<tr>
<td colspan=2>See All Users</td>
</tr>
</table>
</body>
</html>
Thank you in advance..
I got the answer.
In usercontroller's editaction change the code $u_id = $this->_request->getParam('user_id'); to $u_id = $this->getRequest()->getParam('id');
Small change needed: change the following line:
<td>Edit</td>
to
<td>Edit</td>

Is it possible to profile Zend_Table queries?

I'm looking for a way to profile queries which are performed internally by Zend_Table.
For example, after you finish Quickstart course, how to profile all the queries that are performed ?
I've tried to enable profiler from application.ini like this:
resources.db.profiler.class = "Zend_Db_Profiler_Firebug"
resources.db.profiler.enabled = true
And placed next rows in a Guestbook controller:
...
$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$profiler = $db->getProfiler();
echo $profiler->getTotalElapsedSecs();
Which gives me 0
I've also tried to enable profiler in a Bootstrap file like this:
protected function _initProfiler() {
$this->bootstrap("db");
$profiler = new Zend_Db_Profiler_Firebug("All DB Queries");
$profiler->setEnabled(true);
Zend_Registry::get("db")->setProfiler($profiler);
}
Whick doesn't give me any result (I've installed and tested Firebug and FirePHP, using Zend_Log_Writer_Firebug())
I will appreciate any help. Thanks !
I've not tried the Firebug profiler. But I do use a html table to output profiler info at the bottom of each page that does a query.
enable profiler in application.ini
resources.db.params.profiler = true
render the profile results in the layout:
<?php
$this->addScriptPath(APPLICATION_PATH . '/views/scripts');
echo $this->render('profiler.phtml')
?>
profiler.phtml the view to render
<?php
// get the default db adapter
$adapter = Zend_Db_Table::getDefaultAdapter();
$profiler = $adapter->getProfiler();
if ($profiler->getEnabled() && $profiler->getTotalNumQueries() > 0) :
?>
<div style='text-align:center'>
<h2>Database Profiling Report</h2>
<p>Total queries executed: <?php echo $profiler->getTotalNumQueries() ?></p>
<p>Total elapsed time: <?php echo $profiler->getTotalElapsedSecs() ?></p>
</div>
<table class='spreadsheet' cellpadding='0' cellspacing='0' style='margin:10px auto'>
<thead>
<tr>
<th>#</th>
<th>Query</th>
<th>Time</th>
</tr>
</thead>
<tbody>
<?php foreach ($profiler->getQueryProfiles() as $queryNumber => $query) : ?>
<tr>
<td>(<?php echo $queryNumber + 1 ?>)</td>
<td><?php echo $query->getQuery(); ?></td>
<td><?php echo $query->getElapsedSecs(); ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<?php endif ?>
I don't imagine that using the firebug profiler is much more difficult.
The problem was in the parameters instantiation. When I've entered
resources.db.params.profiler.class = "Zend_Db_Profiler_Firebug"
resources.db.params.profiler.enabled = true
instead of previous configuration, everything went fine.
Note .params section of the parameter lines

Zend_Form_Element_File (no decorator found error)

I am creating a form with an upload element. I have created the file element with the code below
$image = new Zend_Form_Element_File('image');
$image->setAttrib('title','Listing Image:')
->addDecorator('File')
->addValidator('Size', false, 204800)
->addValidator('Extension', false, 'jpg,png,gif');
$this->addElement($image);
Which as far as I can tell works fine. It tries to add a file element to the page. I get an error which says " Warning: No file decorator found... unable to render file element in..."
As you can see I have set the decorator for the element to be the File decorator so I am unsure why I am getting this error?
It is worth noting I have removed all default decorators except for viewHelper and PrepareElements and I am using a view partial as the viewHelper to display the output. Just incase this is required I have also included that below.
<form method="<?php echo $this->element->getMethod(); ?>" action="<?php echo $this->element->getAction(); ?>">
<div class="boxmiddle-Holder">
<div class="boxmiddle-content">
<table border="0" cellspacing="2" cellpadding="3" class="dataTable">
<?php foreach($this->element->getElements() as $element): ?>
<!-- if not a button then display label -->
<tr>
<td><label for="<?php echo $element->getName(); ?>"><?php echo $element->getAttrib('title'); ?></label></td>
<td class="normal"><?php echo $element; ?></td>
<?php if($element->getMessages()) {
foreach($element->getMessages() as $message) { ?>
<td> <?php echo "<p class='elementError'>".$message."</p>"; ?></td>
<?php }
}
?>
</tr>
<?php endforeach; ?>
</table>
</div>
</div>
Any ideas?
I'd double check to make sure the File decorator isn't being cleared or reset later, that error indicates that your form's file element does not have a Zend_Form_Decorator_File decorator attached to it.
Look in Zend/Form/Element/File.php around line 859 for the function that throws this error. I suspect somehow the File decorator is being removed from the element. It could be that it has no decorators or is just lacking the File decorator.
Hope that helps.

Zend_Form : data table with checkboxes

I'd like to have a table of data coming from the DB in my form element, looking like the following :
+-----+-------------------------+-----------------------+
| | Number | Name |
+-----+-------------------------+-----------------------+
| [ ] | 123 | ABC |
+-----+-------------------------+-----------------------+
| [x] | 456 | DEF |
+-----+-------------------------+-----------------------+
| [x] | 789 | HIJ |
+-----+-------------------------+-----------------------+
It would allow to select several rows, like the MultiCheckBox element.
Here is the kind of markup I would like to have:
<table>
<thead>
<tr>
<th>Select</th>
<th>Number</th>
<th>Name</th>
</tr>
</thead>
<tr>
<td><input type="checkbox" name="subscribers[]" value="1234"></td>
<td>1234</td>
<td>ABC</td>
</tr>
<tr>
<td><input type="checkbox" name="subscribers[]" value="375950"></td>
<td>375950</td>
<td>DEF</td>
</tr>
<!-- and so on... -->
I can do it by hand but using Zend_Form would allow me to populate the form, retrieve the values easily and to have validation. I have other normal elements in my form.
Any idea on how to achieve this with Zend_Form ? Maybe a custom element and decorator ?
Thanks. Ask for more info if needed.
This question seems to be related: Zend_Form: Database records in HTML table with checkboxes
Marc
ok, so this is going to be a longer sort of answer
the Form
<?php
class Form_MyTest extends Zend_Form
{
public function init()
{
$element = $this->createElement('multiCheckbox', 'subscribers');
$element->setOptions(array('value1' => 'label1', 'value2' => 'label2'));
$this->addElement($element);
// ... other elements
}
}
Controller
<?php
class MyController extends Zend_Controller_Action
{
public function myTestAction()
{
$form = new Form_MyTest();
// ... processing logics
$this->view->assign('form', $form);
}
}
View
<form action="<?php echo $this->form->getAction(); ?>" method="<?php echo $this->form->getMethod(); ?>">
<table>
<thead>
<tr>
<th>Select</th>
<th>Number</th>
<th>Name</th>
</tr>
</thead>
<?php $values = $this->form->getElement('subscribers')->getValue(); ?>
<?php foreach($this->form->getElement('subscribers')->getMultiOptions() as $key => $value) : ?>
<tr>
<td><input type="checkbox" name="subscribers[]" id="subscribers-<?php echo $key; ?>" value="<?php echo $key; ?>" <?php echo in_array($key, $values) ? 'checked="checked"':''; ?>/></td>
<td><label for="subscribers-<?php echo $key; ?>"><?php echo $key; ?></label></td>
<td><label for="subscribers-<?php echo $key; ?>"><?php echo $value; ?></label></td>
</tr>
<?php endforeach; ?>
</table>
<!-- rest of form -->
</form>
A couple things are happening here.
I get the prepopulated values out of the form object:
<?php $values = $this->form->getElement('subscribers')->getValue(); ?>
I mark each checkbox as checked or not based on the array above
<?php echo in_array($key, $values) ? 'checked="checked"':''; ?>
EDIT IN RESPONSE TO COMMENT B/C COMMENTS DON'T SUPPORT PRE BLOCKS
the
$element->setOptions(
or
$element->setMultiOptions(
only accepts key => value pairs, so anything you want to do outside of key value pairs is going to be a little wierd. If your program allows you could pass another variable to the view, an array that uses the same keys as the multiCheckbox so
$this->view->assign('more_datums', array('value1' => array('col_1' => 'col_1_val'[, ...])));
and then in the foreach in the view use
$this->more_datums[$key]['col_1']