Jump to content

Archived

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

stefands

Passing variables via URL

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!

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
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"?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.