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

[eluser]sqwk[/eluser]
Code:
if($this->input->post('first_name')) {
   // Do something
};

When the first name field has been set (The form has been submitted) but is empty ('') the if statement will not execute.

However, when one saves the post value into a variable first, it will.

Code:
$first_name = $this->input->post('first_name');
if(isset($first_name)) {
   // Do something
};

Inconsistent?
#2

[eluser]dioramayuanito[/eluser]
Code:
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Test extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
?>
<html><head><title>Test</title></head>
<body><form action="/index.php?fa=test&m=post" method="post">
    <input type="text" name="thisis">
    <input type="submit" name="Submit" value="submit">
</form></body>
</html>
<?php
    }
    
    function post() {
        //first if
        if ($this->input->post('thisis')) {
            echo("ok1\r\n");
        }
        //second if
        $thisis = $this->input->post('thisis');
        if ($thisis) {
            echo("ok2\r\n");
        }
        echo("end\r\n");
    }
}

"first if" and "second if" run exactly the same. Am i missing somethin?
#3

[eluser]sqwk[/eluser]
You used if($thisis) and not if(isset($thisis))—sorry, apparently posted the wrong example…

I get the following:

Code:
$field = $this->input->post('field')

// Form has been submitted, but has empty value (Set, but empty)
if ($this->input->post('field')) // FALSE -------------------------- Should be TRUE, or not?
if (isset($field)) // TRUE
if ($field) // FALSE

// Form has been submitted and has a value (Set and not empty)
if ($this->input->post('field')) // TRUE
if (isset($field)) // TRUE
if ($field) // TRUE

// Form has not been submitted (Not set) (No POST data according to profiler)
if ($this->input->post('field'))  // FALSE
if (isset($field)) // TRUE --------------------------- Or rather this should be FALSE?
if ($field) // FALSE
#4

[eluser]dioramayuanito[/eluser]
Code:
// Form has been submitted, but has empty value (Set, but empty)
if ($this->input->post('field')) // FALSE -------------------------- Should be TRUE, or not?

because $this->input->post('field') contains empty string ''. it returns FALSE if you use it inside if-statement.
it actually same with $field.

All issets return TRUE because you already declare $field before you use isset($field) statement.
every variables which already declared





Code:
$var = '';

// This will evaluate to TRUE so the text will be printed.
if (isset($var)) {
    echo "This var is set so I will print.";
}
from isset example at http://php.net/manual/en/function.isset.php




Theme © iAndrew 2016 - Forum software by © MyBB