Send back value from controller when submit form codeigniter - codeigniter

here is a question that I am not able to find a way to do it. At the home view, I have a php section that will echo out if users input is just signed in when they submit form. But when I am doing this, it will give out a undefined error, because that controller is not yet executed.
This is my home view.
<?php include('header.php'); ?>
<?php echo $sign_in_results;?>
<?php include('forms/forms.php'); ?>
<?php include('footer.php'); ?>
This is my sign in controller, will get execute when submit pressed, the modal will return true or false.
function form_sign_in_controller(){
$this->load->model("form_sign_in");
$database_insert_results = $this->form_sign_in->check_user_detail_with_db();
$data['sign_in_results']= $database_insert_results;
$template = $this->load->View('main_view', $data);
}
I tried to use if(isset()).. The error is gone, but that section does not echo out anything. Any idea? Thanks

validate the data that comes from your sign in form. Never pass data from a form directly into a database query.
validating the data will tell you if the form has correct values. if it does not, then show the form again.
form data is validated - query your database
there are 3 possible results - you got a match OR you did not get a match OR there was an error. Check for these 3 different conditions, and then show the appropriate view.

Related

Magento - how to display email to a friend form in View phtml file

When you click 'Email to a Friend' you will be directed to a form to submit. I want to display this form in the product view page instead of 'Email to a friend' link. How it is possible to call send.phtml template in view.phtml?
I tried by giving this
<?php
echo $this->getLayout()->createBlock("core/template")->setTemplate("sendfriend/send.phtml")->toHtml();?>
But this didn't work,displayed an error. Please help
use this you were missing the block type
<?php
echo $this->getLayout()->createBlock("sendfriend/send")->setTemplate("sendfriend/send.phtml")->toHtml();?>

How to display error messages in CodeIgniter

In my controller I put this code to check if the shopping cart is empty:
if (!$this->cart->contents()){
$this->session->set_flashdata('message', 'Your cart is empty!');
}
$this->data['message'] = $this->session->flashdata('message');
$this->load->view('templates/header', $this->data);
$this->load->view('bookings', $this->data);
$this->load->view('templates/footer');
On my "bookings" view I set this code to display the message:
<div id="infoMessage"><?php echo $message;?></div>
But on the first page load the message "Your cart is empty!" is not showing. However, it will display when I press the F5 key or refresh the browser.
I have already read the manual from codeigniter that says "CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared."
However, I don't know where to set this message so it will be available on "next server requrest".
There is no need to pass the data to the view and assign it as flashdata.
The reason that it's not working the first time, is that flashdata is only available for the next server request. Loading the views isn't a server request. However, refreshing the page is.
There are a couple of solutions to this:
Pass the data directly and load the view. (Not using flash data.)
Use flashdata and redirect to the page.
Passing the data directly
You could just pass it directly (personally I'd go with this option - I'm not sure why you'd want to use flashdata in this case, I would've thought that you'd always like to inform the user if their cart is empty...):
Controller:
if (!$this->cart->contents())
{
$this->data['message'] = 'Your cart is empty!';
}
$this->load->view('templates/header', $this->data);
$this->load->view('bookings', $this->data);
$this->load->view('templates/footer');
View:
<div id="infoMessage"><?php echo $message;?></div>
Using flashdata
Or, if you want to use flashdata, then:
Controller:
if (!$this->cart->contents())
{
$this->session->set_flashdata('message', 'Your cart is empty!');
}
redirect('/your_page');
View:
<div id="infoMessage"><?php echo $this->session->flashdata('message');?></div>
If you want to use this method, you can't simply just load the view - due to the server request issue that I explained above - you have to redirect the user, using redirect('/your_page');, and the URL Helper $this->load->helper('url');. You can also send the appropriate HTTP header using the native PHP header function. Loading the view the first time won't work.
You can use this $this->session->keep_flashdata('message'); to preserve the data for an additional request.
I'd also check that your either auto-loading the session library or loading it in the constructor of your controller: $this->load->library('session'); (I'm fairly sure you will be, but it's worth checking anyway.)
Finally, I know that some people who store session data in their database can have issues with flashdata, so that may be worth looking into, if none of the above helps.
On a side note, personally I'd have a check to ensure that a variable is set before echoing it.

Yii - Ajax Form with validations

I am a Yii Newbie, and I have the following problem.
I have a form that is going to be like an admin - backend form. It will have loads of buttons each having their own "action" in the controller class. Now all I want to do is to validate the form elements, depending upon a "scenario" and to display appropriate error messages if all the parameters needed for the action is not filled in properly.
Can some one show me how I can do this without me having to reload the page?
[ I have found out a way, but I dont know if what I am doing is "technically" correct. I have submit buttons for all the actions that I want to perform in the form, and in the respective actions, I perform the validations and renderPartial form data back. OnSuccess of each button replaces the data of the entire "form-div" by the data that was retured from the controller. It works, but I want to know if this is the only way to achieve this.]
you should enable CActiveFom ajaxformvalidation property to true see the following example
in your controller action
you should uncomment the following lines
$this->performAjaxValidation($model);
in your view
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'test-form',
'enableAjaxValidation' => true,
));
?>

CodeIgniter - sending variable from controller to a view which use template

I have the following problems.
I am using the following template for my application:
<?php $this->load->view('backOffice/bo_header_in');?>
<?php $this->load->view($bo_main_content);?>
<?php $this->load->view('backOffice/bo_footer_in');?>
When I send data from the controller, to edit a record, I have the following code:
$data['userid'] = $this->back_office_users->getOneUser($userid);
$data['bo_main_content'] = "backOffice/edituser_view";
$this->load->view('backOffice/bo_template_in', $data);
Now, in my view, where I want to edit the record, I am getting it all right, my form is filled with the user name and password but.... on the top of the page, I get displayed the userid, username, userpassword, which is not what I want. Can anyone help me how can I send the data from my controller to my view, so I can have only my form filled, and not have the userid,username and password diplayed on the top of the page?
What you have shown looks ok. Search your models/controllers for a var_dump and re-look at the code in your view for a print_r. I typically do this during development to see the variable contents.

CodeIgniter Message System

I'm finding for a message passing system for codeIgniter. I found 3 ways.
1) set_flashdata(); from session class,
2) form_validaion from form validation class and
3) a variable set in controller and show it in view file.
1) is good, but we can only use for next server request. we can't use it for calling views.
2) can only use for form validation and it doesn't disappear on page refreshs.
3) is also doesn't disappear on page refreshs and we had to set it manually for form validation errors.
my want is like set_flashdata, show only one time and disappear on page refresh, doesn't need to set error message manually for form validations and able to use together with calling views.
It is not so bad if we use all three ways together, but I hope for better way.
Is there any ways or something for it?
Thanks!
Are you saying that you want to directly display flashdata in the view? Normally you just pass the flashdata to the view via the controller. I wouldn't recommend any other way.
Controller:
$data['var'] = $this->session->flashdata('var');
$this->load->view('view_file', $data);
View:
This is the flashdata: <?php echo $var; ?>

Resources