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.
Related
this is my first time using PHP in a real project environment. The project is pretty simple, take an existing, working PHP site and update the HTML to be consistent with HTML5. After designing the HTML, I am inserting the PHP from the previous site. This works most of the time, but I get a few errors. For instance:
<?
$sec = $_GET['sec'];
if ($sec == "1") {
echo ('Success!');
}
?>
Is causing the error:
Notice: Undefined index: sec in /file_that_holds_site_build.
Of course that is only if the url doesn't include the append tag (=1) that alerts the message.
So the question is this, what am I missing that causes the $GET when there is no $sec? How do I eliminate this error on first page load?
You're getting that notice because you're trying to access an array index that doesn't exist in some scenarios. Here's how you should be getting the data out of the request.
$sec = array_key_exists('sec', $_GET) ? $_GET['sec'] : null;
Thanks to everyone who provided possible answers to this question. It was Daniel that came up with the easiest fix. Again, I am just adjusting someone else's code to work, so a universal solve would involve too much of my own writing. To the point, the final code looks like this:
<?
if (isset($_GET["sec"])){
$sec = $_GET['sec'];
if ($sec == "1") {
echo ('Success! Your username and password have been sent via email.');
}}
?>
Notice the added if statement. As I said in a comment to Daniel, SO SIMPLE!
Thanks again for everyone's help. I hope to be likewise of service to you all soon.
Simple just use isset($_GET['sec']) to check for the parameter 'sec' before using it in the php code. That should eliminate the error. This is quite trivial I suppose.
I often simply extract() the wohle $_GET super global and then either get the desired variable or not. As a kind of "declaration" I initialize each expected variable first with false. This way I find it much easier to handle than individually doing a check like if(isset($_GET['element'])) ...
$var1=$var2=$var3=false; // my desired variables
extract($_GET); // everything I get from the form
// or: extract($_REQUEST);
if ($var1) { /* do something with it */ }
Possible security risk:
Of course you should be aware that everybody could simply include their own variable as an argument to he page ...
After hours of frustration, I finally found the line of code that has been causing an error, but now I need to know why.
jQuery was throwing this error: Uncaught Error: Syntax error, unrecognized expression: .
I've researched it and found that this is a Sizzle error that occurs when jQuery cannot find the selector that is referenced. As was suggested in other questions on SO, this was not actually an error in my jQuery code, it was elsewhere.
I was defining a variable to use as a target element to load content, and was using PHP to dynamically define the variable, like so:
var $container = $(".<? echo $target ?>");
This did not work, as the . is flagged as an unrecognized expression. However, replacing the PHP variable with a static string works fine:
var $container = $(".target");
This was so hard for me to find because I couldn't pinpoint the line that was throwing the error, and in the source from the browser, the initial line above looks just like the second line.
Why does the first example not work? Does it have to do with order of execution? And, how can I use a dynamic variable as a selector?
you have to use
<?php echo $test; ?>
or the shortcut:
<?= $test ?>
You can try trim($target) before doing this. If it works you probably have some unwanted spaces in that variable of yours.
Also consider using json_encode to pass variables from php to javascript. Like so:
var selector = <?php echo json_encode($target); ?>;
var $container = $(selector);
This will allow you to pass not only simple strings but more complex variable structures as well (and with encoding safety).
Turns out the page I was loading wasn't having the variable $target passed to it. On the initial page, $target was initialized with a value, and thus the source output looked as specified in the question. However, the ajax call I was making to reload the page with new data was not passing the variable.
So this currently loads the page for me.
/controllername/view/id/27/title/shoes
However, I want the user to be able to type in:
/controllername/27/shoes
to view the file. So in the routing file, I made a rule like this,
$route['controllername/(:num)/(:any)'] = "controllername/view/id/$1/title/$2";
The original address works with uri_to_assoc(n) because I have it as
$array = $this->uri->uri_to_assoc(3);
$id = $array['id'];
$title = $array['title'];
But once I route it and use the new address to access the file, I get the errors:
Message: Undefined index: id
Message: Undefined index: title
I do not get these errors with my original url way of accessing it. I guess the value of uri_to_assoc(3) changes once the url is changed but I thought the routing function would take care of that. Can anyone help me get rid of these variable errors?
In this case you'll want ruri_to_assoc:
http://codeigniter.com/user_guide/libraries/uri.html
$this->uri->ruri_to_assoc(n)
This function is identical to [uri_to_assoc], except that it creates
an associative array using the re-routed URI in the event you are
using CodeIgniter's URI Routing feature.
You should still validate your array indices anyways, in case the URL itself is invalid.
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/
I am working on modification of a program. There are two value passes happened in one PHP file, and I get a notification from system like this:
Notice: Undefined index: content in /Users/alexhu/NetBeansProjects/menagerie/svn/trunk/apps/frontend/modules/legacy/legacy_lib/content/utilities/hraprint.php on line 23
And
Notice: Undefined index: isProvena in /Users/alexhu/NetBeansProjects/menagerie/svn/trunk/apps/frontend/modules/legacy/legacy_lib/content/utilities/hraprint.php on line 24
How to avoid it?
That error means you've basically got something like:
$my_arrray = array();
if ($my_array['content'] == ....) {
}
... attempting to access an index/key in an array which has not yet been defined. To guard against it, you'd need something like:
if (isset($my_array['content']) && ($my_array['content'] == ....)) {
Well you are trying to use an index of an array which doesn't exist.
The error is stating what is wrong :-)
You're simply trying to access $array['content'] and $array['isProvena'] in your code. Without the code we can't tell you anything though.