Welcome Guest, Not a member yet? Register   Sign In
CI4 controller can't 'see' a GET parameter
#1

(This post was last modified: 02-06-2020, 06:21 PM by Ged.)

the form:

Code:
<form action="/news/create">

    <label for="title">Title</label>
    <input type="input" name="title" /><br />

the link:

https://df2.pennyjack.co.uk/news/create?...+news+item
[url=https://df2.pennyjack.co.uk/news/create?title=test+title&body=this+is+a+test+body&submit=Create+news+item][/url]
the check code:

PHP Code:
public function create()
 {
helper('form');
$model = new NewsModel();

var_dump($this->request->getVar('body'));
echo 
'<br>';
var_dump($this->request->getVar('title'));

//die(); 

the result:


PHP Code:
string(19"this is a test body"

NULL 


It can see the body but not the title.  Any idea?

Thank you
Reply
#2

you've got <input type="input" name="title" /> should be <input type="text" name="title" />
Simpler is always better
Reply
#3

(This post was last modified: 02-07-2020, 01:12 AM by Ged. Edit Reason: clarify and remove formatting )

(02-06-2020, 08:20 PM)donpwinston Wrote: you've got <input type="input" name="title" /> should be <input type="text" name="title" />

that's the tutorial Smile and the uri is correctly formed. I've changed it to "text" and the result is the same. The parameter is arriving; the uri is:
[/url]

Code:
https://df2.pennyjack.co.uk/news/create?title=this+is+a+test+title&body=this+is+a+test+body&submit=Create+news+item


so it's arriving at the controller correctly.  Var_dumping the query object gives:


Code:
["query":protected]=> array(3) { ["title"]=> string(20) "this+is+a+test+title" ["body"]=> string(19) "this+is+a+test+body" ["submit"]=> string(16) "Create+news+item" }
[url=https://df2.pennyjack.co.uk/news/create?title=this+is+a+test+title&body=this+is+a+test+body&submit=Create+news+item]



Accessing 'title' directly from $_GET gives the correct result. Using getVar returns NULL. Do I have to assume it's a bug in the framework?
Reply
#4

Looks like it. You can try var_dump($this->request->getGet('title')); too.
Simpler is always better
Reply
#5

getVar() pulls from $_REQUEST. Try dumping out your $_GET and $_REQUEST vars to see what shows up. I don't know of a reason it wouldn't show up in the REQUEST object of the top of my head.

Also, as dropwinston said, you can use $this->request->getGet('title') when you know it's coming from $_GET.
Reply
#6

(02-07-2020, 12:08 PM)kilishan Wrote: getVar() pulls from $_REQUEST.  Try dumping out your $_GET and $_REQUEST vars to see what shows up. I don't know of a reason it wouldn't show up in the REQUEST object of the top of my head.

Also, as dropwinston said, you can use $this->request->getGet('title') when you know it's coming from $_GET.

ahh... there is progress. Dumping $_REQUEST gives me:


array(3) { ["//news/create?title"]=> string(12) "lkjhlkjhlkjh" ["body"]=> string(16) "4353425435342543" ["submit"]=> string(16) "Create news item" } lkjhlkjhlkjh

Ok, not seen that before. Why is the whole uri there for title?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB