PHP redirect after posting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gnawz
    New Member
    • Nov 2006
    • 64

    PHP redirect after posting

    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
    Code:
    <?php
    require_once '../../functions.php';
    
    $_SESSION['login_return_url'] = $_SERVER['REQUEST_URI'];
    checkUser();
    
    $view = (isset($_GET['view']) && $_GET['view'] != '') ? $_GET['view'] : '';
    
    switch ($view) {
       case 'list' :
          $content    = 'list.php';      
          $pageTitle    = 'Fragrance Lounge - Stock panel';
          break;
    
       case 'add' :
          $content    = 'add.php';      
          $pageTitle  = 'Fragrance Lounge - Add Stock ';
          break;
    
       case 'modify' :
          $content    = 'modify.php';      
          $pageTitle    = 'Fragrance Lounge - Modify Stock';
          break;
          
       case 'detail' :
          $content    = 'detail.php';      
          $pageTitle    = 'Fragrance Lounge - Showing Stock Details';
          break;
          
       case 'showbybrand' :
          $content    = 'showbybrand.php';      
          $pageTitle    = 'Fragrance Lounge - Showing Stock By Brand';
       break;
          
       case 'showbycategory' :
          $content    = 'showbycategory.php';      
          $pageTitle    = 'Fragrance Lounge - Showing Stock By Category';
       break;
       
       case 'showbysearch' :
          $content    = 'showbysearch.php';      
          $pageTitle    = 'Fragrance Lounge - Showing Stock By Searching';
       break;
       
       case 'stocksheet' :
          $content    = 'printsheet.php';      
          $pageTitle    = 'Fragrance Lounge - Printing stock sheet';
       break;
       
       case 'add' :
             $content    = 'add.php';      
             $pageTitle    = 'Fragrance Lounge - Add Stock';
       break;
    
       default :
    
          $content    = 'list.php';   //the default page which holds the search form   
          $pageTitle    = 'Fragrance Lounge - Stock panel';
    }
    
    $script  = array('stock.js');
    
    require_once '../include/template.php';
    ?>




    The page with the search form

    Code:
    <?php
    if (!defined('WEB_ROOT')) {
       exit;
    }
    $rowsPerPage = 5;
    
    $_SESSION['login_return_url'] = $_SERVER['REQUEST_URI'];
    checkUser();
    
    $sql = "SELECT * FROM fragrancestock ORDER BY Brand ASC";
    $result     = dbQuery(getPagingQuery($sql, $rowsPerPage));
    $pagingLink = getPagingLink($sql, $rowsPerPage);
    
    $errorMessage = (isset($_GET['cstock_error']) && $_GET['cstock_error'] != '') ? $_GET['cstock_error'] : '&nbsp;';
    ?>
    <form action="index.php?view=add" method="post"  name="frmList" id="frmList">
      <table width="100%" border="0" align="center">
        <tr class="title_text">
          <td width="130">Category</td>
          <td width="520">Brand</td>
          <td width="130" align="center">Stock</td>
          <td width="130"> Price </td>
          <td width="66">Date Added </td>
          <td width="66">Add</td>
          <td width="66">Delete </td>
        </tr>
        <?php
    if (dbNumRows($result) > 0) {
       $i = 0;
       
       while($row = dbFetchAssoc($result)) {
          extract($row);
             
          if ($i%2) {
             $class = 'row1';
          } else {
             $class = 'row2';
          }
          
          $i += 1;
    ?>
        <tr class="<?php echo $class; ?>">
          <td width="130"><?php echo $Category; ?></td>
          <td width="520"><?php echo $Brand; ?></td>
          <td width="130" class="inner_border"><?php echo $Quantity; ?></td>
          <td width="130" class="inner_border"><?php echo $SellingPrice; ?></td>
          <td width="66"><?php echo $DateAddedStock; ?></td>
          <td width="66"><a href="javascript:modifyStock(<?php echo $StockID; ?>);"><strong>Add
            </strong> </a></td>
          <td width="66"><a href="javascript:deleteStock(<?php echo $StockID; ?>);">Delete</a></td>
        </tr>
        <?php
       } // end while
    
    
    ?>
        <tr>
          <td colspan="7" align="center">
            <?php 
       echo $pagingLink;
       ?>      </td>
        </tr>
        <?php   
    } else {
    ?>
        <tr>
          <td colspan="7" align="center">No Stock Items Yet</td>
        </tr>
        <?php
    }
    ?>
        <tr>
          <td colspan="7">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="6" align="right"> <input name="btnAddStock" type="submit" value="New Entry" class="button_image">
        <td align="right">    </tr>
      </table>
    </form>
    
    <label class="title_text">View Stock:</label>
    <hr>
    <table width="100%" border="0">
      <tr>
        <td><form action="index.php?view=showbybrand" method="post" name="frmByBrand">
            <table width="100%" border="0">
              <tr>
                <td width="200">By Brand:</td>
                <td width="200"><select name="sltBrand">
                    <option value="0">Select Brand</option>
                    <?
             $sql = "SELECT  Brand FROM cstockitems ORDER BY Brand ASC";
             $result = dbQuery($sql);      
             
             if(dbNumRows($result))
             {
                while($row = dbFetchAssoc($result))
                {
                echo "<option>$row[Brand]</option>";
                }
             }
                    else
                   {
                echo "<option>No Brands Present</option>";
                   }
         ?>
                  </select></td>
                <td width="200"><input type="submit" name="Submit" value="Show" class="button_image" onClick="return CheckShowBrandByBrand();">
                </td>
              </tr>
            </table>
          </form></td>
      </tr>
      <tr>
        <td><form name="frmByCategory" method="post" action="searchcategory.php">
            <table width="100%" border="0">
              <tr>
                <td width="200">By Category:</td>
                <td width="200"><select name="sltCategory">
                    <option value="0">Select Category</option>
                    <?
             $sql = "SELECT DISTINCT Category FROM cstockitems ORDER BY Category ASC";
             $result = dbQuery($sql);      
             
             if(dbNumRows($result))
             {
                while($row = dbFetchAssoc($result))
                {
                echo "<option>$row[Category]</option>";
                }
             }
                    else
                   {
                echo "<option>No Categories Present</option>";
                   }
         ?>
                  </select></td>
                <td width="200"><input type="submit" name="Submit2" value="Show" class="button_image" onClick="return CheckShowBrandByCategory();"></td>
              </tr>
            </table>
          </form></td>
      </tr>
      <tr>
        <td><form name="frmSearchBrand" method="post" action="showbysearch.php">
            <table width="100%" border="0">
              <tr>
                <td width="200">Search:</td>
                <td width="200"><input type="text" name="txtSearchBrand" size="40"></td>
                <td width="200"><input type="submit" name="Submit3" value="Search" class="button_image" onClick="return CheckShowBrandBySearch();"></td>
              </tr>
            </table>
          </form></td>
      </tr>
    </table>
    <label class="title_text">Stock management:</label>
    <hr>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><a href="exportoutofstockfile.php">Out of stock:</a></td>
        <td><a href="exportlowstockfile.php">Low stock:</a></td>
        <td><a href="exportwarningstockfile.php">Stock running low:</a></td>
       <td><a href="index.php?view=stocksheet">Print stock sheet</a></td>
       <td><a href="showstockvalue.php">View stock value</a></td>
      </tr>
    </table>
    
    
    <script>
    function CheckShowBrandByBrand()
    {
       form = window.document.frmByBrand;
       
       if (form.sltBrand.selectedIndex == 0)
       {
          alert('You have not selected a brand to view details!');
          return false;   
       }
       else
       {
       return true;
       }
    }
    
    function CheckShowBrandByCategory()
    {
       form = window.document.frmByCategory;
       
       if (form.sltCategory.selectedIndex == 0)
       {
          alert('You have not selected a Category to view details!');
          return false;   
       }
       else
       {
       return true;
       }
    }
    
    function CheckShowBrandBySearch()
    {
       form = window.document.frmSearchBrand;
       
       if (form.txtSearchBrand.value == "")
       {
          alert('You have not entered a brand name to search!');
          return false;   
       }
       else
       {
       return true;
       }
    }
    </script>


    The page from where I make the change



    Code:
    <?php
    if (!defined('WEB_ROOT')) {
       exit;
    }
    
    if (isset($_GET['StockID']) && (int)$_GET['StockID'] > 0) {
       $StockID = (int)$_GET['StockID'];
    } 
    else 
    {
       header('Location: index.php');
    }
    
    $errorMessage = (isset($_GET['cstock_error']) && $_GET['cstock_error'] != '') ? $_GET['cstock_error'] : '&nbsp;';
    
    $sql = "SELECT * FROM fragrancestock WHERE StockID = $StockID";
    $result = dbQuery($sql);      
    extract(dbFetchAssoc($result));
    ?>
    <p class="errorMessage"><?php echo $errorMessage; ?></p>
    <form action="processStock.php?action=modify" method="post" enctype="multipart/form-data" name="frmModifyStock" id="frmModifyStock">
      <table width="100%" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable">
        <tr>
          <td colspan="2" class="title_text">Add Stock</td>
        </tr>
        <tr>
          <td class="label">Category</td>
          <td width="755" class="content"><? echo $Category ?> </td>
        </tr>
        <tr>
          <td class="label">Brand</td>
          <td class="content"><? echo $Brand; ?></td>
        </tr>
        <tr>
          <td class="label">Stock available</td>
          <td class="content"><?php echo $Quantity; ?>
          <input name="hidStockID" type="hidden" id="hidStockID" value="<?php echo $StockID; ?>" /></td>
        </tr>
        <tr>
          <td class="label">Current Selling price </td>
          <td class="content"><?php echo $SellingPrice; ?></td>
        </tr>
        <tr>
          <td class="label">Add new stock quantity </td>
          <td class="content"><input name="txtQuantity" type="text" value="" size="10" onkeyup="Modify_CheckQuantityField();" /></td>
        </tr>
        <tr>
          <td width="245" class="label">New Selling price per brand </td>
          <td class="content"><input name="txtSellingPrice" type="text" value="<?php echo $SellingPrice; ?>" size="20" onkeyup="Modify_CheckSellingPrice();" onclick="EmptyField();" />
          (Change selling price here) </td>
        </tr>
      </table>
      <input name="btnAddStock" type="submit" id="btnAddStock" value="Add Stock" onClick="return checkModifyNewStock();" class="button_image">
      &nbsp;&nbsp;<input name="btnCancel" type="button" id="btnCancel" value="Cancel" onClick="window.history.back();" class="button_image"> 
    </form>




    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)



    Code:
    <?php
    require_once '../../functions.php';
    
    $action = isset($_GET['action']) ? $_GET['action'] : '';
    
    switch ($action) 
    {
       case 'add' :
          addStock();
          break;
          
       case 'modify' :
          modifyStock();
          break;
          
       case 'delete' :
          deleteStock();
          break;
          
       case 'deletestockitem' :
          deletestockItem();
          break;
          
             
       case 'deletestock' :
          deletestockwithID();
          break;
          
          case 'deletestockcat' :
          deletestockwithIDCat();
          break;
       default :
          
          echo "<META HTTP-EQUIV=\"refresh\" content=\"; URL=index.php\"> ";
    }
    /*Add Stock*/
    
    function addStock()
    {
        $Category = $_POST['sltCategory'];
       $Brand = $_POST['sltBrand'];
       $Quantity = $_POST['txtQuantity'];
       $SP = $_POST['txtSellingPrice'];
       
       // Check if the stock already exists
       $sql1 = "SELECT Brand FROM fragrancestock WHERE Category = '$Category' AND Brand = '$Brand'";
       $result = dbQuery($sql1);
       
       if (dbNumRows($result) == 1) 
       {
           echo '<script language="javascript">alert("This brand already exists in stock! Add a new one."); </script>;';
           echo "<META HTTP-EQUIV=\"refresh\" content=\"; URL=index.php\"> ";   
       } 
       else
       {
             
             $sql   = "INSERT INTO fragrancestock (Category, Brand, Quantity, SellingPrice, DateAddedStock) 
             VALUES ('$Category', '$Brand ', '$Quantity', '$SP', NOW())";
             dbQuery($sql);
          
          //$errorMessage = "Stock added successfully";
          echo "<META HTTP-EQUIV=\"refresh\" content=\"; URL=index.php\"> ";   
       }
    }
    //Modify Stock.....This is the function that performs my modification to a brand
    function modifyStock()
    {
       $StockID   = (int)$_POST['hidStockID'];   
       $Quantity = $_POST['txtQuantity'];
       $SP = $_POST['txtSellingPrice'];
       
       $sql   = "UPDATE fragrancestock SET Quantity = Quantity + '".$Quantity."',SellingPrice = '$SP', DateAddedStock = Now() WHERE StockID = '$StockID'";
       dbQuery($sql);
       
       //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
    
       echo "<META HTTP-EQUIV=\"refresh\" content=\"; URL=index.php\"> ";
       
    function deleteStock()
    {
       if (isset($_GET['StockID']) && (int)$_GET['StockID'] > 0) {
          $StockID = (int)$_GET['StockID'];
       } 
       else 
       {
          //header('Location: index.php');
          echo "<META HTTP-EQUIV=\"refresh\" content=\"; URL=index.php\"> ";
       }
       
       $sql = "UPDATE fragrancestock SET Quantity = '0' WHERE StockID = $StockID";
       dbQuery($sql);
       //$errorMessage = "Stock deleted successfully";
       echo "<META HTTP-EQUIV=\"refresh\" content=\"; URL=index.php\">";
    }
    
    function deletestockItem()
    {
       $Brand = $_POST['sltBrand'];
       
       $sql2 = "DELETE FROM fragrancestock WHERE Brand = '$Brand'";
       dbQuery($sql2);
       
       echo "<META HTTP-EQUIV=\"refresh\" content=\"; URL=index.php\"> ";
    }
    
    function deletestockwithID()
    {
       if (isset($_GET['StockID']) && (int)$_GET['StockID'] > 0) {
          $StockID = (int)$_GET['StockID'];
       } 
       else 
       {
          header('Location: index.php');
       }
       
       
       $sql = "DELETE FROM fragrancestock WHERE StockID = $StockID";
       dbQuery($sql);
       
       header('Location: index.php');
    }
    
    function deletestockwithIDCat()
    {
       if (isset($_GET['StockID']) && (int)$_GET['StockID'] > 0) {
          $StockID = (int)$_GET['StockID'];
       } 
       else 
       {
          header('Location: index.php');
       }
       
       
       $sql = "DELETE FROM fragrancestock WHERE StockID = $StockID";
       dbQuery($sql);
       
       header('Location: index.php');
    }
    ?>



    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
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you could use your session....$_SE SSION['login_return_u rl'] seems to be the place where you have been last. use that in the header() function.

    Comment

    Working...