How to use $_GET[] in wordpress template? - php

I try to use the $_GET[] array in a wordpress template page but I keep getting this error when I use it :
Fatal error: Can't use function return value in write context in ... on line ...
If I comment every call to $_GET, the page displays without error.
How can I use the $_GET array in this case?
Thanks

If you are using empty() incorrectly to check your $_GET[] variables, it can cause that error.
Look at this-> http://blog.ryanrampersad.com/2009/07/20/fatal-error-cant-use-method-return-value-in-write-context/

there is one plugin that can help you http://wordpress.org/extend/plugins/wordpress-registry/

Related

How to get params in url , and it is array? yii2

I have url like this
http://localhost/belajar4/web/index.php?r=data%2Fsingle&id=2&DataSearch[TANGGAL]=2015-08-04&DataSearch[TANGGAL_SELESAI]=2015-08-12
And I want to get DataSearch['Tanggal'] dan DataSearch[TANGGAL_SELESAI]
I have tried for id with
Yii::$app->request->queryParams['id']
And it was success for it, but not with DataSearch['Tanggal']
When I try for DataSearch['Tanggal'] the error is
Undefined index: DataSearch[TANGGAL]
I think it should have easy to answer but i am newbie for yii2 and i didnt find the solution yet
You can get them from array like this:
Yii::$app->request->queryParams['DataSearch']["TANGGAL_SELESAI"]
Yii::$app->request->queryParams['DataSearch']["TANGGAL"]
You can also use combination of ArrayHelper::getValue() and Yii::$app->request->get() with dot notation:
use Yii;
use yii\helpers\ArrayHelper;
...
$value = ArrayHelper::getValue(Yii::$app->request->get(), 'DataSearch.TANGGAL_SELESAI');
The main advandage is you can avoid Undefined index exception and change default value (third parameter).
Official docs:
Getting values
ArrayHelper::getValue()
Yii::$app->request->get()
I had faced slimier issue & spent lots of time to get the value but somehow I found solution like below.
// Trying to print the value
echo Yii::$app->request->queryParams['DataSearch']['"TANGGAL_SELESAI"'];
echo Yii::$app->request->queryParams['DataSearch']['"TANGGAL"'];

PHP Warning while passing $_POST to array_key_exists()

So I am using the following style of code if(array_key_exists('some_value', $_POST)){echo 'hi';}
For PHP 5.2.17 I am getting a warning from this style of code. This is the warning:
WARNING: argument 2 for array_key_exists() is not either an array or an object on line: 123
This seems strange to me because I believe that the $_POST array should always be defined. Is that not the case? I'm not sure what would cause the $_POST array to not be considered an array. I am not resetting $_POST to anything so it should exist as an array at all times. Does anyone have any idea what is wrong. Please let me know if more information is needed and thank you for the help.
Edit: I should note that this only happens on the production server. My local environment does not have this problem.
The Superglobals $_POST and $_GET are only populated if the script is POSTed to or GET from. In your example, the reason that you'd get that error is if there was not post action to the script. Before checking for a certain post value, you should check to make sure there was a post:
if(isset($_POST)) {
//The form was posted
}
In that fashion. From there, you can check for certain values using array_key_exist, or you can further check isset($_POST['myKey']).
Use if(isset($_POST['some_value'])) { echo 'hi'; } instead. Never had a problem with it.
Also check if you are not overriding or unsetting $_POST (or some framework you are using is doing it for you). I avoid to do so with superglobal variables since I think it is a bad practice and might give headaches like this one.

PHP variable passing

I'm working with drupal. I have this page something.com/node/13. Inside I have this script
<?php
$door = $_GET["variable"];
echo $door;
?>
When I open the URL like this something.com/node/13?variable=1231 I get the following error:
Error message
Notice: Undefined index: variable in eval() (line 2 of /var/www/html/modules/php/php.module(74) : eval()'d code).
and no output. Any ideas what I might be doing wrong?
The error, partcilarly in drupal 7 simply means that you aren't getting any value for $_GET['variable']. This would be because the url doesn't have a value (someurl?variable=foo). You can avoid the notice by prefixing an # like this:
$door = #$_GET['variable'];
but that won't make the variable return any data. It's pretty unclear from the code you've posted what you're trying to accomplish. You might consider starting there. There is no reason why you can't use $_GET variables in drupal. I do so all the time.
Use drupal_get_query_parameters
Because the rewrite rules with Drupal, you don't grab the $_GET values cleanly directly like that.

Passing a variable to PHP with jQuery .load

I have a page ("main.php") which loads content from an external PHP file ("rpc.php"). Using the below syntax on main.php successfully pulls in content from rpc.php:
$("#portfolioContent").load("rpc.php?o="+day+"");
On rpc.php I have an if statement (part of a long switch function), as follows:
if ( $pagename == "home" ) {
break;
}
This break is not occuring because the variable has not been set. rpc.php is used by various parent pages so the variables need to be set on those. On a parent page I have tried using the following code to attempt to set the variable and pass it to rpc.php but to no avail:
$("#portfolioContent").load("rpc.php?o="+day+"$pagename="home"");
Can anybody point me in the right direction? Thank you.
It should be like this,
$("#portfolioContent").load("rpc.php?o="+day+"&pagename=home");
Your syntax is wrong. Try this:
$("#portfolioContent").load("rpc.php?o="+day+"&pagename=home");
Notice i substituted the $ with a &
Cheers
change this line
$("#portfolioContent").load("rpc.php?o="+day+"$pagename="home"");
to this
$("#portfolioContent").load("rpc.php?o="+day+"pagename="home");
then access the variable with $_GET['pagename']
To access variables from the URL in PHP just do:
$_GET['variablename']
For example, with the URL http://www.example.com?hello=hellWorld
echo $_GET['hello'];
would print helloWorld
Also, you use & to separate variables, not $

Undefinded $_GET[' ...'] index

After making a javascript variable accesable into a php file the $_GET['something'] keeps saying undefined index.
Although the proper result gets displayed and being written into the xml.
If i try to initialise it my score no longer shows up for some reason.
How can i initalise this without it showing anything at al on my screen?
Regards.
If you do this with javascript:
window.location.href="index.php?scoreresult="+score
you have to access the variable in PHP using
$_GET['scoreresult']
instead of
$_GET['score']
If you make use of XSL to transform the XML, XSLT::setParameter may be interesting to you. It allows you to register (PHP)-variables for use inside a XSL-stylesheet.

Categories