Jump to content


Photo

carrying $myvar with the image hyperlink?


  • Please log in to reply
5 replies to this topic

#1 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 01 July 2006 - 11:35 AM

hi, php gurus.

I hope you could help me with this puzzle?

let say i have 2 files.

This will display the hyperlink image and assign the value of the $myvar.
file1.php
<?php
  $myvar = 2;
  
  echo "<a href=\"file2.php\" >";
      echo "<img src=\"my_img.gif\" border=\"1\">";
  echo "</a>";
?>

//and now this will display the image info and the $myvar value.
file2.php
<?php
  echo $myvar;
  echo "<img src=\"my_img.gif\" border=\"1\">";
  ..... echo more info here ....
?>
now how would i do this?
I need your advice what should i do with this? to be able to redirect
to file2.php and display the image info and carry the $myvar value then
display too?

thank you very much in advance.


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 01 July 2006 - 11:47 AM

If its the hyperlink and the variable are stored in file1.php then use include where you need to show the image and use the variable.

#3 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 01 July 2006 - 11:56 AM

thanks for the attention wildteen

i think my explaination was misleading.

ok, let me clear my story again.

What i want to do here is to click the image in file1.php then
at that moment the $myvar should carry the value into file2.php.

so that i can use the $myvar again into file2.php

now my question is should i  use session here now to do this?

thank you again very much in advance.

-I hope i could help people in need with their php problems too.
-maybe in the future.



#4 scraptoft

scraptoft
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 01 July 2006 - 12:36 PM

Do you want to pass $myvar through the hyperlink?

<?php
  $myvar = 2;
  
  echo "<a href=\"file2.php\" >";
      echo "<a href="file2.php?myvar=value"><img src=\"my_img.gif\" border=\"1\"></a>";
  echo "</a>";


#5 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 01 July 2006 - 12:37 PM

Two ways:
1) The easy way (not the best way at all)- Pass it with url variables, this way:
$myvar="something";
echo('<a href="nextpage.php?myvar='.$myvar.'"><img src=....></a>');
And in the next page you can do:
$myvar=$_GET['myvar'];
To get the var.
Why is this bad?
a) People can change the value of myvar to whatever they want.
b) People can see what myvar contains (in case its a password, a credit card number etc', this is NOT recomended).



2) The better way- Sessions.
Sessions let you "save" variables.
(You can read tons of tutorials about sessions, I suggest you to start with this one).
Baiscly, that's how the script will look:
 
//This is page1.php
session_start(); //Make sure this is at the VERY begining of the page
//more html stuff, tags etc'
$myvar="something";
$_SESSION['myvar']=$myvar;
echo('<a href="nextpage.php"><img src=....></a>');
 

 
//this is nextpage.php
session_start(); //Make sure this is at the VERY begining of the page
$myvar=$_SESSION['myvar'];
echo $myvar;
 


Orio.
Think you're smarty?

(Gone until 20 to November)

#6 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 01 July 2006 - 12:59 PM

Thank you for your worthy reply Orio.

your explaination really make sense. thanks.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users