I've tried several ways to redirect the URL back to the page where the user came from after submitting a form or just refresh the form page to allow the user to make a new entry if the user came directly to it. So far, I've been unsuccessful. The following method is the closest I've got but I'm not sure what's going wrong -
The user clicks this link which takes them to the page where the form is -
<a target="_blank" class="sampleredirect" href="<?php echo base_url();?>sample/controller/add">Add User</a>
Upon clicking, the following jQuery script links the click to a function -
<script>
$('#sampleredirect').click(function(){
$.ajax({
type: "POST",
url: "sample/controller/redfunc",
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
</script>
The function gets the URL where the click was initiated -
public function redfunc(){
$referred_from = $this->session->set_userdata('referred_from',current_url());
return $referred_from;
}
Inside sample/controller/add
where the form logic is located, after the submission, I'm calling the function to redirect the user back to the previous URL -
public function adduser(){
//sample code
$this->redfunc();
redirect($referred_from, 'refresh');
}
This isn't working and I keep getting redirected to the default page that shows up if a URL can't be found. When I do vardump($referred_from)
after calling the function $this->redfunc();
, I'm getting NULL
.
I'm kinda lost here and would love some help with this. Thanks!
$referred_from
will return nothing. It is just setting a session. It will not return session value. Try below code
<script>
$('#sampleredirect').click(function(){
$.ajax({
type: "POST",
url: "sample/controller/redfunc",
data: {current_url:<?=current_url()?>}
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
public function redfunc(){
$this->session->set_userdata('referred_from',$this->input->post('current_url');
}
public function adduser(){
//sample code
$referred_from = $this->session->referred_from;
redirect($referred_from, 'refresh');
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments