CodeIgniter flashdata [flash:old:message] being displayed - codeigniter

I'm using CodeIgniter on OpenShift.
In my controller I'm using:
$this->session->set_flashdata('message', 'message X');
$this->load->view('viewpage');
In my view I'm using:
print_r ($this->session->userdata);
echo $this->session->flashdata('message');
Here are my observations:
first time through the controller/load view, I see nothing echoed with the
$this->session->flashdata('message');
I see this with the print_r:
[flash:new:message]=>message 1
second time through the controller/load view, I see "message 1" being echoed
I see this with the print_r:
[flash:old:message] =>message 1[flash:new:message]=>message 2
So what appears to be happening is that [flash:old:message] is being displayed instead of [flash:new:message]. If [flash:old:message] isn't set, then nothing is displayed.
Please help.
Cheers,
Mike

when you set a value in a flash data, you need to make a view refresh like:
controller.php
function do_somthing(){
$this->session->set_flashdata('index', 'text message');
redirect('controller/view', 'refresh');
}
controller/view.php
<div>
<?= (isset($this->session->flashdata('index'))) ? $this->session->flashdata('index') : ''?>
</div>

Flashdata is designed to be used moving from 1 page to another (redirects), you typically use it after a post, the return a success/failure message.
the reason for this:
[flash:old:message] =>message 1[flash:new:message]=>message 2
occurring is because flashdata is retained for 1 additional page load (so you can use $this->session->keep_flashdata() if required... as you are triggering flashdata by refreshing the page to generate these results its confusing things and not designed for use this way..
This really seems to be an issue occurring due to the way you are using flashdata than it displaying the incorrect data.
A working example of using flashdata is below (even without a redirect)
controller:
public function index()
{
if (!$this->input->post()) {
$this->load->view('playland/index');
}else{
if ($this->input->post('submit') == "submit") {
$data['firstname'] = $this->input->post('firstname');
$data['lastname'] = $this->input->post('lastname');
$this->session->set_flashdata('test', 'data posted');
$this->load->view('playland/retrieve', $data);
}
}
}
index view:
<html>
<body>
<?php print_r($this->session->userdata)?>
<form method="post" action="playland">
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname"><br>
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>
retrieve view:
<html>
<body>
<?php echo $this->session->flashdata('test') ?><br>
<p>
First Name:<br>
<?php echo isset($firstname) ? $firstname : '';?><br>
Last Name:<br>
<?php echo isset($lastname) ? $lastname : '';?><br>
</p>
Click to refresh the page
Return to original page
</body>
</html>

Related

form submit in codeigniter going to blank page

I am submitting form using form helpers and it's going to blank page and when I see the view source, the action in the view source is like this http://::1/ci1/login_validation I don't understand the ::1 in it shouldn't it be localhost:8080/? But if I use simple form tags like regular html it works fine?
<?php
echo form_open ('login_validation');
echo form_input('email');
echo form_password('password');
echo form_submit('login_submit', 'login');
echo form_close();
?>
View Source
<form action="http://::1/ci1/login_validation" method="post" accept-charset="utf-8">
<p>Email:<input type="text" name="email" value="" /></p>
<p>Password:<input type="password" name="password" value="" /></p>
<p><input type="submit" name="login_submit" value="login" /></p>
</form>
I would think it is a couple of thing like.
If your form action has http://::1/ then does not submit correct because base url is empty
$config['base_url'] = '';
Fill base url example:
$config['base_url'] = 'http://localhost/project_name/';
On codeigniter 3 version not best idea to leave it blank because you will run in to that issue.
Make sure your class and file names of controllers etc have first letter upper case.
Filename: Login_validation.php
Controller:
<?php
class Login_validation extends CI_Controller {
public function index() {
// form submit controller code.
}
}
View
<?php
echo form_open ('login_validation');
echo form_input('email');
echo form_password('password');
echo form_submit('login_submit', 'login');
echo form_close();
?>
This is a common mistake people for get to check.
Why http://::1/??
This mean your base_url() is empty.
Then how this http://::1/ comes??
When your project URL is empty, CI detect your project URL. So this http://::1/ knows your project path.
How to set base_url()??
In config/config.php set $config['base_url'] = ''; the project URL.
Keeping base_url() empty any harm??
When you in local its ok and fine. But when you host that just add your site URL to it.
$config['base_url'] = 'www.stackoverflow.com';

How Can I check if Page is single page of Custom Post Type in Wordpress

On the Homepage I have called the content of Custpm Post Type with Ajax with this code
jQuery(document).ready(function($){
$.ajaxSetup({cache:false});
$(".sector_item a").click(function(){
var post_link = $(this).attr("href");
$(".sector_item_content").html("<div class='load' style='position:relative;margin-top:50px;padding-bottom:50px;'><img src='<?php bloginfo("template_url"); ?>/images/ajax-loader.gif'></div>");
$(".sector_item_content").load(post_link);
return false;
});
});
But In the single Page of this same Post Type I need to have another design with content so how I can detect the single page. I tried with Wordpess is_single() function but it doesnt work it displayed anyway in ajax.How Can I fix this?
Here is my single template.php
<?php if(is_singular('sector' )){
echo 'Single page' ;
}else{
while (have_posts() ) : the_post(); ?>
<div class="container single_page_inner">
<div class="row clearfix">
<div class="col-md-10 column" style="float:none;margin:auto;">
<div class="sector_single_title">
<?php the_title();?>
</div>
<div class="single_sector_contentmain"> <?php the_content();?></div>
<div class="single_sector_more">
<img src="<?php bloginfo(template_url);?>/images/single_secormoreline.png"/>
<div class="single_sector_button">
<span class="single_sec_leftline"></span>
<span class="single_sec_rightline"></span>
Click Here To Read More
<div class="more_line"></div>
</div>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
If you want to be able to do different things when your single.php files is loaded from Ajax, you need to tell it somehow when it is getting an Ajax request.
One approach would be to add a parameter to your call, like this:
var post_link = $(this).attr("href") + '?isajax=1';
Then in your single.php file, test for this parameter:
<?php if(!empty($_GET['isajax'])){
// This is Ajax - display in Ajax format
} else {
// This is not Ajax - display in standard single.php format
}
use this:
is_singular('YOUR_POST_TYPE' );
CODEX

Cannot get the data in another page in CI

I have form and when i submit that data are passed to next page i can see that in url. but i when i get and print that it shows nothing , here i have attached my coding
<form action="addfilters/add"><input type="hidden" name="id" value=<?php echo $filters->id ?> > <input type="submit" value="Add New"></form>
my controller
<?php
class AddFilters extends Admin_Controller
{
function add(){
echo $this->input->post('id');
}
}
URL
http://localhost/code/index.php/admin/addfilters/add?id=1
but when i print ,it shows nothing , please help me
i have just used
<?php echo form_open_multipart('admin/addfilters/add'); ?>
top of the form. it works fine :-)

Ajax CodeIgniter form submit hide and show new view

I'm a newbie to ajax and trying to integrate it in to my CodeIgniter app. Basically, when the form is successful I would like the original template to be hidden and the new one shown? It currently displays both, how could I amend my code? Thanks in advance!
I have tried adding a redirect to my controller with no such luck :(
view:
$('#submit').click(function(){
$.post("<?php echo base_url(); ?>part-two",
$("form").serialize(),
function(result){
$("#error_message").html(result);
}, "html"
);
});
controller:
if($this->form_validation->run() == FALSE){
echo validation_errors();
} else {
echo "success";
$data['content'] = "part_two";
$this->load->view('template', $data);
}
OK, I think that I know what you want...
View html markup:
<div class="container">
<form id="form">
...
</form>
<div class="error_message">
</div>
</div>
Script:
$('#submit').click(function(){
$.post("<?php echo base_url(); ?>part-two",
$("form").serialize(),
function(result){
// clear the content and assing the result
$("#form").remove();
$("#error_message").html(result);
}, "html"
);
});
I cant help you so much. Would you please use firebug to see received data of post request ?
Since both are displaying so i think post request is okay. Problem is in your view file.

Suggest an AJAX solution to form submission without reloading page, with action="" instead of action="response.php"

I have a search page: (please excuse the bad syntax, just for demo purposes)
<form action="" method="post" name="form">
//form elements and such with a hidden input name="action" value="search"
</form>
<?php if(isset($_POST['action']) and $_POST['action'] == 'search'): ?>
<?php include results.php ?> //takes form data, build SQL query, puts results in array
<?php foreach($results as result) blah blah //for each result display it ?>
<?php endforeach; ?>
<?php endif; ?>
I've seen solutions where the action attribute is set to a php file. Unfortunately in my example it doesn't have one. Take this one: http://www.simonerodriguez.com/ajax-form-submit-example/
<form name="MyForm" action="response_ajax.php" method="post" onsubmit="xmlhttpPost('response_ajax.php, 'MyForm', 'MyResult', '<img src=\'pleasewait.gif\'>'); return false;">
It looks nice but unfortunately I don't have the first parameter required: reqsponse_ajax.php, mine is just blank.
The JS can be found here: http://www.simonerodriguez.com/wp-content/plugins/downloads-manager/upload/ajaxsbmt.js
If anyone can make modifications to the script or suggest a better solution, that'll be awesome, thanks.
it shouldn't matter at all what's in the action attribute when you're using AJAX. it's the onsubmit or the submit button's onclick attribute that starts the AJAX processing.
you do understand the basics of AJAX, right? instead of actually submitting the form, which would load a new page, you send an XMLHttpRequest to the server, POSTing the data, and wait for an answer back with onreadystatechange, at which point you dynamically update your page. there are literally hundreds if not thousands of examples of this on the web.

Resources