0

I have two php pages and I want to pass variable value using URL while click on a hyperlink.

Code snip 1: movie1-rev01.php

<html>
<head>
<title>Find my Favorite Movie!</title>
</head>
<body>
<?php
echo "<a href='moviesite-rev03.php?favmovie=Stripes'>Click here to see movie</a>";
?>
</body>
</html>

Code snip 2: moviesite-rev03.php

<html>
<head>
<title>My Movie Site - <?php echo $favmovie; ?>
</title>
</head>
<body>
<?php
echo "My favorite movie is ";
echo $favmovie;
echo "<br>";
$movierate = 5;
echo "My movie rating for this movie is: ";
echo $movierate;
?>
</body>
</html>

When I click on the link on page movie1-rev01.php then it shows an error message "Undefined variable: favmovie in C:\wamp\www\solutions\moviesite-rev03.php"

Please help me out. I'm new in PHP.

4
  • Use $_GET['favmovie']. Commented Jun 2, 2014 at 6:03
  • Use isset($_GET['favmovie']) ? $_GET['favmovie'] : ''. Commented Jun 2, 2014 at 6:05
  • @SomadderAbhijit if someone did helped you on this question, maybe you could repay them an upvote atleast or accept an answer Commented Jun 2, 2014 at 7:45
  • Okay. I'll do the same. But how to do it? Commented Jun 2, 2014 at 8:14

5 Answers 5

1

You can do the same by

$_REQUEST['favmovie'] or $_GET['favmovie']

ie.

define some variable,
$va = $_REQUEST[''favmovie];
instead of  echo $favmovie;
Sign up to request clarification or add additional context in comments.

1 Comment

@ankit format your code properly so that code is understandable.
0

You are trying to access a GET variable in the second file.
Its not defined as $favmovie but $_GET['favmovie'].

Comments

0

You have to assign the get value to $favmovie;

Put

$favmovie = $_GET['favmovie'];

or

$favmovie = $_REQUEST['favmovie'];

at the top of the page or anywhere before using $favmovie

Comments

0

You have to use $_GET to fetch the url variables.

So, in your moviesite-rev03.php. Do like this:

<html>
<head>
<?php
   $favmovie = $_GET['favmovie'];
?>
<title>My Movie Site - <?php echo $favmovie; ?>
</title>
</head>
<body>
<?php
echo "My favorite movie is ";
echo $favmovie;
echo "<br>";
$movierate = 5;
echo "My movie rating for this movie is: ";
echo $movierate;
?>
</body>
</html>  

Also, for a side note: If you are planning to use this variable for database query purpose, I suggest to you to look into removing sql injection

Comments

0

First need to get variables that exists in url.. for that PHP provides several methods. In your case you can use GET.

you can use like this :

$favmovie = $_GET['favmovie'];

this will store value from variable(favmovie) in url and store it to $favmovie in that page.. now you can use $favmovie as you wish.

to display in page. use echo $favmovie; or echo $_GET['favmovie'];

for more info: http://www.php.net/manual/en/reserved.variables.get.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.