Jump to content

[SOLVED] Header redirect issue


KevinM1

Recommended Posts

I have a form with two submit buttons.  One is supposed to take the user to the previous page, the other to the starting page.  The second button works fine, but the first also takes the user to the starting page.  I've looked over the code a few times and can't find the problem.  Below is my code.  The form in question is at the bottom, with the submit button named 'back' the source of the problem.  Please help.

[code]
<?php

#viewitem.php script

session_start();
ob_start();

include('../php_config/config.php');
include('../dbconnect.php');
include('../templates/sub_header.inc');

if(isset($_GET['cat'])){
  $tableName = $_GET['cat'];
}

else{
  $_SESSION['ip'] = urlencode(serialize($ip));
  $_SESSION['myCart'] = urlencode(serialize($myCart));
  header("Location: http://www.thinkingmachinestore.com/storefront.php");
  exit();
}

if(isset($_GET['id'])){
  $id = $_GET['id'];
}

else{
  $_SESSION['ip'] = urlencode(serialize($ip));
  $_SESSION['myCart'] = urlencode(serialize($myCart));
  header("Location: http://www.thinkingmachinestore.com/storefront.php");
  exit();
}

if(isset($_POST['back'])){
  $_SESSION['ip'] = urlencode(serialize($ip));
  $_SESSION['myCart'] = urlencode(serialize($myCart));
  header("Location: http://www.thinkingmachinestore.com/viewcat.php?cat={$tableName}");
  exit();
}

if(isset($_POST['restart'])){
  $_SESSION['ip'] = urlencode(serialize($ip));
  $_SESSION['myCart'] = urlencode(serialize($myCart));
  header("Location: http://www.thinkingmachinestore.com/storefront.php");
  exit();
}

$query = "SELECT * FROM $tableName WHERE {$tableName}_id = '$id'";
$result = mysql_query($query);

$row = mysql_fetch_assoc($result);
$description = nl2br($row['description']);
$src = $row['pic_url'];
$name = $row['name'];
$price = $row['price'];

//start outputting the product info

echo <<<EOT
  <div style='text-align: center;'><a href='checkout.php'><img src='images/store/storefront_02a.jpg' alt='' /></a><img src='../images/store/{$tableName}_banner.jpg' alt='' style='margin-top: 5px;' /><a href='viewcart.php'><img src='../images/store/storefront_02.jpg' alt='View Cart' /></a><br /><div id='leftside'>\n<img src='$src' alt='' />
  <br />\n<p style='text-align: left;'>$description</p></div>\n
EOT;

?>

<form name="iteminfo" action="addcart.php" method="POST" style="margin-left: auto; margin-right: auto; text-align: center;">
  Quantity: <input name="quantity" type="text" size="4" maxlength="4"/>
  <input type="submit" value="Put in cart" />
  <input name="name" type="hidden" value="<?php echo $name; ?>" />
  <input name="price" type="hidden" value="<?php echo $price; ?>" />
  <input name="tableName" type="hidden" value="<?php echo $tableName; ?>" />
  <input name="id" type="hidden" value="<?php echo $id; ?>" />
</form>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
  <input type="submit" name="back" value="Go Back" /><input type="submit" name="restart" value="Go Back to the Beginning" />
</form>

<?php

include('../templates/sub_footer.inc');

?>
[/code]
Link to comment
Share on other sites

[quote author=SemiApocalyptic link=topic=124014.msg513424#msg513424 date=1169753299]
Is $_GET['cat'] and $_GET['id'] set on the page that you're clicking the back button?
[/quote]

Yes, the viewcat.php script passes the cat and id values to viewitem.php via the GET method.

I tried explicitly passing the $tableName value retrieved by cat by using a hidden input, but it still hasn't helped.  I'll post my code below.  I did try giving my hidden tableName input a different name, in case there was a naming conflict, but that didn't help.
[code]
<?php

#viewitem.php script

session_start();
ob_start();

include('../php_config/config.php');
include('../dbconnect.php');
include('../templates/sub_header.inc');

if(isset($_GET['cat'])){
  $tableName = $_GET['cat'];
}

else{
  $_SESSION['ip'] = urlencode(serialize($ip));
  $_SESSION['myCart'] = urlencode(serialize($myCart));
  header("Location: http://www.thinkingmachinestore.com/storefront.php");
  exit();
}

if(isset($_GET['id'])){
  $id = $_GET['id'];
}

else{
  $_SESSION['ip'] = urlencode(serialize($ip));
  $_SESSION['myCart'] = urlencode(serialize($myCart));
  header("Location: http://www.thinkingmachinestore.com/storefront.php");
  exit();
}

if(isset($_POST['back'])){
  $_SESSION['ip'] = urlencode(serialize($ip));
  $_SESSION['myCart'] = urlencode(serialize($myCart));
  header("Location: http://www.thinkingmachinestore.com/viewcat.php?cat={$_POST['tableName']}");
  exit();
}

if(isset($_POST['restart'])){
  $_SESSION['ip'] = urlencode(serialize($ip));
  $_SESSION['myCart'] = urlencode(serialize($myCart));
  header("Location: http://www.thinkingmachinestore.com/storefront.php");
  exit();
}

$query = "SELECT * FROM $tableName WHERE {$tableName}_id = '$id'";
$result = mysql_query($query);

$row = mysql_fetch_assoc($result);
$description = nl2br($row['description']);
$src = $row['pic_url'];
$name = $row['name'];
$price = $row['price'];

//start outputting the product info

echo <<<EOT
  <div style='text-align: center;'><a href='checkout.php'><img src='images/store/storefront_02a.jpg' alt='' /></a><img src='../images/store/{$tableName}_banner.jpg' alt='' style='margin-top: 5px;' /><a href='viewcart.php'><img src='../images/store/storefront_02.jpg' alt='View Cart' /></a><br /><div id='leftside'>\n<img src='$src' alt='' />
  <br />\n<p style='text-align: left;'>$description</p></div>\n
EOT;

?>

<form name="iteminfo" action="addcart.php" method="POST" style="margin-left: auto; margin-right: auto; text-align: center;">
  Quantity: <input name="quantity" type="text" size="4" maxlength="4"/>
  <input type="submit" value="Put in cart" />
  <input name="name" type="hidden" value="<?php echo $name; ?>" />
  <input name="price" type="hidden" value="<?php echo $price; ?>" />
  <input name="tableName" type="hidden" value="<?php echo $tableName; ?>" />
  <input name="id" type="hidden" value="<?php echo $id; ?>" />
</form>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
  <input type="hidden" name="tableName" value="<?php echo $tableName; ?>" />
  <input type="submit" name="back" value="Go Back" /><input type="submit" name="restart" value="Go Back to the Beginning" />
</form>

<?php

include('../templates/sub_footer.inc');

?>
[/code]
Link to comment
Share on other sites

[quote author=ted_chou12 link=topic=124014.msg516134#msg516134 date=1170084565]
inorder to make my life easier, :) what is the url for:
1. the previous page
2. the back page
Ted
[/quote]

The url is for the back page.  The logical order is:

storefront.php (which is where the "Go Back to the Beginning" submit button is supposed to take the user) -> viewcat.php?cat=something (short for view category.  link is derived by the value passed to it by storefront.php via the GET method) -> viewitem.php?cat=something&id=somethingelse (cat and id obtained via GET... this is the problem script -- I want the "Go Back" submit button to bring the user back to viewcat.php, not storefront.php like it is currently).

As you can see, the "Go Back" button is supposed to use an HTML header to redirect the user back to viewcat.php?cat={$tableName}, but instead it's bring me back to storefront.php.
Link to comment
Share on other sites

so there are two variables you want to pass from viewitem.php to viewcat.php, is that true? and you pass these two variables through the url. However, your back button is suppose to bring the user to viewcat.php from viewitem.php, is it true? correct me if i am wrong.
Ted
Link to comment
Share on other sites

[quote author=ted_chou12 link=topic=124014.msg516148#msg516148 date=1170085345]
so there are two variables you want to pass from viewitem.php to viewcat.php, is that true? and you pass these two variables through the url. However, your back button is suppose to bring the user to viewcat.php from viewitem.php, is it true? correct me if i am wrong.
Ted
[/quote]

There are two values that are passed from viewcat.php to viewitem.php -- the category name and the item id.  In order to go back one step (from viewitem.php back to viewcat.php), all that's needed is the category name (I call it $tableName in my script).  But, for some reason, that button is taking me back to storefront.php, almost as though both submit buttons are being checked by just one of my isset conditionals (if(isset($_POST['restart'])){...} instead of if(isset($_POST['back'])){...}).
Link to comment
Share on other sites

I hate to keep bumping this up, but the only thing stopping my site from going live is this error, which, for the life of me, I cannot solve.

To recap:

I have a script, named viewitem.php, that [i]should[/i] allow the user to move back one step to a script named viewcat.php.  This navigation is performed by a small, simple form at the bottom of viewitem.php.  The problem is, rather than bring the user to viewcat.php, it instead brings the user to the startpage.  I'm thinking that this may be caused by viewcat.php's automatic redirection if the get part of its url is not set, but I explicitly make sure that viewitem.php's header call includes the category value that viewcat.php expects.  I'm going to post both scripts below.

viewitem.php:
[code]
<?php

#viewitem.php script

session_start();
ob_start();

include('../php_config/config.php');
include('../dbconnect.php');
include('../templates/sub_header.inc');

if(isset($_GET['cat'])){
  $tableName = $_GET['cat'];
}

else{
  $_SESSION['ip'] = urlencode(serialize($ip));
  $_SESSION['myCart'] = urlencode(serialize($myCart));
  header("Location: http://www.thinkingmachinestore.com/");
  exit();
}

if(isset($_GET['id'])){
  $id = $_GET['id'];
}

else{
  $_SESSION['ip'] = urlencode(serialize($ip));
  $_SESSION['myCart'] = urlencode(serialize($myCart));
  header("Location: http://www.thinkingmachinestore.com/");
  exit();
}

if(isset($_POST['back'])){ //<-- the condition that doesn't seem to be working
  $_SESSION['ip'] = urlencode(serialize($ip));
  $_SESSION['myCart'] = urlencode(serialize($myCart));
  header("Location: http://www.thinkingmachinestore.com/viewcat.php?cat={$_POST['tableName2']}");
  exit();
}

if(isset($_POST['restart'])){
  $_SESSION['ip'] = urlencode(serialize($ip));
  $_SESSION['myCart'] = urlencode(serialize($myCart));
  header("Location: http://www.thinkingmachinestore.com/");
  exit();
}

$query = "SELECT * FROM $tableName WHERE {$tableName}_id = '$id'";
$result = mysql_query($query);

$row = mysql_fetch_assoc($result);
$description = nl2br($row['description']);
$src = $row['pic_url'];
$name = $row['name'];
$price = $row['price'];

//start outputting the product info

echo <<<EOT
  <div style='text-align: center;'><a href='checkout.php'><img src='images/store/storefront_02a.jpg' alt='' /></a><img src='../images/store/{$tableName}_banner.jpg' alt='' style='margin-top: 5px;' /><a href='viewcart.php'><img src='../images/store/storefront_02.jpg' alt='View Cart' /></a><br /><div id='leftside'>\n<img src='$src' alt='' />
  <br />\n<p style='text-align: left;'>$description</p></div>\n
EOT;

?>

<form name="iteminfo" action="addcart.php" method="post" style="margin-left: auto; margin-right: auto; text-align: center;">
  Quantity: <input name="quantity" type="text" size="4" maxlength="4"/>
  <input type="submit" value="Put in cart" />
  <input name="name" type="hidden" value="<?php echo $name; ?>" />
  <input name="price" type="hidden" value="<?php echo $price; ?>" />
  <input name="tableName" type="hidden" value="<?php echo $tableName; ?>" />
  <input name="id" type="hidden" value="<?php echo $id; ?>" />
</form>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <?php // <-- the form in question ?>
  <input type="hidden" name="tableName2" value="<?php echo $tableName; ?>" />
  <input type="submit" name="back" value="Go Back" /><input type="submit" name="restart" value="Go Back to the Beginning" />
</form>

<?php

include('../templates/sub_footer.inc');

?>
[/code]

viewcat.php:
[code]
<?php

#viewcat.php script

session_start();
ob_start();

include('../php_config/config.php');
include('../dbconnect.php');
include('../templates/sub_header.inc');

if(isset($_GET['cat'])){
  $tableName = $_GET['cat'];
}

else{ // <-- the segment of code that redirects the user back to the start if, for some reason, they don't select a category to view
  $_SESSION['ip'] = urlencode(serialize($ip));
  $_SESSION['myCart'] = urlencode(serialize($myCart));
  header("Location: http://www.thinkingmachinestore.com/");
  exit();
}

if(isset($_POST['submit'])){
  $_SESSION['ip'] = urlencode(serialize($ip));
  $_SESSION['myCart'] = urlencode(serialize($myCart));
  header("Location: http://www.thinkingmachinestore.com/");
  exit();
}

$query = "SELECT * FROM $tableName WHERE availability='y'";
$result = mysql_query($query);

echo "<div style='margin-left: auto; margin-right: auto; text-align: center;'><a href='checkout.php'><img src='images/store/storefront_02a.jpg' alt='' /></a><img src='../images/store/{$tableName}_banner.jpg' alt='' style='margin-top: 5px;' /><a href='viewcart.php'><img src='../images/store/storefront_02.jpg' alt='View Cart' /></a>\n<br /><br />\n";

$count = 0;

while($row = mysql_fetch_assoc($result)){
  $id = $row["$tableName" . "_id"];
  $pic = $row["pic_url"];
  echo "<a href='viewitem.php?cat=$tableName&id=$id'><img src='$pic' alt='' /></a>";
  $count++;

  if($count == 2){
      echo "<hr /><br />\n";
      $count = 0;
  }
}

?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" name="submit" value="Go Back" />
</form></div>

<?php

include('../templates/sub_footer.inc');

?>
[/code]

Like I said above, I wouldn't think that viewcat.php's redirect would be invoked since I explicitly set a category in my header call in viewitem.php.  That said, I can't see anything else that would be causing this problem.

Please help.
Link to comment
Share on other sites

If you haven't gotten it yet, I think you should have a 3 page system.

Page 1 - Display the form
Page 2 - Process buttons, inputs, and redirect
Page 3 - Final display page

Page 1 you should understand
Page 2 the user will/may never see this page it decides what button was pressed, and if previous page was selected it will do a heder of what the previous page was if the first button is selected a header of the first page will take place.
page 3 will display the correct page.
Link to comment
Share on other sites

[quote author=The Little Guy link=topic=124014.msg518247#msg518247 date=1170272194]
If you haven't gotten it yet, I think you should have a 3 page system.

Page 1 - Display the form
Page 2 - Process buttons, inputs, and redirect
Page 3 - Final display page

Page 1 you should understand
Page 2 the user will/may never see this page it decides what button was pressed, and if previous page was selected it will do a heder of what the previous page was if the first button is selected a header of the first page will take place.
page 3 will display the correct page.
[/quote]

Actually, it looks like my script was choking because I had two forms on the same page.  One was processed by a separate script, and the other, which I used for the navigation, was a sticky form.  By meshing the two forms together in my viewitem.php script and having the other script handle it, everything started working properly.
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.