View Full Version : Can't get variable to transfer when link is clicked.

12-22-2010, 06:19 PM
I've realised that my previous problem basically boils down to this problem. I need to transfer a variable to a new page as a kind of 'piggy-back' when a link is clicked to go to that page. I've searched and tried various syntax but what keeps happening is when the link is clicked (it's an image) the link thinks the destination is the variable rather than the actual page to link to.
This is the code:

echo "<a href=\"pict_pic_page.php" . $new . "\">" <img src=" echo $new ;" "width = "72" height = "84" border="0" alt "car""></a>
The image source bit is fine. However, when I click the link, instead of going to pict_pic_page.php the parse decodes $new ( it's a route to a file) and attempts to find this page (obviously there's no such url).
I've tried three versions of the line above but all do the same thing.

Could someone just tell me what this line of code should be as unique seasonal gift !
Thanks in anticipation - Songboy

12-22-2010, 09:37 PM
OK I've experimented and realised that if I put in a question mark, as in:

echo "<a href=\"pict_pic_page.php?" . $new . "\">" etc, etc, it finds the php page.

I have two such test lines as above, one carries the variable $new and the other carries the variable $new1. Each of the variables is activated depending on which of two images is clicked. What is supposed to happen is that the variables are used as comparisons in a mysqli query and this delivers an id which itself then finds a specific user page. Unfortunately, when testing, I've noticed that my code does not distinguish which of the triggers is used. This code delivers the user page connected with the first photo no matter which picture (of the two) is clicked:

$pict_pic = $new;
$pict_pic = $new1;

This code delivers the user page connected with the second image, no matter which of the two images is clicked.

$pict_pic = $new;
$pict_pic = $new1;

I am sure this is a simple problem (probably one I've come across before) but I'm not sure how to separate the outcomes ie when you click on this image you get that outcome and when you click on the other image, you get the other outcome.
Any help would be appreciated.

12-23-2010, 12:07 AM
im not sure what you want (too early) but using a question mark passes a $_GET so if yo have
"<a href=\"pict_pic_page.php?value=" . $new . "\">"

you can use $_get['value'] on your target page

12-24-2010, 12:59 PM
Hi edbr - your pointer worked out - thanks.
Anyone observing will know what's coming.
The $_GET carries the variable and shows it in the status bar. Unfortunately, my directory system links writer ids with folders and files which contain various data. It doesn't take a great stretch of imagination to link what appears in the status bar with an id.
OK - I'm using $_GET to carry my variable. Is there another way to transfer a variable after clicking an image and moving to another script page which does not use $_GET ?
Seasonal greetings to all those who have helped me with my quest this year.

12-25-2010, 06:02 AM
you can use hidden form variables or sessions to move data around between pages.

look in the php manual (get the enhanced chm version and put a shortcut to it in your start menu) and look up session_start() and $_SESSION[], that should get you started. session_start() should occur before anything like HTML is output to the browser, so the first thing on the page should be <?php session_start(); ?> and you may want to also initialize the session variable like this:
if (!isset($_SESSION['someindex'])) {
$_SESSION['someindex'] = ''; //can't use true/false on session variables
if (isset($_POST['someindex']) && strlen($_POST['someindex']) <= 17) {
//take integer value of POST var.
//will probably have to use intval again when reading $_SESSION...
//if you want a real integer instead of garbage.
//it's a way of vaidating form input.
$_SESSION['someindex'] = intval($_POST['someindex']);

cookies are another method, although not everybody has cookies enabled in their browser.

for hidden form variables, you use a form like

<form action="zippy.php">
<input type="hidden" name="someindex" value="<?php echo $new; ?>">
<input type="submit">

then when you want to access it from php, $_POST['someindex']

12-25-2010, 08:06 AM
oh, I should have added, when you know your POST or SESSION is set, $new=$_POST['someindex'];

$_POST is set by the server. $_SESSION or $_COOKIE is set by YOU, and may start up in the page with a value already in it, or the variable may not be set.