Welcome Guest, Not a member yet? Register   Sign In
$this->input->post() and 0 values
#1

[eluser]mikegioia[/eluser]
Hi Guys -

I have an interesting problem with the post data helper methods and having a value of 0 in the post variable.

CodeIgniter returns bool(false) if the item in the POST array doesn't exist. However, I'm also getting bool(false) if a value of 0 is passed through the post array. For example, someone is updating a field and wants to set it to 0.

The problem is I want to use the POST value if it was used and the session otherwise. I can't accurately check for values of 0 since both a 0 value and absence of the POST variable return the same thing. Can anyone shed some light on this?

Thanks,
Mike
#2

[eluser]jedd[/eluser]
When you talk of the post array, do you mean the CI post array, or the $_POST array?

Anyway, this sounds like it might be a job for ===

Either that or lower your expectations.
#3

[eluser]mikegioia[/eluser]
Haha, I meant the CI post() method.

I think I might have to change the approach. It looks like I'll need to first check if the form as submitted, and then use the post values. If it wasn't submitted, then use the session.
#4

[eluser]Whiplash[/eluser]
You should be able to use:

Code:
if( $this->input->post('textbox') != NULL)
{
// do something
}

That will return TRUE for any value and FALSE for an empty string or no POST.

Dana
#5

[eluser]Cro_Crx[/eluser]
You could also check the post value is idential to false instead of checking if it's equal. You can do that with the === operator


Code:
if( $this->input->post('textbox') === false)
{
// do something
}



More info here ==> http://www.php.net/manual/en/language.op...arison.php. Also look at the table here ==> http://www.php.net/manual/en/types.comparisons.php in the top table (using ==) you'll notice that 0 == false. In the bottom table you can see that 0 !== false.

Hope that helps.
#6

[eluser]mikegioia[/eluser]
Thanks whiplash, that seems to be exactly what I was after.

I remember trying === 0 but not === false.
#7

[eluser]jedd[/eluser]
If only someone had suggested that in the first reply in this thread.
#8

[eluser]Cro_Crx[/eluser]
Lol well done. I linked to a pretty table though!




Theme © iAndrew 2016 - Forum software by © MyBB