Forum Moderators: coopster

Message Too Old, No Replies

POST function not working right.

header error

         

chaddsisco

12:44 am on Sep 21, 2005 (gmt 0)

10+ Year Member



I have a drop down menu that when the user hit submit I want the action to take them to the page they selected but I keep getting a header error.

here is the page with the drop down menu

<html>
<head>
<title>What gallery would you like to view</title>
</head>
<body>
<?php
include ('includes/header.inc');
?>
<form name="form1" method="post" action="displaygallery.php"
enctype="multipart/form-data">

<table border="0" cellpadding="5">
<tr>
<td>What gallery would you like to view?<br>
<em>Example: Weddings</em></td>
<td><select name="displaygallery">
<option value="" selected>Select a location....</option>
<option value="http://www.example.com/gallery/centerpieces/gallery_centerpieces1.php">Center Pieces</option>
<option value="http://www.example.com/gallery/classicdecor/gallery_classicdecor1.php">Classic Decor</option>
<option value="http://www.example.com/gallery/dancefloors/gallery_dancefloors1.php">Dance Floors</option>
<option value="http://www.example.com/gallery/deliverybouquets/gallery_deliverybouquets1.php">Delivery Bouquets</option>
<option value="http://www.example.com/gallery/propsandeffects/gallery_propsandeffects1.php">Props and Effects</option>
<option value="stagedecor">Stage Decor</option>
<option value="themedecor">Theme Decor</option>
<option value="tradeshows">Trade Shows</option>
<option value="weddings">Weddings</option>
</select>
</td>
</tr>

</table>
<br>
<p align="center"><input type="submit" name="Submit" value="Submit">
&nbsp;
<input type="reset" name="Submit2" value="Clear Form">
</p>
</form>
</body>
</html>

<?php
include ('includes/footer.inc');
?>

Here is displaygallery.php

<?php
$displaygallery = $_POST['displaygallery'];

echo "Display: $displaygallery<br>";
header("location: $displaygallery");
?>

Any ideas why this would not work? Thanks!

[edited by: coopster at 2:43 pm (utc) on Sep. 22, 2005]
[edit reason] generalized url TOS [webmasterworld.com] [/edit]

BertieB

12:59 am on Sep 21, 2005 (gmt 0)

10+ Year Member



You may want to exemplify the URLs in your code if you still have the 'owner edit' button.

As for your error, off the top of my head, I'm guessing your header error is something along the lines of:

Warning: Cannot modify header information - headers already sent by...

This is because of the 2nd line in displaygallery.php

echo "Display: $displaygallery<br>";

With this, you are sending data to the client, and so a header response (of 200 OK most likely) has already been sent. Hence a location: header cannot be added (because that issues a 302 redirect).

Remove the echo... line and see if it works. If it doesn't, I apologise for it is 2 AM here :)

chaddsisco

1:38 am on Sep 21, 2005 (gmt 0)

10+ Year Member



When I remove that line then the error I get is that the page can not be display, any other ideas?

BertieB

9:42 am on Sep 21, 2005 (gmt 0)

10+ Year Member



>> page cannot display

Check the URL in the title bar, because displaygallery.php may be redirecting to a page that does not exist.

You could also // comment out the header("location: $displaygallery"); part, then re-add

echo "Display: $displaygallery";

To make sure $displaygallery is set to a valid URI. From the php website:

"HTTP/1.1 requires an absolute URI as argument to Location: including the scheme, hostname and absolute path, but some clients accept relative URIs."

kees mekhalel

9:49 am on Sep 21, 2005 (gmt 0)

10+ Year Member



I Found that problem in my Web site

this is the solution

replace the line:

header("location: $displaygallery");

with

echo "<script>location.href='$displaygallery'</script>";

I think this is the answer

BertieB

11:35 am on Sep 21, 2005 (gmt 0)

10+ Year Member



>> I think this is the answer

That solution will work, but only with browsers that have javascript enabled. The rest will see a blank page. To complete the solution chaddsisco could add:

echo("If you are not automatically redirected to $displaygallery, please <a href='$displaygallery'>click here</a>");

Even still, if using the php header function didn't work, chances are the URI set in $displaygallery is wrong. Using the header function to send a 302 redirect has the other advantage that it will automatically redirect even those without javascript enabled.

Although, if you want to go down the javascript route, you could make an onClick event handler for the submit button on the form page... but again you run into trouble when someone has javascript disabled.

Only my .02