How to redirect URL in CodeIgniter after form submission back to the previous page only when the user comes from that page?

Anish Sana

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!

Vinie

$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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to redirect URL in CodeIgniter after form submission back to the previous page only when the user comes from that page?

From Dev

How to redirect page on form submission

From Dev

How to get the previous page url after redirect

From Dev

How to redirect user to previous page when Logout?

From Dev

How to redirect to new html page after form submission in Apps script

From Dev

php form: take user back to previous page from register form

From Dev

How to redirect user back to the previous page using passport-spotify

From Dev

how to redirect the user to Page 404 from URL

From Dev

How to redirect the user back to the same page after authentication?

From Dev

PHP get previous page url after redirect

From Dev

Laravel 4 - Redirect back to the same page where the request comes from

From Dev

Trigger a redirect to a page on form submission after three incorrect attempts in Javascript

From Dev

Redirect Url After Form Submission

From Dev

Redirect back to previous page in PHP

From Dev

PHP - Redirect back to previous page, without any form

From Dev

Rails: redirect back to previous page after sign in / sign up

From Dev

Redirect a page after previous page-redirect

From Dev

redirect to previous page after login ion_auth codeigniter

From Dev

Ruby on Rails : No response from form submission after the page is link_to and works fine when it is directly loaded or refreshed

From Java

Redirect and refresh page on form submission in React frontend

From Dev

Form submission to api and then redirect to a specifi page

From Dev

Redirect user to previous page after auth (yii2)

From Dev

Prevent user from accessing previous page using back button after logout

From Dev

Redirect User back to Page on Login

From Dev

Redirect User back to Page on Login

From Dev

how to redirect user to another page and then back, while persisting form input values

From Dev

How to redirect user to previous page after login in Angular Js and IonicFramework project

From Dev

Get the URL of the previous page even after history.back() event

From Dev

How to block / prevent user to go back to previous page after logout in ASP.NET

Related Related

  1. 1

    How to redirect URL in CodeIgniter after form submission back to the previous page only when the user comes from that page?

  2. 2

    How to redirect page on form submission

  3. 3

    How to get the previous page url after redirect

  4. 4

    How to redirect user to previous page when Logout?

  5. 5

    How to redirect to new html page after form submission in Apps script

  6. 6

    php form: take user back to previous page from register form

  7. 7

    How to redirect user back to the previous page using passport-spotify

  8. 8

    how to redirect the user to Page 404 from URL

  9. 9

    How to redirect the user back to the same page after authentication?

  10. 10

    PHP get previous page url after redirect

  11. 11

    Laravel 4 - Redirect back to the same page where the request comes from

  12. 12

    Trigger a redirect to a page on form submission after three incorrect attempts in Javascript

  13. 13

    Redirect Url After Form Submission

  14. 14

    Redirect back to previous page in PHP

  15. 15

    PHP - Redirect back to previous page, without any form

  16. 16

    Rails: redirect back to previous page after sign in / sign up

  17. 17

    Redirect a page after previous page-redirect

  18. 18

    redirect to previous page after login ion_auth codeigniter

  19. 19

    Ruby on Rails : No response from form submission after the page is link_to and works fine when it is directly loaded or refreshed

  20. 20

    Redirect and refresh page on form submission in React frontend

  21. 21

    Form submission to api and then redirect to a specifi page

  22. 22

    Redirect user to previous page after auth (yii2)

  23. 23

    Prevent user from accessing previous page using back button after logout

  24. 24

    Redirect User back to Page on Login

  25. 25

    Redirect User back to Page on Login

  26. 26

    how to redirect user to another page and then back, while persisting form input values

  27. 27

    How to redirect user to previous page after login in Angular Js and IonicFramework project

  28. 28

    Get the URL of the previous page even after history.back() event

  29. 29

    How to block / prevent user to go back to previous page after logout in ASP.NET

HotTag

Archive