how to get value of input in view to controller in codeigniter - php

I am quite new to CodeIgniter. In my application I have page where user enters his details. I want to get that detail in a variable (Lets say just Name ) and want to use that variable in one of the controller file.
How to do that ?

for a form value called "supername"
to use locally inside a method
$supername = $this->input->post( 'supername', TRUE ) ;
to use in any method in the class use $this->
$this->supername = $this->input->post( 'supername', TRUE ) ;

In your controller
$name= $this->input->post('name');
Better start off with some tutorials on Codeigniter.

From http://ellislab.com/codeigniter/user-guide/libraries/input.html :
$post = $this->input->post();
//on the controller you can access this and will return the array
//of the post data you submitted
You can also get them by variable name like:
$somedata = $this->input->post('some_data');
Make sure your form action is pointing to your controller as well. :)

CodeIgniter has this Input class wherein simplifies the inputs when they are used in some conditional statements.
To answer your question, on how to fetch the data entered by the user is by:
$name = $this->input->post('name');
Stores the value of name (it is the name of your field in the form) to a variable name $name.
Do you want to know if the field is empty or not?
try:
if($name = $this->input->post('name')){
//$name has the value of the name field and it is not empty, do something.
}else{
//$name has the value of the name field and it is empty, do something.
}
CodeIgniter Input class also has XSS Filtering capabilities, it filters input automatically to prevent cross-site scripting attacks.
All you have to do is:
Set it to TRUE in your config/config.php
$config['global_xss_filtering'] = TRUE;

Related

How can I get the form data from the FOSUserBundle registration form?

I am using the FOSUserBundle and have overwritten the RegistrationController. When the form is submitted and valid, I want to get the email address the user entered in the registration form.
But I don't see any way to get it. As taken from the Symfony2 forms documentation, you can get form data like this:
$this->get('request')->request->get('name');
But the RegistrationController does not know the get() method (as it's not inherited from the Symfony2 controller entity). So I could go like this:
// Note the ...->container->...
$this->container->get('request')->request->get('name');
But this returns NULL. Now I try to get it from the $form.
// Does contain a lot of stuff, but not the entered email address
$form->get('email');
// Does also contain a lot of stuff, but not the desired content
$request->get('email');
$request->request('email');
// Throws error message: No method getData()
$request->getData();
Any idea?
It's really, really simple. You create a form with related entity. In FOSUserBundle you should have a RegistrationFormHandler, and in process method you've got:
$user = $this->createUser();
$this->form->setData($user);
if ('POST' === $this->request->getMethod()) {
$this->form->bind($this->request);
if ($this->form->isValid()) /**(...)**/
After the line $this->form->bind($this->request) every value in $user object is overwritten by data from form. So you can use $user->getEmail().
On the other hand you are able to get data directly from request, but not by the property name, but by form name. In FOSUserBundle registration form it is called fos_user_registration - you can find it in FOS/UserBundle/Form/Type/RegistrationFormType.php in getName method.
You get it by:
$registrationArray = $request->get('fos_user_registration');
$email = $registrationArray['email'];
If you were to use Controller as a Service (which should work with this), you could pass the RequestStack (sf >=2.4) in the constructor and do $this->request_stack->getCurrentRequest()->get();
My guess is that you are trying to get the POST data. You are trying to put the data in a form object I presume. I would recommend you to take a look at: http://symfony.com/doc/current/book/forms.html if you have a custom form.
As regarding to your question, the form is probably containing a name. If you want to have direct access to it instead of doing things in your form, you need to fetch it multilevel if directly via the true at $deep, get('registration_form_name[email]', null, true); You can also do $email = $request->get('registration_form_name')['email']; (if you have php 5.4+)

form->isValid() empty my model

I have a big model with around 13 attributs.
I want to update a few value of them using a form.
But actually using $form->isValid() empty my model and set only value which are feeded in my form or have an inputFilter setted.
Are they a way to avoid it ?
Checkout Validation Groups
http://framework.zend.com/manual/2.0/en/modules/zend.form.quick-start.html#validation-groups
You can partially validate your model, and only get back the values in the Validation Group.
I think you have set filter for all the fields in form ,
check what you are getting with this .
$unfiltered = $form->getUnfilteredValues();
print_r($unfiltered);

Yii framework: passing variable through Chtml:button

I am trying to pass a variable from a view (of mobile model) to a different controller (of inventory model), using the chtml:button with this code
echo CHtml::button(
'Sell It',
array('submit' => array('inventory/create', array('id'=>$data->id)))
);
Now how do I access the $id variable in the Inventory controller, so that I can prepopulate the create view with details corresponding to the passed 'id' variable of the mobile model.
In your inventory/create controller action do checking before getting the id like this :-
if (isset($_REQUEST['id'])) {
$id = $_REQUEST['id'];
$this->render('create',array('model'=>$inventory, 'id'=>$id));
}
else{
$this->render('create',array('model'=>$inventory);
}
If you are trying to come up an update/edit form with the values prefilled based on the Id passed then you should have to go through the CRUD options available within YII.. This is much better way to handle record updation and its easy too . See this topic for furhter info..
http://www.yiiframework.com/doc/guide/1.1/en/quickstart.first-app
In your inventory/create controller action do a test for $_GET['id'] something like:
$id = (#$_GET['id']) ? : DEFAULT_VALUE;
$this->render('create',array('model'=>$inventory, 'id'=>$id));
and then you pass the data through to the view by passing an array of variables you want to make available.
(you would want to filter the input better, this is just a sample -- using filter_input or some other method and define a default value and/or some test for it being null/invalid)
in your controller you can get the variable by giving an argument to your controller method like this:
public function actionCreate($id){
$id = isset($id)?$id:NULL; // Or whatever, you can access it like this.
}
You don't have to use $_GET, and yii has already done some security checks on the value.

pass a post value from view to controller to model, and back to controller in code igniter

I'm creating a login form with Codeigniter, and I have a controller that collects the inputs from the form, then I want to check to make sure what the user entered is in the database, so I'm collecting those values in the post and want to send them to the model for the database connection. Then if the results are in the database I want to send something back to the controller with a yes or no and then I can go from there. I'm kind of stuck, but this is what I have so far:
The controller:
function do_login()
{
$login = $this->input->post('Login');
$pwd = md5($this->input->post('Passwd'));
}
The Model:
function check_login()
{
$sql = $this->db->query("SELECT * FROM members WHERE loin = '?' AND password = '?'", array(//POST stuff goes in here));
return $sql->result();
}
I'm not sure how to pass the data to the model, and then back to the controller.
Any help would be great!
Thanks!
In any MVC form POST is sending to controller (in action property in form) and controller (as the name is decribed) controls what will happend, in your case should ask database for verification via model, get response, decide what to do, and use view to display results...
so in your controller:
function do_login() {
$login = $this->input->post('Login');
$pwd = md5($this->input->post('Passwd'));
$results = $this->...your_model_name...->chek_login( parameters as login and password would help )
// base on results: has records or has not - do something
// maybe display view
}
+1 to #bensiu (I tried to up the answer but wasn't able to yet)
Also: don't forget that you need to explicitly load your model.
Reading this part of the user guide should answer most of your questions.
I would add 2 parameters to check_login and make it boolean:
function check_login($user, $password)
{
$sql = $this->db->query("SELECT * FROM members WHERE loin = '?' AND password = '?'", array($user, $password));
if (...)
return TRUE;
else
return FALSE;
}
Make it boolean lets the full login checking inside the controller, so that if there are more tests to do, the developper who uses the model doesn't have to know them, so it avoids errors.
For instance if you want to check that there is only one row in the database, and an internal variable to know whether login is allowed or not (just an example).
Would all developer read the documentation about that variable that has to be checked before validating the login? Would that documentation be written?

update/validate embedded forms before saving in symfony

I would like to validate an embedded form field before it gets saved in the database. Currently it will save an empty value into the database if the form field is empty. I'm allowing empty fields, but I want nothing inserted if the form field is empty.
Also quick question, how to alter field values before validating/saving an embedded form field?
$this->form->getObject works in the action, but $this->embeddedForm->getObject says object not found
I found a really easy solution. The solution is to override your Form's model class save() method manually.
class SomeUserClass extends myUser {
public function save(Doctrine_Connection $conn = null)
{
$this->setFirstName(trim($this->getFirstName()));
if($this->getFirstName())
{
return parent::save();
}else
{
return null;
}
}
}
In this example, I'm checking if the firstname field is blank. If its not, then save the form. If its empty, then we don't call save on the form.
I haven't been able to get the validators to work properly, and this is an equally clean solution.
Symfony gives you a bunch of hooks into the form/object saving process.
You can overwrite the blank values with null pre/post validation using by overriding the the doSave() or processValues() functions of the form.
You can read more about it here: http://www.symfony-project.org/more-with-symfony/1_4/en/06-Advanced-Forms#chapter_06_saving_object_forms

Categories