CodeIgniter Forums
Avoiding something with $_POST - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Avoiding something with $_POST (/thread-21069.html)



Avoiding something with $_POST - El Forum - 07-29-2009

[eluser]Ci beginner[/eluser]
Hey there!

Just a simple question... with PHP, I used to write:

Code:
if (!$_POST) ...

But now, using the $this->input->post('blabla'); and stuff... how can I make the same thing in CI? I mean, if the user didn't posted anything, do X.

Thank you!


Avoiding something with $_POST - El Forum - 07-29-2009

[eluser]Jay Logan[/eluser]
Technically, you can still use the way you used to write it.

What exactly are you trying to do? Check to see if POST data is blank or what? If so, look at the Form Validation library.


Avoiding something with $_POST - El Forum - 07-29-2009

[eluser]Eric Barnes[/eluser]
I would also recommend the form validation. Or something like:
$var = $this->input->post('var');
if($var)
{
//whatever
}


Avoiding something with $_POST - El Forum - 07-29-2009

[eluser]aquary[/eluser]
Are you trying to check if data is posted from forms?

I use something like:

Form's html:
Code:
<form name="submitform" action="somewhere">
<input type="text" name="abc">
<input type="submit" name="submit" value="submit">
</form>

In CI:
Code:
<?
     if($this->input->post('submit'){
          // process data
     }
     $this->load->view('form');
?>



Avoiding something with $_POST - El Forum - 10-05-2009

[eluser]uptime[/eluser]
I usually try to use built-in CI functions/classes but you can give this a shot:

Code:
if (!function_exists('form_post')) :
function form_post()
{
    return ($_SERVER['REQUEST_METHOD'] == 'POST');
}
endif;



Avoiding something with $_POST - El Forum - 10-05-2009

[eluser]BrianDHall[/eluser]
[quote author="Ci beginner" date="1248935582"]Hey there!

Just a simple question... with PHP, I used to write:

Code:
if (!$_POST) ...

But now, using the $this->input->post('blabla'); and stuff... how can I make the same thing in CI? I mean, if the user didn't posted anything, do X.

Thank you![/quote]

Tons of options, none of them more right than another.

The only one thing is technically...the way you were doing it wasn't strictly 'right', as if nothing was posted $_POST would be an unitialized variable and would generate a PHP Notice.

A few options:

Code:
if (! is_set($_POST) )
if ( empty($_POST) )
if (! $this->input->post('necessary_form_field') ) <- my preference
if ($_SERVER['REQUEST_METHOD'] != "POST")

The Input class of CodeIgniter is purely to allow extra helper and convenience functions and provide some automatic security functioning so you don't have to do so much data sanitizing.

There is nothing, however, unsafe about just checking to set if something was POSTed by accessing the $_POST superglobal directly.

No reason to change what you are doing (other than not accessing an uninitialized variable), and there isn't much of a real advantage to using a CI built-in method.


Avoiding something with $_POST - El Forum - 10-06-2009

[eluser]uptime[/eluser]
[quote author="BrianDHall" date="1254818469"][quote author="Ci beginner" date="1248935582"]Hey there!

Just a simple question... with PHP, I used to write:

Code:
if (!$_POST) ...

But now, using the $this->input->post('blabla'); and stuff... how can I make the same thing in CI? I mean, if the user didn't posted anything, do X.

Thank you![/quote]

Tons of options, none of them more right than another.

The only one thing is technically...the way you were doing it wasn't strictly 'right', as if nothing was posted $_POST would be an unitialized variable and would generate a PHP Notice.
[/quote]

Paying attention to warnings is pretty important (in my opinion), in this case ! $var would check if the variable exists or not but in other scenario, the warning could be generated (undefined variable) and cause issues with the code (in case of course the programmer didn't check if the variable exists.

Bottom line, it's may not be "strict" but definitely "important" to pay attention to everything, the better code you write, the more you learn and less you program / fix in the future.

Don't even ask me how I got to it... :-)


Avoiding something with $_POST - El Forum - 10-06-2009

[eluser]Zack Kitzmiller[/eluser]
Throwing warnings is now always an issue..

It it was '@' wouldn't be available in PHP to suppress warnings.


Avoiding something with $_POST - El Forum - 10-06-2009

[eluser]cahva[/eluser]
[quote author="BrianDHall" date="1254818469"]

The only one thing is technically...the way you were doing it wasn't strictly 'right', as if nothing was posted $_POST would be an unitialized variable and would generate a PHP Notice.
[/quote]
Well not exactly right because $_POST itself is always initialized so using
Code:
if (!$_POST)
..will not generate notice. $_POST will be just an empty array.