[SOLVED] Probably simple: $_REQUEST['username'] Undefined index error |
[eluser]Guest[/eluser]
Hi guys, I'm having a bit of a problem. I usally use this code just to confirm that a user has sent variables to the current page (they are post not get) -- and the code works fine when the variables have been set, but when the page is loaded without the passed variables, i get this error: Quote:A PHP Error was encountered My code is: Code: $username = $_REQUEST['username']; I think it's because the $_request is null (which is right -- as the page has been deliberately loaded wrong), but instead of just leaving it unset for the php code to pick it up, codeigniter is displaying an error. I do not want to turn error handling off, i would just like to know the proper way an experianced coder would handle the above. Thanks so much! Regards,
[eluser]DieterStruik[/eluser]
In php you can suppress errors by adding a @ sign like: $array = array('green' => 'existing value'); echo @$array['red']; // won't display an error. More clean is checking for a existing key or variable with key_exists($key, $array) or isset($var)
[eluser]flaky[/eluser]
first of all use Code: $this->input->post('username'); if you want to check if the post has been set Code: if($this->input->post('username')){
[eluser]flaky[/eluser]
And if you are dealing with forms, consider using Form Validation class http://ellislab.com/codeigniter/user-gui...ation.html
[eluser]Guest[/eluser]
Thanks very much for your replies. DieterStruik -- i have now used your @ trick in my views -- that is great, as now i do not have to keep passing empty $data['vars'] to views that have variables, but somtimes do not need them :-) flaky -- i have rewriten everywhere that had $_REQUEST to use $this->input->post. Great tip :-) Thank you both for your help :-)
[eluser]kacyblack[/eluser]
[quote author="DieterStruik" date="1263562830"]In php you can suppress errors by adding a @ sign like: $array = array('green' => 'existing value'); echo @$array['red']; // won't display an error. More clean is checking for a existing key or variable with key_exists($key, $array) or isset($var)[/quote] Thanks DieterStruik, Your Code Advice helped me a lot, God Bless You!
[eluser]Narf[/eluser]
There's no point in using both error suppression (the @ operator, which is also considered a bad practice) and $this->input->post(). |
Welcome Guest, Not a member yet? Register Sign In |