$_POST vs. 'post'

Discussion and education for beginner / novice programmers interested in embarking on the development process to take advantage of the extensible nature of the Joomla! CMS.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Forum rules
Post Reply
shahpound
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Sat Jan 05, 2008 10:23 pm
Location: Cambridge, MA

$_POST vs. 'post'

Post by shahpound » Thu Mar 13, 2008 12:55 pm

Hi,

In a component I created, depending on the system, visitors will get errors from this:

$this->assignRef( 'name', JRequest::getVar( 'name','', 'post', 'string') );

but not from this:

$this->assignRef( 'name', JRequest::getVar( 'name','', $_POST, 'string') );

The error is from PHP:

*Fatal error*: Only variables can be passed by reference in ... [filename]

Any ideas why this happens and what the difference is?

radiant_tech
Joomla! Apprentice
Joomla! Apprentice
Posts: 41
Joined: Sat Dec 15, 2007 3:02 pm
Location: Washington DC Metro

Re: $_POST vs. 'post'

Post by radiant_tech » Sat Mar 15, 2008 3:17 pm

Instead of including the getVar method within the assignRef method, set the variable first and then pass the variable to assignRef(). Such as

Code: Select all

$name = JRequest::getVar( 'name','', 'post', 'string');
$this->assignRef('name', $name);
Denise

shahpound
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Sat Jan 05, 2008 10:23 pm
Location: Cambridge, MA

Re: $_POST vs. 'post'

Post by shahpound » Sat Mar 15, 2008 4:20 pm

Thanks for the reply, is there really a difference (other than syntatically of course) betweenthese two methods?

User avatar
Rogue4ngel
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 199
Joined: Sun Nov 26, 2006 10:46 pm
Location: New York

Re: $_POST vs. 'post'

Post by Rogue4ngel » Sat Mar 15, 2008 9:13 pm

I believe yours is passing the value of the string, the other is passing a reference to the value.
If you're not a part of the solution, you're a part of the problem.

User avatar
louis.landry
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 101
Joined: Wed Aug 17, 2005 11:03 pm
Location: New Orleans, Louisiana
Contact:

Re: $_POST vs. 'post'

Post by louis.landry » Sat Mar 15, 2008 9:29 pm

The correct way to do what you are wanting to do is:

Code: Select all

$this->assign( 'name', JRequest::getVar( 'name','', 'post', 'string') );
http://api.joomla.org/Joomla-Framework/ ... tml#assign

You cannot assign a value by reference... you may only assign a reference by reference.

JView::assignRef() assigns by reference.
JRequest::getVar() returns a value.

Louis
Project Manager :: Developer
http://www.webimagery.net
A hacker does for love what others would not do for money.

User avatar
Rogue4ngel
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 199
Joined: Sun Nov 26, 2006 10:46 pm
Location: New York

Re: $_POST vs. 'post'

Post by Rogue4ngel » Sat Mar 15, 2008 9:32 pm

Thanks Louis. Always helps to get it 'from the source'.
If you're not a part of the solution, you're a part of the problem.


Post Reply