Jump to content

[SOLVED] Passing values through the URL


Recommended Posts

Hi

 

I'm trying to pass values between web pages using php and have come across a problem.  On the first page, when a user clicks on the 'Photos' link, I want the photo albums to display in the content section of the page.  To make sure  the correct albums are displayed I want to pass a value to search the database where my photos are stored.  The value I want to search the database on is passed from a previous web page.  This value is $getValue. 

Is the way I'm doing this wrong?????

 

This is the code for this page..

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<<?php 
$getValue = $_GET['place'];
?>
<title>My Travel Website</title>
<link href="americanStyle.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="wrapper">
<div id="americaHeader">
             .........
</div>

<div id="menu">
	<ul>
		<li><a href="#">Home</a></li>
		<li><?php echo"<a href=America1.php?page=main>"; ?>Photos</a></li>
		<li><a href="#">Sights</a></li>
		<li><a href="#">Dos & Donts</a></li>
	</ul>
</div>

<div id="page">
	<div id="content">

	<?php


                         $index = array( "main" => "DisplayAlbums.php?title=$getValue" , "photo" => "DatabaseConnectionBackup.php" ); 

                         if ( !empty( $_GET["page"] ) ) { 
                               $page = $_GET["page"]; 

                         if ( array_key_exists( $page , $index ) ) 
                               include( $index[$page] ); 
                          ?> 

	</div>	
</div>
</div>
</body>
</html>

 

Thank you.

Link to comment
Share on other sites

It doesn't work.

 

thats a pretty broad statement. What is it that isn't working? Are you not receiving the data passed via the URL? Are you not getting the right values out of the database when you query it?

 

Be specific so we can help you!

Link to comment
Share on other sites

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
error_reporting(E_ALL);

ini_set('display_errors', 1);$getValue = $_GET['place'];
?>
<title>My Travel Website</title>
<link href="americanStyle.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="wrapper">
   <div id="americaHeader">
             .........
   </div>
   
   <div id="menu">
      <ul>
         <li><a href="#">Home</a></li>
         <li><?php echo"<a href=America1.php?page=main>"; ?>Photos</a></li>
         <li><a href="#">Sights</a></li>
         <li><a href="#">Dos & Donts</a></li>
      </ul>
   </div>

   <div id="page">
      <div id="content">
                  
      <?php
         

                         $index = array( "main" => "DisplayAlbums.php?title=$getValue" , "photo" => "DatabaseConnectionBackup.php" ); 

                         if ( !empty( $_GET["page"] ) ) { 
                               $page = $_GET["page"]; 

                         if ( array_key_exists( $page , $index ) ) 
                               include( $index[$page] );
                         } //You missed the closing brace here...
                          ?> 
            
      </div>   
   </div>
</div>
</body>
</html>

 

There was a < tag added before the actual <?php. Is that the problem as it may be treated as an unclosed tag and display nothing.

Link to comment
Share on other sites

There's a number of obvious issues with the code you've presented.  The way we work here, is that we require the people asking questions to help narrow things down to the pertinent area.   

 

In reading your original post, you have this line:

 

  </pre>
<li>"; ?>Photos</li

 

If as you say, you want to pass that along to the Photos URL using a GET parameter, then you'd need to have that parameter as part of the URL.  Perhaps that's part of the problem?

 

Later you have this code:

 

   $index = array( "main" => "DisplayAlbums.php?title=$getValue" , "photo" => "DatabaseConnectionBackup.php" ); 

 

You then use this array to do an include.  The include needs a local file, which "DisplayAlbums.php?title=$getValue" is not.  So if the code executes for page=main, then this is not going to work.  If your code is in the file DisplayAlbums.php, then the $getValue variable will be available to it, because an include simply inserts the code of the included file into the current script at the point it is called.  All script variables will be seen by that script, so it's free to reference $getValue in queries.  We don't have the code to analyze of course.

 

Link to comment
Share on other sites

Thanks everyone who posted constructive comments.  I understand that I didn't explain my problem very well, I find it hard to do without talking to someone face to face.  I've made a much better attempt this time.

I've attached 2 screen shots (sorry I dont have the website up and working yet so cant provide a link)to try and explain what I'm trying to do.  I'm hoping this will make sense.

screen1.jpg is my web page where the user can select which city they want to view.  At the minute i have my website set up so that when a user clicks on one of the links, eg. San Francisco, this is the code.

 

<?php echo "<a href=America1.php?place=sanfrancisco>"; ?><h3>San Francisco</h3></a>

 

The full code behind screen1.jpg is....

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Language" content="zh">

<title>My Travel Website</title>
<link href="americanStyle.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="wrapper">
<div id="americaHeader">
	<div id="logo">
		<h1><a href="#">My American Adventures</a></h1>
	</div>
</div>
<div id="menu">
	<ul>
		<li><a href="#">Home</a></li>
		<li><a href="#">America</a></li>
		<li><a href="#">Europe</a></li>
		<li><a href="#">Asia</a></li>
	</ul>
</div>
<div id="page">
	<div id="content">
		<div id="piclinks">
			<table border="0" cellpadding="5" width="300">
			<tr>
				<td><img src="images/sanFranThumb.jpg" align="middle"/></td>
				<td><?php echo "<a href=America1.php?place=sanfrancisco>"; ?><h3>San Francisco</h3></a> </td>
			</tr>
			<tr>
				<td><img src="images/memphisThumb.jpg" align="middle"/></td>
				<td><a href="#"><h3>Memphis</h3></a> </td>
			</tr>
			<tr>
				<td><img src="images/nashvilleThumb.jpg" align="middle"/> </td>
				<td><a href="#"><h3>Nashville</h3></a></td>
			</tr>
			<tr>
				<td><img src="images/newyorkThumb.jpg" align="middle"/></td>
				<td><a href="#"><h3>New York</h3></a></td>
			</tr>
			</table>
		</div>
	</div>
</div>

</div>
</body>
</html>

 

 

 

When the link is clicked, another page will open.  This is screen2.jpg that I've attached.  So I'm trying to pass the name of the city that has been selected previously to this page so I can display the appropriate info. 

So now on this page, say for example San Francisco was selected, when the user clicks on 'Photos' on the menu only photo albums (stored in a database) from San Francisco will be displayed.

On this page I was trying to display, for example the Photos, in the content section of the page rather than a new webpage, that's why I was using the Include.

So the value that had been passed from screen1 I was trying to use to get, for e.g. San Franciscos, photo albums.  Searching the database on this value.

 

<?php echo"<a href=America1.php?page=main>"; ?>Photos</a>

 

When the user clicks this on the menu then from the code below the photos would be displayed in the content section.  This was the only way I could get this to work, by doing it this way.

 

$index = array( "main" => "DisplayAlbums.php?title=$getValue" , "photo" => "DatabaseConnectionBackup.php" ); 

if ( !empty( $_GET["page"] ) ) { 
   $page = $_GET["page"]; 

    if ( array_key_exists( $page , $index ) ) 
        include( $index[$page] ); 

 

My intentions were then when I tried to call 'DisplayAlbums.php' to show the photo albums in the content section, to pass $getValue which would store the city that the user had selected to view.  And from DisplayAlbums.php then I could search my database on this value.

 

The code between screen2 is.....

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Language" content="zh">
<?php 
$getValue = $_GET['place'];
?>
<title>My Travel Website</title>
<link href="americanStyle.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="wrapper">
<div id="americaHeader">
	<div id="logo">
		<h1><a href="#">My American Adventures</a></h1>
	</div>
</div>
<div id="menu">
	<ul>
		<li><a href="#">Home</a></li>
		<li><?php echo"<a href=America1.php?page=main>"; ?>Photos</a></li>
		<li><a href="#">Sights</a></li>
		<li><a href="#">Dos & Donts</a></li>
	</ul>
</div>

<div id="page">
	<div id="content">

		<?php

// build an array for the pages you have. 
$index = array( "main" => "DisplayAlbums.php?title=San" , "photo" => "DatabaseConnectionBackup.php" ); 

if ( !empty( $_GET["page"] ) ) { 
   $page = $_GET["page"]; 

    if ( array_key_exists( $page , $index ) ) 
        include( $index[$page] ); 

?> 		
	</div>

</div>

</div>
</body>
</html>

 

 

Also to lonewolf, I tried your suggestion ...

<?php

echo "<pre>";

print_r($_GET);

echo "</pre>";

 

and this was the result...

Array

(

)

 

 

 

I'm still very new to php and trying to find my way, so I know that I'm probably doing this all wrong.  I would appreciate any suggestions or ideas you would have to make this work.  I'm sure there's a much better way than what I'm trying.

 

Thank you.

 

[attachment deleted by admin]

Link to comment
Share on other sites

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Language" content="zh">

<title>My Travel Website</title>
<link href="americanStyle.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="wrapper">
 <div id="americaHeader">
 	<div id="logo">
 		<h1><a href="#">My American Adventures</a></h1>
 	</div>
 </div>
 <div id="menu">
 	<ul>
 		<li><a href="#">Home</a></li>
 		<li><a href="#">America</a></li>
 		<li><a href="#">Europe</a></li>
 		<li><a href="#">Asia</a></li>
 	</ul>
 </div>
 <div id="page">
 	<div id="content">
 		<div id="piclinks">
 			<table border="0" cellpadding="5" width="300">
 			<tr>
 				<td><img src="images/sanFranThumb.jpg" align="middle"/></td>
 				<td><h3><a href="America1.php?page=main&place=sanfrancisco">San Francisco</a> </td>
 			</tr>
 			<tr>
 				<td><img src="images/memphisThumb.jpg" align="middle"/></td>
 				<td><h3><a href="America1.php?page=main&place=Memphis">Memphis</a></h3> </td>
 			</tr>
 			<tr>
 				<td><img src="images/nashvilleThumb.jpg" align="middle"/> </td>
 				<td><h3><a href="America1.php?page=main&place=NashVille">Nashville</a></h3></td>
 			</tr>
 			<tr>
 				<td><img src="images/newyorkThumb.jpg" align="middle"/></td>
 				<td><h3><a href="America1.php?page=main&place=NY">New York</a></h3></td>
 			</tr>
 			</table>
 		</div>
 	</div>
 </div>

</div>
</body>
</html>

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Language" content="zh">
<?php 
$getValue = $_GET['place'];
?>
<title>My Travel Website</title>
<link href="americanStyle.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="wrapper">
 <div id="americaHeader">
 	<div id="logo">
 		<h1><a href="#">My American Adventures</a></h1>
 	</div>
 </div>
 <div id="menu">
 	<ul>
 		<li><a href="#">Home</a></li>
 		<li><a href="America1.php?page=main">Photos</a></li>
 		<li><a href="#">Sights</a></li>
 		<li><a href="#">Dos & Donts</a></li>
 	</ul>
 </div>

 <div id="page">
 	<div id="content">
 	 
 		<?php
 	 
// build an array for the pages you have. 
$index = array( "main" => "DisplayAlbums.php" , "photo" => "DatabaseConnectionBackup.php" ); 

if ( !empty( $_GET["page"] ) ) { 
   $page = $_GET["page"];

 if ($page = "main")
 	$title = isSet( $getValue ) ? $getValue : 'SetDefault'; // $title can now be used in DisplayAlbums.php 

    if ( array_key_exists( $page , $index ) ) 
        include( $index[$page] ); 

?> 	 
 	</div>
 
 </div>

</div>
</body>
</html>

 

Try something along those lines, it will need changing a little to plugin the correct values.

Link to comment
Share on other sites

Thanks Andy-H for your help, that worked great.  There's just one problem I'm not sure how to deal with.  When I click on the 'San Francisco' link shown in screen1.jpg, screen2.jpg opens and automatically displays the photo albums.  I was hoping that when screen2 opens the content would be blank coz I later want to add a intro there and then when the user clicks on 'Photos' on the menu bar, then the photo albums of San Francisco would be displayed.  Then I would be able to do this for all my options in the menu bar.  For example, when a user clicks 'San Francisco' from screen1, screen2 opens a intro will appear (a separate html file) and then when a user clicks on 'Photos' from the menu bar, San Francisco photo albums will appear, when a user clicks 'Sights' from the menu bar a list of sights or info about sights in San Francisco will appear.

I'm sorry I should of made myself more clear.  I really appreciate your help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.