7

the following code gives me an error. I want to call a function and pass a parameter to it with php codeigniter.

redirect(base_url() . 'MainController/Student_Login($user_email)')

here MainController is the Controller name, Student_Login is the function $user_email is a variable that holds the user email id. I have also tried sending it through url. e.g.

redirect(base_url() . 'MainController/Student_Login/.$user_email.')

Please Help.

Ajmal Razeel
  • 1,663
  • 7
  • 27
  • 51
  • 1
    Possible duplicate of [Sending data along with a redirect in CodeIgniter](http://stackoverflow.com/questions/1837467/sending-data-along-with-a-redirect-in-codeigniter) – Alex Andrei Jun 30 '16 at 11:21

5 Answers5

6

There is no need of adding base_url with redirect.

Just try

redirect('mainController/Student_Login/'.$user_email)

when user_email you will receive as argument in student_Login funciton

Raj Jagani
  • 738
  • 1
  • 6
  • 21
  • What should I do if I have to call a function of another Controller and pass the $user_email variable to it? – Ajmal Razeel Jun 30 '16 at 11:31
  • same way `redirect('CONTROLLER_NAME/FUNCTION_NAME/'.$email)` – Raj Jagani Jun 30 '16 at 11:34
  • It gives me error: The URI you submitted has disallowed characters. – Ajmal Razeel Jun 30 '16 at 11:38
  • It's because codeigniter won't allow a character like `@` and some other characters in URL so it will throw error like that. If you want to allow than go to your `application/config/config.php` and change `$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';` to `$config['permitted_uri_chars'] = " "` If still it won't work you can check the answer [here](http://stackoverflow.com/questions/9334700/not-allowing-symbol-in-codeigniter-url) – Raj Jagani Jun 30 '16 at 11:43
  • Is there anyway to pass it by parameter like: redirect(base_url() . 'Student_Controller/Student_Login('.$user_email.')'); – Ajmal Razeel Jun 30 '16 at 11:55
4

You can pass it over session. If you want it available next request only, put it in session flash data.

First controller

public function method1()
{
    // if not loaded session library in APPPATH . 'config/autoload.php' load it in here

    $this->session->set_flashdata('user_email', $user_email);

    redirect('secondcontroller/method2', 'refresh');
}

Second controller

public function method2()
{
    // if not loaded session library in APPPATH . 'config/autoload.php' load it in here

    $user_data = $this->session->flashdata('user_email');

    // when flash data is set, after this request it will be unset from $_SESSION array
}
Tpojka
  • 6,996
  • 2
  • 29
  • 39
2

Use it as with out base_url()

redirect('MainController/Student_Login/'.$user_email);

And in method you get it using

function Student_Login($user_email){
    echo $user_email;
    //...
}
Saty
  • 22,443
  • 7
  • 33
  • 51
0

Try this

redirect(base_url() . "MainController/Student_Login/" . $user_email);

or

$url = base_url() . "MainController/Student_Login/" . $user_email;
redirect($url);
Jatin Raikwar
  • 406
  • 5
  • 17
-1

Try this

 redirect(base_url()."MainController/Student_Login/".$user_email);
doğukan
  • 23,073
  • 13
  • 57
  • 69