Welcome Guest, Not a member yet? Register   Sign In
Simple question - isset()
#1

[eluser]Samuurai[/eluser]
Hi everyone,

Simple question.. I have a view which has three modes, new, edit and view...

depending on the mode, it will create a "submit" button or an "update" button which then requests a function in a controller.

In the controller I need to be able to do

Code:
if(isset($this->input->post('update')))

But that doesn't work. Do I really have to type:
Code:
$state = $this->input->post('update')
if(isset($state))

Seems messy setting a variable and not using it.
#2

[eluser]Dam1an[/eluser]
You shouldn't need the isset check, as the post method will return false if the post variable you're checking doesn't exist

Code:
if($this->input->post('update')) {
  This should work
}
#3

[eluser]Samuurai[/eluser]
Oh!
So is isset() a redundant function?
#4

[eluser]jpi[/eluser]
In CI isset is equivalent to $this->input->post('update') !== FALSE

For explanation why isset doesn't work :
Quote:isset() only works with variables as passing anything else will result in a parse error. For checking if constants are set use the defined() function.
(from php doc)
#5

[eluser]Samuurai[/eluser]
They aren't the same. I receive this error if I use this method when I only set the $edit flag:
Code:
if($new)
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: new

Filename: views/user_form.php

Line Number: 3
#6

[eluser]cahva[/eluser]
[quote author="Samuurai" date="1251167548"]They aren't the same. I receive this error if I use this method when I only set the $edit flag:
Code:
if($new)
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: new

Filename: views/user_form.php

Line Number: 3[/quote]
It seems you misunderstood. These are "the same".

Code:
// Normally
if (isset($_POST['update']))

// in CI
if ($this->input->post('update'))
And ofcourse if you send some variables to the view and you use some variable that you might not use in every controller, you would first check if its set:
Code:
if (isset($new))




Theme © iAndrew 2016 - Forum software by © MyBB