Jump to content

carrying $myvar with the image hyperlink?


bilis_money

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.
Link to comment
https://forums.phpfreaks.com/topic/13380-carrying-myvar-with-the-image-hyperlink/
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.

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.

Archived

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

×
×
  • Create New...

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.