How to display error messages in CodeIgniter - 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.

Related

Send back value from controller when submit form 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.

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; ?>

How to use SSL/https with non-menu items?

We have a site that needs to have several sections be secure. We have
our SSL certificate installed, and for the areas that are accessible
via menu item, it's no problem - we just use the SSL Enabled system
parameter in the menu item editor. But we have a few sections (i.e. a
shopping cart checkout screen) that are only accessible via a submit
button (they don't have their own URL, so to speak - they're just
submitted to themselves via the controller and the view changes based
on the form action.) Right now, the form action is set like this:
<form name="instantForm" action="/<?=$this->segment?>/" method="post" onsubmit="updateSubmitValue()">
where segment is passed via the view.html.php. The rendered form tag
looks like this:
<form id = "checkoutForm" name="checkoutForm" action="/checkout/" method="post" onsubmit="updateSubmit()">
When submitted, the controller grabs the value of a few submitted
fields and determines which view to display (logged in with saved
account info or anonymous transaction) and then displays the correct
form.
Here's a stripped-down version of the controller's display method:
if (JRequest::getVar('checkoutCodeSubmitBTN') != ""){
//user has clicked Checkout button; go to billing info page
JRequest::setVar('view','checkoutpay');
// JRequest::setVar('view','checkout_thankyou');
//reference view
$viewCode =& $this->getView('checkoutpay','html');
$viewCode->voucher =& $voucher;
} //close test for step 1 if
How can I make sure that the view that gets displayed gets switched
over to an https URL?
I've already posted this on the google joomla dev discussion group, and got a response telling me to use JRoute to generate a URL and use setRedirect instead of posting to the form, but then someone else responded that using JRoute produces a completely new request, so all your access to JRequest::getVar type stuff is gone. We need to be able to access the variables that are posted through the form, so that solution is out. Does anyone have any other ways of doing this? I'm pretty new to Joomla development and am not familiar with many of the objects and methods available.
I've heard from some people that JRoute would be better for this, but that only works if you know the URL you need; we have to build our URL dynamically based on the current request, so I used JURI.
In my view.html.php, I added this code:
$needSecure = $model->needSecure();
if($needSecure) {
$u =& JURI::getInstance( JURI::base() );
$u->setScheme( 'https' );
$tmpURL = $u->toString()."checkout";
}
else {
$tmpURL = "/checkout";
}
$this->assignRef("tmpURL", $tmpURL);
needSecure() is a function in my model that pulls a value from a database table and returns a boolean. So if needSecure returns true, we get the current request URI, set the first part to https, then append the bit that we're submitting to. If it returns false, we just set the bit to submit to.
In the default.php, we have this:
<form id = "checkoutForm" name="checkoutForm" action="<?=$this->tmpURL?>/" method="post" onsubmit="updateSubmit()">
If needSecure is true, the action renders to
<form id = "checkoutForm" name="checkoutForm" action="https://www.mysite.com/checkout" method="post" onsubmit="updateSubmit()">
otherwise it renders to
<form id = "checkoutForm" name="checkoutForm" action="/checkout" method="post" onsubmit="updateSubmit()">
It works perfectly, and because we're storing the boolean in a database, it means we don't ever have to change the code itself if we want to make a new form submission secure or insecure.

CI: Use view/controller in another included view?

I'm new to CI & PHP.
I have an auth library included, and works great stand-alone.
I simply want to have the login form load as a view inside another view...is that weird?:
I'm quasi-templating:
index:
$this->load->view('head_content');
$this->load->view('stuff');
$this->load->view('footer');
Inside the stuff view:
<stuff></>
$this->load->view('login_view');
<morestuff></>
I just want the login form to show up on the front page, and then tie into the auth system...
You have to load the login view in the controller, and then pass the data to the stuff view.
In the controller:
$this->load->view('head_content');
// the line below will save the output of the login view to $data['login']
// instead of outputting to the screen
$data['login'] = $this->load->view('login_view', '', TRUE);
$this->load->view('stuff');
$this->load->view('footer');
In the stuff view:
<stuff>
<?php echo $login; ?>
<morestuff>
In the page Views, at the bottom, check out the section Returning views as data
I've just recommended this but I'm going to recommend it again:
http://codeigniter.com/forums/viewthread/77279/
Django-like template inheritance helper for CodeIgniter.
I use this on ALL of my CI projects and it makes things like this stupidly easy.

Resources