Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] - Strange problem with POST data
#1

(This post was last modified: 09-28-2020, 05:09 AM by Matleyx.)

Hi guys..
I started a new project with ci4 (my first time with 4), and I have a problem that seems really stupid: I can't pass POST data between the view and the controller. I've done a lot of test, but I don't understand where the problem lies.

The controller Test.php:

Code:
<?php namespace App\Controllers;

use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Response;

class Test extends BaseController
{

public function index()
{
            $response = service('response');       
            $request = service('request');
           
            echo $request->uri->getPath()."<br>";
            echo $request->getMethod()."<br>";
           
            if ($request->getVar('submit'))
                {
                    if (isset($_POST['name']))
                        {
                            echo "Nome: ".$_POST['name']."<br>";
                        }   
                    if (isset($_POST['lastname']))
                        {
                            echo "Cognome: ".$_POST['lastname'];
                        }
                    return view('test_1');
                } else
                    {
                        return view('test_2');
                    }
}

//--------------------------------------------------------------------

}

view test_1.php:

Code:
Test1 si post


view test_2.php

Code:
Test 2 nessun post
<form action="test" method="post">
    <input type="text" name="name" value="Mario" id="name">
    <input type="text" name="lastname" value="Rossi" id="lastname">
    <input type="submit" name="submit" value="submit" id="submit">
</form>

When i try to submit the form, in the response, i haven't any POST data and reopen test_2 view. This is the html source code:

Code:
test<br>get<br><!-- DEBUG-VIEW START 1 APPPATH/Config/../Views/test_2.php -->

Test 2 nessun post

<form action="test" method="post">

<input type="text" name="name" value="Mario" id="name">

<input type="text" name="lastname" value="Rossi" id="lastname">

<input type="submit" name="submit" value="submit" id="submit">

</form>

<!-- DEBUG-VIEW ENDED 1 APPPATH/Config/../Views/test_2.php -->



<script type="text/javascript" id="debugbar_loader" data-time="1600851974" src="[url=http://www.yiisupertest.com/?debugbar]http://www.yiisupertest.com/?debugbar[/url]"></script><script type="text/javascript" id="debugbar_dynamic_script"></script><style type="text/css" id="debugbar_dynamic_style"></style>

I don't understand where i s the problem....
If i try without codeigniter, with only php simple file, the post data works fine.
I followed this guide:https://guidaphp.it/base/variabili-superglobali and post data work.

I don't know how to do it anymore

any help is welcome
Reply
#2

You can process POST with:

$request = \Config\Services::request();

$name = $request->getPost('name');
$lastname = $request->getPost('lastname');

Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams

Reply
#3

(09-23-2020, 02:23 AM)nc03061981 Wrote: You can process POST with:

$request = \Config\Services::request();

$name = $request->getPost('name');
$lastname = $request->getPost('lastname');

I tryed but don't work:
Test.php:
Code:
<?php namespace App\Controllers;

use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Response;

class Test extends BaseController
{

public function index()
{
            $response = service('response');       
            //$request = service('request');
            $request = \Config\Services::request();
           
            echo $request->uri->getPath()."<br>";
            echo $request->getMethod()."<br>";
           
            $name = $request->getPost('name');
            $lastname = $request->getPost('lastname');
            echo 'StarttestingName'.$name.'EndtestingName<br>';
           
            if ($request->getVar('submit'))
                {
                    if (isset($_POST['name']))
                        {
                            echo "Nome: ".$_POST['name']."<br>";
                        }   
                    if (isset($_POST['lastname']))
                        {
                            echo "Cognome: ".$_POST['lastname'];
                        }
                    return view('test_1');
                } else
                    {
                        return view('test_2');
                    }
}

//--------------------------------------------------------------------

}

Output:
Code:
test<br>get<br>StarttestingNameEndtestingName<br><!-- DEBUG-VIEW START 1 APPPATH/Config/../Views/test_2.php -->

Test 2 nessun post

<form action="test" method="post">

<input type="text" name="name" value="Mario" id="name">

<input type="text" name="lastname" value="Rossi" id="lastname">

<input type="submit" name="submit" value="submit" id="submit">

</form>

<!-- DEBUG-VIEW ENDED 1 APPPATH/Config/../Views/test_2.php -->



<script type="text/javascript" id="debugbar_loader" data-time="1600853547" src="[url=http://www.yiisupertest.com/?debugbar]http://www.yiisupertest.com/?debugbar[/url]"></script><script type="text/javascript" id="debugbar_dynamic_script"></script><style type="text/css" id="debugbar_dynamic_style"></style>

as you can see in the output, the variable "name" is empty, and there is nothing between the two strings testingname.....
I'm going crazy.....
Reply
#4

(This post was last modified: 09-23-2020, 03:02 AM by nc03061981.)

error Logic

First

var_dump($_POST);
die();

to see your post info

No need 2 lines:
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Response;

Only use:
$request = \Config\Services::request();

and Form action, you should leave empty to post to current Controller

<form action="test" method="post">

to

<form action="" method="post">

Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams

Reply
#5

(This post was last modified: 09-23-2020, 03:30 AM by Matleyx.)

(09-23-2020, 02:49 AM)nc03061981 Wrote: error Logic

First

var_dump($_POST);

die();

to see your post info

No need 2 lines:
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Response;

Only use:
$request = \Config\Services::request();

and Form action, you should leave empty to post to current Controller

<form action="test" method="post">

to

<form action="" method="post">


I added a new public function in controller with name "return" where i put var_dump($_POST); and die();
I change the action in form to test/return.

the response:


Code:
array(0) { }

I have never had all these problem in my php experience.......


For information, i testing my apache and php with this simple index.php file:
Code:
<?php
if (isset($_POST['submit'])) {
    if (isset($_POST['name'])) {
        echo "Nome: ".$_POST['name']."<br>";
    }
   
    if (isset($_POST['lastname'])) {
        echo "Cognome: ".$_POST['lastname'];
    }
} else {
?>
<form action="/" method="post">
    <input type="text" name="name" value="Mario">
    <input type="text" name="lastname" value="Rossi">
    <input type="submit" name="submit" value="submit">
</form>
<?php
}
?>

The output, after submit is:
Code:
Nome: Mario<br>Cognome: Rossi

The post data work fine OUTSIDE Codeigniter.......
Is there a limitation?
I there a flag that i don't know?
this is my .env file, if necessary (i'm in development environmental):
Code:
#--------------------------------------------------------------------
# Example Environment Configuration file
#
# This file can be used as a starting point for your own
# custom .env files, and contains most of the possible settings
# available in a default install.
#
# By default, all of the settings are commented out. If you want
# to override the setting, you must un-comment it by removing the '#'
# at the beginning of the line.
#--------------------------------------------------------------------

#--------------------------------------------------------------------
# ENVIRONMENT
#--------------------------------------------------------------------

CI_ENVIRONMENT = development

#--------------------------------------------------------------------
# APP
#--------------------------------------------------------------------

# app.baseURL = ''
# app.forceGlobalSecureRequests = false

# app.sessionDriver = 'CodeIgniter\Session\Handlers\FileHandler'
# app.sessionCookieName = 'ci_session'
# app.sessionSavePath = NULL
# app.sessionMatchIP = false
# app.sessionTimeToUpdate = 300
# app.sessionRegenerateDestroy = false

# app.cookiePrefix = ''
# app.cookieDomain = ''
# app.cookiePath = '/'
# app.cookieSecure = false
# app.cookieHTTPOnly = false

# app.CSRFProtection  = false
# app.CSRFTokenName  = 'csrf_test_name'
# app.CSRFCookieName  = 'csrf_cookie_name'
# app.CSRFExpire      = 7200
# app.CSRFRegenerate  = true
# app.CSRFExcludeURIs = []

# app.CSPEnabled = false

#--------------------------------------------------------------------
# DATABASE
#--------------------------------------------------------------------

database.default.hostname = 127.0.0.1
database.default.database = ci4-test2
database.default.username = ci4-test2
database.default.password = ci4-test2
database.default.DBDriver = MySQLi

# database.tests.hostname = localhost
# database.tests.database = ci4
# database.tests.username = root
# database.tests.password = root
# database.tests.DBDriver = MySQLi

#--------------------------------------------------------------------
# CONTENT SECURITY POLICY
#--------------------------------------------------------------------

# contentsecuritypolicy.reportOnly = false
# contentsecuritypolicy.defaultSrc = 'none'
# contentsecuritypolicy.scriptSrc = 'self'
# contentsecuritypolicy.styleSrc = 'self'
# contentsecuritypolicy.imageSrc = 'self'
# contentsecuritypolicy.base_uri = null
# contentsecuritypolicy.childSrc = null
# contentsecuritypolicy.connectSrc = 'self'
# contentsecuritypolicy.fontSrc = null
# contentsecuritypolicy.formAction = null
# contentsecuritypolicy.frameAncestors = null
# contentsecuritypolicy.mediaSrc = null
# contentsecuritypolicy.objectSrc = null
# contentsecuritypolicy.pluginTypes = null
# contentsecuritypolicy.reportURI = null
# contentsecuritypolicy.sandbox = false
# contentsecuritypolicy.upgradeInsecureRequests = false

#--------------------------------------------------------------------
# ENCRYPTION
#--------------------------------------------------------------------

# encryption.key =
# encryption.driver = OpenSSL

#--------------------------------------------------------------------
# HONEYPOT
#--------------------------------------------------------------------

# honeypot.hidden = 'true'
# honeypot.label = 'Fill This Field'
# honeypot.name = 'honeypot'
# honeypot.template = '<label>{label}</label><input type="text" name="{name}" value=""/>'
# honeypot.container = '<div style="display:none">{template}</div>'
Reply
#6

You should leave form action=''
- because your data do not post to right Controller, so you will not see post data

Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams

Reply
#7

(09-23-2020, 04:08 AM)nc03061981 Wrote: You should leave form action=''
- because your data do not post to right Controller, so you will not see post data
Sorry Nc but i don't understand.... 
If i leave blank the action, where i put var_dump? Where it going the output from the form?
Reply
#8

(This post was last modified: 09-23-2020, 04:37 AM by nc03061981.)

Var_dump in your controller
1. Comment
// var_dump($_POST)
// die();
save file and refresh browser
2. Uncomment
var_dump($_POST)
die();
Save file and press Sumbit button

Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams

Reply
#9

(09-23-2020, 04:31 AM)nc03061981 Wrote: Var_dump in your controller
1. Comment
// var_dump($_POST)
// die();
save file and refresh browser
2. Uncomment
var_dump($_POST)
die();
Save file and press Sumbit button

Same result:

Code:
array(0) {

}

I don't whant rebuild my debian box.....
Reply
#10

Never tried it this way but why is the post action just the name of the function ? and not the url to the function like :

action="<?php echo base_url('ControllerName/function'); ?>"

then in your controller

$request = \Config\Services::request();

$name = $request->getVar('name');
$lastname = $request->getVar('lastname');
Reply




Theme © iAndrew 2016 - Forum software by © MyBB