PDA

View Full Version : PHP jump menu, cant send ID to next page.


tux
09-06-2007, 05:43 PM
Hi, After building a site for my camera club in ASP ( hosting restrictions ) they now want to make it more interactive. I have decided that I will rewrite the site in PHP.

I have changed the structure of the site so everything is now PHP pages, I've done all of DJs vids and have made a start on hand coding the gallery page.

I have hit my first hurdle as I cant get my jump menu to send an ID to the URL of the next page to display the chosen thumbnails. Once that is done I will need a thumbnail link to a further page to show the full image.

My code so far is as follows....


<?php
require_once("connections/connection.php"); // database connection
///////////////////////////////////////////
$photographerID = $_POST['photographerID'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
///////////////////////////////////////////
///////////////////////////////////////////
$query = sprintf("SELECT * FROM photographers");
$result = @mysql_query($query);
$photographerlist = @mysql_fetch_array($result);
///////////////////////////////////////////
?>


and the form is......


<form id="menu"
method="post" action="gallery_detail.php?photographerID=<?php echo $photographerlist['photographerID']; ?>">
<div align="right">
<select name="menu" class="photographerlist" onChange="document.forms[0].submit()">
<option value="">Please choose a photographer</option>
<?php do { ?>
<option value="<?php echo $photographerlist['photographerID']; ?>"><?php echo $photographerlist['firstname']; ?> <?php echo $photographerlist['lastname']; ?></option>
<?php }while ($photographerlist = mysql_fetch_array($result)); ?>
</select>
</div>
</form>


If anyone could point me in the right direction that would be great. I want to master PHP as I have more sites to build in the future. Thanks.

Oh! A future question I need answering too is how to get my MySQL database to the server. I've used Access before and just uploaded the file. I use EMS now so how do you go about going live?

Thanks again all, regards

Paul

tux
09-06-2007, 05:50 PM
Oh!... The script does send the ID to the URL but only the first one it finds in the DB. Not sure if this helps.

davidj
09-07-2007, 09:18 AM
ok i can see what your doing here but your logic is wrong

first in your form just pass the form back to its self by setting the action to the page your on and catch the value of the dropdown in a $_POST

set the $_POST value to a $variable and do a...


header("redirect:gallery_detail.php?photographerID=$variab le");
exit;


make sure to wrap the header in an IF to check that the $variable contains something because thats how you would know the form was submitted

tux
09-07-2007, 09:51 AM
Thanks David, at work now but will give it a go in my break as have my laptop with me. Could you give some thought to how the thumbs will be displayed on the detail page. Should I send the image ID as a variable too. If so how do I associate the two as they are in different tables. Both tables have the photographer ID.

tux
09-07-2007, 10:27 AM
Ok, I've added....


$menu = $_POST['menu'];


...to the variables and the header with the IF....


if ($menu) {
header("redirect:gallery_detail.php?photographerID=$menu");
exit;
}


...above the closing php tag. I changed the action to..


"<php $SERVER['PHP_SELF']; ?>"


I know I have something wrong as the page loads but when I select from the drop down the page reloads with just white space????

What have I missed?

PS: Cant send code from laptop as using works internet connection for forum.

davidj
09-07-2007, 11:34 AM
will need to see all your code

tux
09-07-2007, 11:46 AM
Okay, have to wait til I get home. I'll have a play in my lunch break and see if I can suss it myself. I'll post again when I get home with the code. Thanks.

tux
09-07-2007, 05:38 PM
No joy at lunch so here is the code....


<?php
require_once("connections/connection.php"); // database connection
///////////////////////////////////////////
if ($menu){
header("redirect:gallery_detail.php?photographerID=$menu");
}
///////////////////////////////////////////
///////////////////////////////////////////
$photographerID = $_POST['photographerID'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$menu = $_POST['menu'];
///////////////////////////////////////////
///////////////////////////////////////////
$query = sprintf("SELECT * FROM photographers");
$result = @mysql_query($query);
$photographerlist = @mysql_fetch_array($result);
///////////////////////////////////////////
?>


and the form....

<form id="menu" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<select name="menu" class="photographerlist" onChange="document.forms[0].submit()">
<option value="">Please choose a photographer</option>
<?php do { ?>
<option value="<?php echo $photographerlist['photographerID']; ?>"><?php echo $photographerlist['firstname']; ?> <?php echo $photographerlist['lastname']; ?></option>
<?php }while ($photographerlist = mysql_fetch_array($result)); ?>
</select>
</form>

The page just reloads when a selection is made from the list and does not redirect to the detail page.

davidj
09-08-2007, 09:00 AM
ok

question...

how can you resolve your header redirect variable when your $_POST / $menu is written after the header is called????

you have to understand the logic when writing PHP.

you have this...


if ($menu){
header("redirect:gallery_detail.php?photographerID=$menu");
}


then after you have this...


$menu = $_POST['menu'];


so your header function can never resolve the $menu variable as the variable is called after.

also you should always add an exit after your header as this will stop any other code running and again is best practice


if ($menu){
header("redirect:gallery_detail.php?photographerID=$menu");
exit;
}

tux
09-08-2007, 09:18 AM
I had noticed that I'd missed off the exit after I posted last.

I have moved the header code to the bottom and now when a selection is made the page reloads to itself but is just white and does not redirect to the detail page??

What is it I'm missing here.

davidj
09-08-2007, 09:21 AM
post the code again as you have it

tux
09-08-2007, 09:26 AM
Code


<?php
require_once("connections/connection.php"); // database connection
///////////////////////////////////////////
$photographerID = $_POST['photographerID'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$menu = $_POST['menu'];
///////////////////////////////////////////
///////////////////////////////////////////
$query = sprintf("SELECT * FROM photographers");
$result = @mysql_query($query);
$photographerlist = @mysql_fetch_array($result);
///////////////////////////////////////////
if ($menu){
header("redirect:gallery_detail.php?photographerID=$menu");
exit;
}
?>


Form

<form id="menu"
method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<select name="menu" class="photographerlist" onChange="document.forms[0].submit()">
<option value="">Please choose a photographer</option>
<?php do { ?>
<option value="<?php echo $photographerlist['photographerID']; ?>"><?php echo $photographerlist['firstname']; ?> <?php echo $photographerlist['lastname']; ?></option>
<?php }while ($photographerlist = mysql_fetch_array($result)); ?>
</select>
</form>

davidj
09-08-2007, 09:34 AM
everything looks fine

you do have a page called gallery_detail.php yes?

i don't understand why this is happening! its as if the exit is terminating the script prematurely. You dont have a rogue exit in your script

tux
09-08-2007, 09:43 AM
Yes, the detail page is just a copy of the gallery page at the moment.

When I remove the header code the page just reloads as you wold expect so it must be to do with that?? But what??

What do yo mean by rogue exit??

tux
09-08-2007, 09:46 AM
The blank gallery page has everything stripped outof it when it reloads. The source code is..

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252"></HEAD>
<BODY></BODY></HTML>

tux
09-08-2007, 09:48 AM
without the exit in the script it just reloads to gallery.php so it must be the exit causing it

davidj
09-08-2007, 09:49 AM
this looks like its working

are you sure that you are loading the correct page

you are loading a page which has HTML in it which indicates its loading a page which exists

davidj
09-08-2007, 09:52 AM
Errr

just noticed something!!!!!!!


arnt you suposed to be passing $photographersID and not $menu

tux
09-08-2007, 10:00 AM
I thought that $menu was taking the value from the form!! and $photographerID was populating it!! I change it and give it a go.

tux
09-08-2007, 10:03 AM
Made no diffence!!

davidj
09-08-2007, 10:18 AM
i am going to have to replicate your system

this may take some time

tux
09-08-2007, 10:28 AM
Okay, so I was thinking it was the select name ( menu ) in the form that was generating the value ( photographerID ) when it is submitted, so that would be the variable that I needed to collect. Using $photographerID reloads the page but doesnt redirect. If I use the $menu it must be empty so the script exits the whole page.

How do I collect the value from the form and create a variable from it??

tux
09-08-2007, 12:38 PM
Still scratching my head but have come up with this....

I created a new page called menu.php with only this code...


<?php
header("Location: gallery_detail.php?photographerID=$menu");
?>


I then changed the action of the form to.....


<form id="menu"
method="post" action="menu.php">
<select name="menu" class="photographerlist" onChange="document.forms[0].submit()">
<option value="">Please choose a photographer</option>
<?php do { ?>
<option value="<?php echo $photographerlist['photographerID']; ?>"><?php echo $photographerlist['firstname']; ?> <?php echo $photographerlist['lastname']; ?></option>
<?php }while ($photographerlist = mysql_fetch_array($result)); ?>
</select>
</form>


...and removed the header code.

Now when you select from the menu it reads menu.php and redirects to the detail page but with no photographerID in the URL just this

gallery_detail.php?photographerID=

Am I close and can you help me get that flamin' ID

:o

davidj
09-08-2007, 12:44 PM
i have just wrote this


$id = $_POST['model'];

if($id){

header("location:page_main.php?id=$id");
exit;

}



<select name="model" onchange="document.forms[0].submit()">
<option value="">Select Model</option>
<?php do { ?>
<option value="<?php echo $rowModel['car_model_id']; ?>"><?php echo $rowModel['car_model']; ?></option>
<?php } while ($rowModel = mysql_fetch_array($result)); ?>
</select>


and this works for me

tux
09-08-2007, 01:13 PM
Right, it nearly works. I moved the


$menu = $_POST['menu'];


below the DB query like this...


<?php
require_once("connections/connection.php"); // database connection
///////////////////////////////////////////
$photographerID = $_POST['photographerID'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
///////////////////////////////////////////
///////////////////////////////////////////
$query = sprintf("SELECT * FROM photographers");
$result = @mysql_query($query);
$photographerlist = @mysql_fetch_array($result);
///////////////////////////////////////////
$menu = $_POST['menu'];
if($menu){
header("Location: gallery_detail.php?photographerID=$menu");
exit;
}
?>


It now redirects okay but no matter which photographer you choose from the list the ID sent to the URL is always the same, the first one in the Database.

davidj
09-08-2007, 01:17 PM
what values have you set in your dropdown?

where you have...
$menu = $_POST['menu'];

try this


$menu = $_POST['menu'];
echo $menu;


remember to comment out your header() function as we dont want it to redirect we just want to see whats being passed

tux
09-08-2007, 01:45 PM
The values are taken from the DB starting from 2000 then auto increment. Adding the echo $menu; prints 2000 to the screen but with a warning....

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\Focus\gallery.php:17) in C:\wamp\www\Focus\gallery.php on line 20

davidj
09-08-2007, 01:52 PM
the warning is ok

ok your passing the id 2000

does this get passed no matter what option you pick from your dropdown?

you should have a different value for every option in the dropdown and you should see these values if you view source. Check to see if they are different in your html

tux
09-08-2007, 01:55 PM
Not sure if I have explained whats wrong correctly. Whats happening, the gallery.php works fine and sends the ID to the gallery_detail.php page. But when you choose from the menu on the detail page, the page reloads but only sends 2000 to the URL. ( the first entry in the DB )

I saved the gallery page as the detail page so the code in each is the same.

Is this clearer!!

tux
09-08-2007, 04:09 PM
The source code shows that the 10 photographers in the list have a different ID each, 2000 to 2010. But it only sends the ID 2000 no matter which one you choose??

davidj
09-08-2007, 09:10 PM
zip up your files and upload them and ill look at the code

tux
09-08-2007, 09:26 PM
I've got the drop menu working on all pages now David. Not sure what was wrong but I have it working. I've started to write the code to display the thumbnails. I'm sure I will need some guidance. I will see how far I can get before I come unstuck. I'm sure I will need some help as the thumbnails are to be displayed in 3 columns of 3. So column 1 will have images 1,2 & 3 and column 2 images 4,5 & 6 etc. See how I get on. Thanks for your help so far David, I appreciate it.