CodeIgniter Forums

Full Version: CI4 controller can't 'see' a GET parameter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
you've got <input type="input" name="title" /> should be <input type="text" name="title" />
(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?
Looks like it. You can try var_dump($this->request->getGet('title')); too.
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.
(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?