Jump to content

Passing variables via URL


stefands

Recommended Posts

Hello all,

I've searched through the current postings to find a solution for my problem, without result, hence this posting  ::) I'm trying to pass a variable from one page to another. My webhosing company is running PHP 5.0.5 and "register_globals" is set to "on" in my php.ini file.
This is an example line of code I'm using:

<a href=\"resultone.php?cmpnr={$row["campref"]}\"><img src=\"../images/icons/moreinfo.gif\" width=\"62\" height=\"17\" border=\"0\" align=\"middle\"></a>

As an example, the link in the browser looks like so:
http://www.mywebsite.com/resultone.php?cmpnr=GR8590

So far so good I though... However, when using the link and checking the newly auto-defined variable $cmpnr in 'resultone.php' - it's empty?? I'd really appreciate any advise on this.
Thx!
Link to comment
https://forums.phpfreaks.com/topic/23854-passing-variables-via-url/
Share on other sites

i would recommend getting into the habit of using the superglobals to access your variables, like $_GET['cmpnr'].  although this doesn't answer your original question, it might solve your problem.

try using print_r(get_defined_vars()) to see what's actually being defined in your script.
When I use "print(get_defined_vars());" I get "Array" as result.

This is where I use the var:
$query = "SELECT * FROM greece WHERE campref='$cmpnr'";
How would I use superglobals here as you say?

What's more: when I use the syntax "resultone.php?cmpnr=value" then php should automatically  create the variable $cmpnr in "resultone.php" right? I don't get any warnings when using an "echo $cmpnr;" so the variable is created. If php recognises that it must create $cmpnr then why doesn't it add the given value, which in this case is "GR8590"?
first, you need to use print_r(), not print() (subtle difference, but as you see, print() doesn't work with arrays).

to use superglobals within a string (or indeed any array), just surround the value in braces so that the parser doesn't jog on the single quotes:

[code]$query = "SELECT * FROM greece WHERE campref='{$_GET['cmpnr']}'";[/code]

finally, you won't get any errors if you use echo $var; and $var is undefined or uninitiated.  you will only be warned of it if you have notices on, which most hosts do not.  use error_reporting(E_ALL) to see notices, and suddenly a whole new ugly world of notices comes out.  theoretically it SHOULD create $cmpnr locally, but that's only in the case that the .ini setting is working correctly.

give that print_r() a whirl and check what's being defined.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.