Jump to content


Photo

Passing variables via URL


  • Please log in to reply
4 replies to this topic

#1 stefands

stefands
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 13 October 2006 - 02:30 PM

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...hp?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!
.:: who decides reason? .:...

#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 13 October 2006 - 02:34 PM

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.

#3 stefands

stefands
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 13 October 2006 - 02:51 PM

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"?
.:: who decides reason? .:...

#4 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 13 October 2006 - 03:06 PM

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:

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

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.

#5 stefands

stefands
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 13 October 2006 - 03:23 PM

Wackamoly, you're tha man!!!  ;D That works!
Thx a lot  8)
.:: who decides reason? .:...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users