Welcome Guest, Not a member yet? Register   Sign In
CI4 add value to post array
#1

In CI3 if I needed to add a value to the received post array, I could do:

PHP Code:
$_POST['keyValue'] = value
in CI4 it doesn't work, how can I get the same result?
Reply
#2

Hi, use
Code:
$request->getVar('keyValue');
.

For more information, please read this link.
Reply
#3

(04-20-2022, 01:10 AM)datamweb Wrote: Hi, use
Code:
$request->getVar('keyValue');
.

For more information, please read this link.

I don't have to retrieve a value but add one to the post array
Reply
#4

(04-20-2022, 06:19 AM)serialkiller Wrote:
(04-20-2022, 01:10 AM)datamweb Wrote: Hi, use
Code:
$request->getVar('keyValue');
.

For more information, please read this link.

I don't have to retrieve a value but add one to the post array

Oh!!, I realized.
You must use the following code:

Code:
$rQ=\Config\Services::request();
$rQ->setGlobal('post',['keyValue' => 'Datamweb']);


var_dump( $rQ->fetchGlobal('post','keyValue'));
Reply
#5

What are you trying to do?
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#6

(This post was last modified: 04-26-2022, 05:25 AM by serialkiller.)

(04-20-2022, 11:11 AM)datamweb Wrote:
(04-20-2022, 06:19 AM)serialkiller Wrote:
(04-20-2022, 01:10 AM)datamweb Wrote: Hi, use
Code:
$request->getVar('keyValue');
.

For more information, please read this link.

I don't have to retrieve a value but add one to the post array

Oh!!, I realized.
You must use the following code:

Code:
$rQ=\Config\Services::request();
$rQ->setGlobal('post',['keyValue' => 'Datamweb']);


var_dump( $rQ->fetchGlobal('post','keyValue'));

It doesn't work, it just returns that value, I need all the values in post plus an addition

(04-21-2022, 01:23 AM)ignitedcms Wrote: What are you trying to do?

In a controller I receive the data in post and I need to add a value to the post array
Reply
#7

(This post was last modified: 04-26-2022, 12:34 PM by ignitedcms.)

Quote:In a controller I receive the data in post and I need to add a value to the post array

I get that, but I was wondering why? Normally once you have the post data you pass it off to a model for evaluation. If you wanted to add another arbitrary value you could do so and then pass it off to a model, perhaps you could give us a little bit more background information so we can help?
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#8

First of all, you do not need to use superglobals in your code.
It is considered a bad practice, and you should not do it.

But if you really want to use $_POST, you could use it and it works in CI4.

PHP Code:
<?php
namespace App\Controllers;
class 
Home extends BaseController
{
    public function index()
    {
        $_POST['keyValue'] = 'value';
        var_dump($_POST); // You will see 'keyValue' => 'value'
    }

Reply




Theme © iAndrew 2016 - Forum software by © MyBB