![]() |
Better than if(count($_POST) > 0) - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Better than if(count($_POST) > 0) (/showthread.php?tid=49756) |
Better than if(count($_POST) > 0) - El Forum - 03-02-2012 [eluser]veledrom[/eluser] Hi, I want to check if POST occurred or not. Currently I'm using code below. I wonder if there is a better way of doing it with CI? Thanks Code: do_save() Better than if(count($_POST) > 0) - El Forum - 03-02-2012 [eluser]Mauricio de Abreu Antunes[/eluser] If you have a button with name, you can do this: Code: if (isset($_POST["submit"])) { Better than if(count($_POST) > 0) - El Forum - 03-02-2012 [eluser]veledrom[/eluser] It is better than mine? Or it is just an alternative suggestion. Better than if(count($_POST) > 0) - El Forum - 03-02-2012 [eluser]aquary[/eluser] In your case, using empty() would be faster. Better than if(count($_POST) > 0) - El Forum - 03-02-2012 [eluser]Mauricio de Abreu Antunes[/eluser] What does it return Code: echo count($_POST); I can not test (@work hehe) now, but if you have 1 (at least) field with "value" not empty, your count function returns > 0 (true). ![]() Better than if(count($_POST) > 0) - El Forum - 03-02-2012 [eluser]veledrom[/eluser] The code works fine. There is no problem. I just just wondering and looking for better and faster approach. Sometimes people come out with better solutions since I'm new in CI. Better than if(count($_POST) > 0) - El Forum - 03-02-2012 [eluser]johnpeace[/eluser] I tend to check based on the presence of a known variable in my $_POST array: Code: if ($this->input->post('action') == 'save') { Better than if(count($_POST) > 0) - El Forum - 03-02-2012 [eluser]Mauricio de Abreu Antunes[/eluser] A lot of sites use a hidden input with hardcoded value. :-) In PHP you do: Code: if (!empty($_POST['hidden_check_submit'])) { Better than if(count($_POST) > 0) - El Forum - 03-02-2012 [eluser]johnpeace[/eluser] [quote author="Mauricio de Abreu Antunes" date="1330699078"]A lot of sites use a hidden input with hardcoded value. :-) In PHP you do: Code: if (!empty($_POST['hidden_check_submit'])) { I never saw the value in that unless it was dynamically generated as some hash of known site values to prevent cross site submissions. Those solutions have generally been too complicated for most of the projects I work on. I'd be interested in hearing how you do it though... Better than if(count($_POST) > 0) - El Forum - 03-02-2012 [eluser]Matalina[/eluser] why not use the input class associated with ci? Code: if($this->input->post('submit_name')) { if the button was not submitted then $this->input->post() returns false; |