By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,709 Members | 1,603 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,709 IT Pros & Developers. It's quick & easy.

PHP redirect after posting

P: 64
Hi guys,

I have a couple of php files that perform various tasks.

I will use fields in my system and provide code as well
I need help as follows:

My database contains the fields Category and Brand
I need to make some changes on a a number of brands in a Category

ie the Category is "LADIES" and has about 700 brands

I make a search of a Category, it lists the results.
I make my change of the first brand, and will want it to redirect to the results page without losing the search criteria.

For now I redirect it to the index page.

How do I achieve this?

Is there a script I can put after my posting script to redirect the page without losing the search criteria?

Or how do I re-organise my files?

The files following;

The index page
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. require_once '../../functions.php';
  3.  
  4. $_SESSION['login_return_url'] = $_SERVER['REQUEST_URI'];
  5. checkUser();
  6.  
  7. $view = (isset($_GET['view']) && $_GET['view'] != '') ? $_GET['view'] : '';
  8.  
  9. switch ($view) {
  10.    case 'list' :
  11.       $content    = 'list.php';      
  12.       $pageTitle    = 'Fragrance Lounge - Stock panel';
  13.       break;
  14.  
  15.    case 'add' :
  16.       $content    = 'add.php';      
  17.       $pageTitle  = 'Fragrance Lounge - Add Stock ';
  18.       break;
  19.  
  20.    case 'modify' :
  21.       $content    = 'modify.php';      
  22.       $pageTitle    = 'Fragrance Lounge - Modify Stock';
  23.       break;
  24.  
  25.    case 'detail' :
  26.       $content    = 'detail.php';      
  27.       $pageTitle    = 'Fragrance Lounge - Showing Stock Details';
  28.       break;
  29.  
  30.    case 'showbybrand' :
  31.       $content    = 'showbybrand.php';      
  32.       $pageTitle    = 'Fragrance Lounge - Showing Stock By Brand';
  33.    break;
  34.  
  35.    case 'showbycategory' :
  36.       $content    = 'showbycategory.php';      
  37.       $pageTitle    = 'Fragrance Lounge - Showing Stock By Category';
  38.    break;
  39.  
  40.    case 'showbysearch' :
  41.       $content    = 'showbysearch.php';      
  42.       $pageTitle    = 'Fragrance Lounge - Showing Stock By Searching';
  43.    break;
  44.  
  45.    case 'stocksheet' :
  46.       $content    = 'printsheet.php';      
  47.       $pageTitle    = 'Fragrance Lounge - Printing stock sheet';
  48.    break;
  49.  
  50.    case 'add' :
  51.          $content    = 'add.php';      
  52.          $pageTitle    = 'Fragrance Lounge - Add Stock';
  53.    break;
  54.  
  55.    default :
  56.  
  57.       $content    = 'list.php';   //the default page which holds the search form   
  58.       $pageTitle    = 'Fragrance Lounge - Stock panel';
  59. }
  60.  
  61. $script  = array('stock.js');
  62.  
  63. require_once '../include/template.php';
  64. ?>
  65.  




The page with the search form

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (!defined('WEB_ROOT')) {
  3.    exit;
  4. }
  5. $rowsPerPage = 5;
  6.  
  7. $_SESSION['login_return_url'] = $_SERVER['REQUEST_URI'];
  8. checkUser();
  9.  
  10. $sql = "SELECT * FROM fragrancestock ORDER BY Brand ASC";
  11. $result     = dbQuery(getPagingQuery($sql, $rowsPerPage));
  12. $pagingLink = getPagingLink($sql, $rowsPerPage);
  13.  
  14. $errorMessage = (isset($_GET['cstock_error']) && $_GET['cstock_error'] != '') ? $_GET['cstock_error'] : '&nbsp;';
  15. ?>
  16. <form action="index.php?view=add" method="post"  name="frmList" id="frmList">
  17.   <table width="100%" border="0" align="center">
  18.     <tr class="title_text">
  19.       <td width="130">Category</td>
  20.       <td width="520">Brand</td>
  21.       <td width="130" align="center">Stock</td>
  22.       <td width="130"> Price </td>
  23.       <td width="66">Date Added </td>
  24.       <td width="66">Add</td>
  25.       <td width="66">Delete </td>
  26.     </tr>
  27.     <?php
  28. if (dbNumRows($result) > 0) {
  29.    $i = 0;
  30.  
  31.    while($row = dbFetchAssoc($result)) {
  32.       extract($row);
  33.  
  34.       if ($i%2) {
  35.          $class = 'row1';
  36.       } else {
  37.          $class = 'row2';
  38.       }
  39.  
  40.       $i += 1;
  41. ?>
  42.     <tr class="<?php echo $class; ?>">
  43.       <td width="130"><?php echo $Category; ?></td>
  44.       <td width="520"><?php echo $Brand; ?></td>
  45.       <td width="130" class="inner_border"><?php echo $Quantity; ?></td>
  46.       <td width="130" class="inner_border"><?php echo $SellingPrice; ?></td>
  47.       <td width="66"><?php echo $DateAddedStock; ?></td>
  48.       <td width="66"><a href="javascript:modifyStock(<?php echo $StockID; ?>);"><strong>Add
  49.         </strong> </a></td>
  50.       <td width="66"><a href="javascript:deleteStock(<?php echo $StockID; ?>);">Delete</a></td>
  51.     </tr>
  52.     <?php
  53.    } // end while
  54.  
  55.  
  56. ?>
  57.     <tr>
  58.       <td colspan="7" align="center">
  59.         <?php 
  60.    echo $pagingLink;
  61.    ?>      </td>
  62.     </tr>
  63.     <?php   
  64. } else {
  65. ?>
  66.     <tr>
  67.       <td colspan="7" align="center">No Stock Items Yet</td>
  68.     </tr>
  69.     <?php
  70. }
  71. ?>
  72.     <tr>
  73.       <td colspan="7">&nbsp;</td>
  74.     </tr>
  75.     <tr>
  76.       <td colspan="6" align="right"> <input name="btnAddStock" type="submit" value="New Entry" class="button_image">
  77.     <td align="right">    </tr>
  78.   </table>
  79. </form>
  80.  
  81. <label class="title_text">View Stock:</label>
  82. <hr>
  83. <table width="100%" border="0">
  84.   <tr>
  85.     <td><form action="index.php?view=showbybrand" method="post" name="frmByBrand">
  86.         <table width="100%" border="0">
  87.           <tr>
  88.             <td width="200">By Brand:</td>
  89.             <td width="200"><select name="sltBrand">
  90.                 <option value="0">Select Brand</option>
  91.                 <?
  92.          $sql = "SELECT  Brand FROM cstockitems ORDER BY Brand ASC";
  93.          $result = dbQuery($sql);      
  94.  
  95.          if(dbNumRows($result))
  96.          {
  97.             while($row = dbFetchAssoc($result))
  98.             {
  99.             echo "<option>$row[Brand]</option>";
  100.             }
  101.          }
  102.                 else
  103.                {
  104.             echo "<option>No Brands Present</option>";
  105.                }
  106.      ?>
  107.               </select></td>
  108.             <td width="200"><input type="submit" name="Submit" value="Show" class="button_image" onClick="return CheckShowBrandByBrand();">
  109.             </td>
  110.           </tr>
  111.         </table>
  112.       </form></td>
  113.   </tr>
  114.   <tr>
  115.     <td><form name="frmByCategory" method="post" action="searchcategory.php">
  116.         <table width="100%" border="0">
  117.           <tr>
  118.             <td width="200">By Category:</td>
  119.             <td width="200"><select name="sltCategory">
  120.                 <option value="0">Select Category</option>
  121.                 <?
  122.          $sql = "SELECT DISTINCT Category FROM cstockitems ORDER BY Category ASC";
  123.          $result = dbQuery($sql);      
  124.  
  125.          if(dbNumRows($result))
  126.          {
  127.             while($row = dbFetchAssoc($result))
  128.             {
  129.             echo "<option>$row[Category]</option>";
  130.             }
  131.          }
  132.                 else
  133.                {
  134.             echo "<option>No Categories Present</option>";
  135.                }
  136.      ?>
  137.               </select></td>
  138.             <td width="200"><input type="submit" name="Submit2" value="Show" class="button_image" onClick="return CheckShowBrandByCategory();"></td>
  139.           </tr>
  140.         </table>
  141.       </form></td>
  142.   </tr>
  143.   <tr>
  144.     <td><form name="frmSearchBrand" method="post" action="showbysearch.php">
  145.         <table width="100%" border="0">
  146.           <tr>
  147.             <td width="200">Search:</td>
  148.             <td width="200"><input type="text" name="txtSearchBrand" size="40"></td>
  149.             <td width="200"><input type="submit" name="Submit3" value="Search" class="button_image" onClick="return CheckShowBrandBySearch();"></td>
  150.           </tr>
  151.         </table>
  152.       </form></td>
  153.   </tr>
  154. </table>
  155. <label class="title_text">Stock management:</label>
  156. <hr>
  157. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  158.   <tr>
  159.     <td><a href="exportoutofstockfile.php">Out of stock:</a></td>
  160.     <td><a href="exportlowstockfile.php">Low stock:</a></td>
  161.     <td><a href="exportwarningstockfile.php">Stock running low:</a></td>
  162.    <td><a href="index.php?view=stocksheet">Print stock sheet</a></td>
  163.    <td><a href="showstockvalue.php">View stock value</a></td>
  164.   </tr>
  165. </table>
  166.  
  167.  
  168. <script>
  169. function CheckShowBrandByBrand()
  170. {
  171.    form = window.document.frmByBrand;
  172.  
  173.    if (form.sltBrand.selectedIndex == 0)
  174.    {
  175.       alert('You have not selected a brand to view details!');
  176.       return false;   
  177.    }
  178.    else
  179.    {
  180.    return true;
  181.    }
  182. }
  183.  
  184. function CheckShowBrandByCategory()
  185. {
  186.    form = window.document.frmByCategory;
  187.  
  188.    if (form.sltCategory.selectedIndex == 0)
  189.    {
  190.       alert('You have not selected a Category to view details!');
  191.       return false;   
  192.    }
  193.    else
  194.    {
  195.    return true;
  196.    }
  197. }
  198.  
  199. function CheckShowBrandBySearch()
  200. {
  201.    form = window.document.frmSearchBrand;
  202.  
  203.    if (form.txtSearchBrand.value == "")
  204.    {
  205.       alert('You have not entered a brand name to search!');
  206.       return false;   
  207.    }
  208.    else
  209.    {
  210.    return true;
  211.    }
  212. }
  213. </script>
  214.  


The page from where I make the change



Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (!defined('WEB_ROOT')) {
  3.    exit;
  4. }
  5.  
  6. if (isset($_GET['StockID']) && (int)$_GET['StockID'] > 0) {
  7.    $StockID = (int)$_GET['StockID'];
  8. else 
  9. {
  10.    header('Location: index.php');
  11. }
  12.  
  13. $errorMessage = (isset($_GET['cstock_error']) && $_GET['cstock_error'] != '') ? $_GET['cstock_error'] : '&nbsp;';
  14.  
  15. $sql = "SELECT * FROM fragrancestock WHERE StockID = $StockID";
  16. $result = dbQuery($sql);      
  17. extract(dbFetchAssoc($result));
  18. ?>
  19. <p class="errorMessage"><?php echo $errorMessage; ?></p>
  20. <form action="processStock.php?action=modify" method="post" enctype="multipart/form-data" name="frmModifyStock" id="frmModifyStock">
  21.   <table width="100%" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable">
  22.     <tr>
  23.       <td colspan="2" class="title_text">Add Stock</td>
  24.     </tr>
  25.     <tr>
  26.       <td class="label">Category</td>
  27.       <td width="755" class="content"><? echo $Category ?> </td>
  28.     </tr>
  29.     <tr>
  30.       <td class="label">Brand</td>
  31.       <td class="content"><? echo $Brand; ?></td>
  32.     </tr>
  33.     <tr>
  34.       <td class="label">Stock available</td>
  35.       <td class="content"><?php echo $Quantity; ?>
  36.       <input name="hidStockID" type="hidden" id="hidStockID" value="<?php echo $StockID; ?>" /></td>
  37.     </tr>
  38.     <tr>
  39.       <td class="label">Current Selling price </td>
  40.       <td class="content"><?php echo $SellingPrice; ?></td>
  41.     </tr>
  42.     <tr>
  43.       <td class="label">Add new stock quantity </td>
  44.       <td class="content"><input name="txtQuantity" type="text" value="" size="10" onkeyup="Modify_CheckQuantityField();" /></td>
  45.     </tr>
  46.     <tr>
  47.       <td width="245" class="label">New Selling price per brand </td>
  48.       <td class="content"><input name="txtSellingPrice" type="text" value="<?php echo $SellingPrice; ?>" size="20" onkeyup="Modify_CheckSellingPrice();" onclick="EmptyField();" />
  49.       (Change selling price here) </td>
  50.     </tr>
  51.   </table>
  52.   <input name="btnAddStock" type="submit" id="btnAddStock" value="Add Stock" onClick="return checkModifyNewStock();" class="button_image">
  53.   &nbsp;&nbsp;<input name="btnCancel" type="button" id="btnCancel" value="Cancel" onClick="window.history.back();" class="button_image"> 
  54. </form>
  55.  




The page that holds my database functions which includes the modify function after which I need the redirect to the search results page (ie the page before the modification page)



Expand|Select|Wrap|Line Numbers
  1. <?php
  2. require_once '../../functions.php';
  3.  
  4. $action = isset($_GET['action']) ? $_GET['action'] : '';
  5.  
  6. switch ($action) 
  7. {
  8.    case 'add' :
  9.       addStock();
  10.       break;
  11.  
  12.    case 'modify' :
  13.       modifyStock();
  14.       break;
  15.  
  16.    case 'delete' :
  17.       deleteStock();
  18.       break;
  19.  
  20.    case 'deletestockitem' :
  21.       deletestockItem();
  22.       break;
  23.  
  24.  
  25.    case 'deletestock' :
  26.       deletestockwithID();
  27.       break;
  28.  
  29.       case 'deletestockcat' :
  30.       deletestockwithIDCat();
  31.       break;
  32.    default :
  33.  
  34.       echo "<META HTTP-EQUIV=\"refresh\" content=\"; URL=index.php\"> ";
  35. }
  36. /*Add Stock*/
  37.  
  38. function addStock()
  39. {
  40.     $Category = $_POST['sltCategory'];
  41.    $Brand = $_POST['sltBrand'];
  42.    $Quantity = $_POST['txtQuantity'];
  43.    $SP = $_POST['txtSellingPrice'];
  44.  
  45.    // Check if the stock already exists
  46.    $sql1 = "SELECT Brand FROM fragrancestock WHERE Category = '$Category' AND Brand = '$Brand'";
  47.    $result = dbQuery($sql1);
  48.  
  49.    if (dbNumRows($result) == 1) 
  50.    {
  51.        echo '<script language="javascript">alert("This brand already exists in stock! Add a new one."); </script>;';
  52.        echo "<META HTTP-EQUIV=\"refresh\" content=\"; URL=index.php\"> ";   
  53.    } 
  54.    else
  55.    {
  56.  
  57.          $sql   = "INSERT INTO fragrancestock (Category, Brand, Quantity, SellingPrice, DateAddedStock) 
  58.          VALUES ('$Category', '$Brand ', '$Quantity', '$SP', NOW())";
  59.          dbQuery($sql);
  60.  
  61.       //$errorMessage = "Stock added successfully";
  62.       echo "<META HTTP-EQUIV=\"refresh\" content=\"; URL=index.php\"> ";   
  63.    }
  64. }
  65. //Modify Stock.....This is the function that performs my modification to a brand
  66. function modifyStock()
  67. {
  68.    $StockID   = (int)$_POST['hidStockID'];   
  69.    $Quantity = $_POST['txtQuantity'];
  70.    $SP = $_POST['txtSellingPrice'];
  71.  
  72.    $sql   = "UPDATE fragrancestock SET Quantity = Quantity + '".$Quantity."',SellingPrice = '$SP', DateAddedStock = Now() WHERE StockID = '$StockID'";
  73.    dbQuery($sql);
  74.  
  75.    //The redirect I have for now which takes the user back to the page with the search form, prompting them to search again....instead of returning to the search results so that the user just browses the pagination to go to the next brand to modify
  76.  
  77.    echo "<META HTTP-EQUIV=\"refresh\" content=\"; URL=index.php\"> ";
  78.  
  79. function deleteStock()
  80. {
  81.    if (isset($_GET['StockID']) && (int)$_GET['StockID'] > 0) {
  82.       $StockID = (int)$_GET['StockID'];
  83.    } 
  84.    else 
  85.    {
  86.       //header('Location: index.php');
  87.       echo "<META HTTP-EQUIV=\"refresh\" content=\"; URL=index.php\"> ";
  88.    }
  89.  
  90.    $sql = "UPDATE fragrancestock SET Quantity = '0' WHERE StockID = $StockID";
  91.    dbQuery($sql);
  92.    //$errorMessage = "Stock deleted successfully";
  93.    echo "<META HTTP-EQUIV=\"refresh\" content=\"; URL=index.php\">";
  94. }
  95.  
  96. function deletestockItem()
  97. {
  98.    $Brand = $_POST['sltBrand'];
  99.  
  100.    $sql2 = "DELETE FROM fragrancestock WHERE Brand = '$Brand'";
  101.    dbQuery($sql2);
  102.  
  103.    echo "<META HTTP-EQUIV=\"refresh\" content=\"; URL=index.php\"> ";
  104. }
  105.  
  106. function deletestockwithID()
  107. {
  108.    if (isset($_GET['StockID']) && (int)$_GET['StockID'] > 0) {
  109.       $StockID = (int)$_GET['StockID'];
  110.    } 
  111.    else 
  112.    {
  113.       header('Location: index.php');
  114.    }
  115.  
  116.  
  117.    $sql = "DELETE FROM fragrancestock WHERE StockID = $StockID";
  118.    dbQuery($sql);
  119.  
  120.    header('Location: index.php');
  121. }
  122.  
  123. function deletestockwithIDCat()
  124. {
  125.    if (isset($_GET['StockID']) && (int)$_GET['StockID'] > 0) {
  126.       $StockID = (int)$_GET['StockID'];
  127.    } 
  128.    else 
  129.    {
  130.       header('Location: index.php');
  131.    }
  132.  
  133.  
  134.    $sql = "DELETE FROM fragrancestock WHERE StockID = $StockID";
  135.    dbQuery($sql);
  136.  
  137.    header('Location: index.php');
  138. }
  139. ?>
  140.  



There it is!

If you look at the modify function above, the redirect I have is to the search form page.

How do I redirect the user back to the results page so that the user does not have to search again, but instead continue to navigate the pagination and modify the next item?

The link to the results page on page 2 is: http://localhost/cstock/admin/stock/...&search=LADIES
Feb 24 '09 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
you could use your session....$_SESSION['login_return_url'] seems to be the place where you have been last. use that in the header() function.
Feb 25 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.