CodeIgniter Forums
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)

Pages: 1 2 3


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()
{
   if(count($_POST) > 0)
   {
      //Validate fields with form validator
   }
   else
   {
      redirect('index');
   }
}



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"])) {
}

<input type="submit" name="submit">

isset($_POST["submit"]) returns false

<input type="submit" name="submit" value="Next">

isset($_POST["submit"]) returns true.

}




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);
in your code?
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). Smile


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') {
   // the form was submitted...do stuff here
}



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'])) {
}
[/quote]

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')) {
// do something
}

if the button was not submitted then $this->input->post() returns false;