Jump to content

Archived

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

bilis_money

carrying $myvar with the image hyperlink?

Recommended Posts

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
[code]
<?php
  $myvar = 2;
 
  echo "<a href=\"file2.php\" >";
      echo "<img src=\"my_img.gif\" border=\"1\">";
  echo "</a>";
?>
[/code]

//and now this will display the image info and the $myvar value.
file2.php
[code]<?php
  echo $myvar;
  echo "<img src=\"my_img.gif\" border=\"1\">";
  ..... echo more info here ....
?>
[/code]
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.

Share this post


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

Share this post


Link to post
Share on other sites
thanks for the attention [b]wildteen[/b]

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.

Share this post


Link to post
Share on other sites
Do you want to pass $myvar through the hyperlink?

[code]<?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>";[/code]

Share this post


Link to post
Share on other sites
Two ways:
[b]1[/b]) The easy way (not the best way at all)- Pass it with url variables, this way:
[code=php:0]$myvar="something";
echo('<a href="nextpage.php?myvar='.$myvar.'"><img src=....></a>');
[/code]
And in the next page you can do:
[code=php:0]$myvar=$_GET['myvar'];[/code]
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).



[b]2[/b]) The better way- Sessions.
Sessions let you "save" variables.
(You can read tons of tutorials about sessions, I suggest you to start with [url=http://www.tizag.com/phpT/phpsessions.php]this one[/url]).
Baiscly, that's how the script will look:
[hr]
[code=php:0]//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>');
[/code]
[hr]

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


Orio.

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.