Welcome Guest, Not a member yet? Register   Sign In
Better than if(count($_POST) > 0)
#1

[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');
   }
}
#2

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

}

#3

[eluser]veledrom[/eluser]
It is better than mine? Or it is just an alternative suggestion.
#4

[eluser]aquary[/eluser]
In your case, using empty() would be faster.
#5

[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
#6

[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.
#7

[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
}
#8

[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'])) {
}
#9

[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...
#10

[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;




Theme © iAndrew 2016 - Forum software by © MyBB