Solved

Form variables only passed on the first result page

Posted on 2009-05-12
3
201 Views
Last Modified: 2013-12-12
I have a database intended for users to search products using miltiple keywords. I made a search form with below code using Dreamweaver CS3:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="t0.php" method="POST" enctype="application/x-www-form-urlencoded" name="Searchform" id="Searchform">
  <div align="center" class="style10">
    <p>Please enter some key words of the products you look for<br />
      <br />
      <input name="searchterm" type="text" size="60"/>
    </p>
    <p><br />
      <input type="submit" name="submit" value="Search"/>
       <input type="reset" name="Reset button" id="Reset button" value="Reset" />
    </p>
  </div>
</form>
</body>
</html>

Then I did a search result page t0.php as below. Rows per page was set as 25. My problem is whenever there are multiple result pages invlolved, the search only works in the first page. From the second result page on, the search only returns records that correspond to the default variable value. For example, if I put 6x6x6 in the search box of the search form above, the first result page shows there are 66 records found, which is right by looking at my database, and display the first 25 records correctly. However, when I click on the Next button, the records shown are not for the 6x6x6, but the 4x4x4 that I put as default value in the recordset. And clicking Next again to go to page 3, nothing there. In my database, there are 44 records for the 4x4x4.  After hours of checking and searching by myself, still have no clue where is wrong.

I am pretty new to DW, PHP and MySql. Any help would be highly appreicated.

<?php require_once('Connections/sb0.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$maxRows_Recordset1 = 25;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
  $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

$searchterm_Recordset1 = "4X4X4";
if (isset($_POST['searchterm'])) {
  $searchterm_Recordset1 = $_POST['searchterm'];
}
mysql_select_db($database_sb0, $sb0);
$query_Recordset1 = sprintf("SELECT * FROM Boxes, Suppliers WHERE MATCH (Description) AGAINST ( %s) AND Boxes.SID = Suppliers.SID ORDER BY Suppliers.Supplier_Name ASC", GetSQLValueString($searchterm_Recordset1, "text"));
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $sb0) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if (isset($_GET['totalRows_Recordset1'])) {
  $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
  $all_Recordset1 = mysql_query($query_Recordset1);
  $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;

$queryString_Recordset1 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Recordset1") == false && 
        stristr($param, "totalRows_Recordset1") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Recordset1 = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<p>Records <?php echo ($startRow_Recordset1 + 1) ?> to <?php echo min($startRow_Recordset1 + $maxRows_Recordset1, $totalRows_Recordset1) ?> of <?php echo $totalRows_Recordset1 ?></p>
<table border="0">
  <tr>
    <td><?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, 0, $queryString_Recordset1); ?>">First</a>
          <?php } // Show if not first page ?>
    </td>
    <td><?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, max(0, $pageNum_Recordset1 - 1), $queryString_Recordset1); ?>">Previous</a>
          <?php } // Show if not first page ?>
    </td>
    <td><?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, min($totalPages_Recordset1, $pageNum_Recordset1 + 1), $queryString_Recordset1); ?>">Next</a>
          <?php } // Show if not last page ?>
    </td>
    <td><?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, $totalPages_Recordset1, $queryString_Recordset1); ?>">Last</a>
          <?php } // Show if not last page ?>
    </td>
  </tr>
</table>
<table width="1000" border="1" align="left">
  <tr>
    <th scope="col">PID</th>
    <th scope="col">Supplier_Product_Code</th>
    <th scope="col">Description</th>
    <th scope="col">Supplier_Name</th>
    <th scope="col">Tollfree</th>
    <th scope="col">Phone</th>
    <th scope="col">Fax</th>
    <th scope="col">Email</th>
    <th scope="col">Website</th>
    <th scope="col">Address</th>
   
   
  </tr>
  <?php do { ?>
    <tr>
      <td><?php echo $row_Recordset1['PID']; ?></td>
      <td><?php echo $row_Recordset1['Supplier_Product_Code']; ?></td>
      <td><?php echo $row_Recordset1['Description']; ?></td>
      <td><?php echo $row_Recordset1['Supplier_Name']; ?></td>
      <td><?php echo $row_Recordset1['Tollfree']; ?></td>
      <td><?php echo $row_Recordset1['Phone']; ?></td>
      <td><?php echo $row_Recordset1['Fax']; ?></td>
      <td><?php echo $row_Recordset1['Email']; ?></td>
      <td><?php echo $row_Recordset1['Website']; ?></td>
      <td><?php echo $row_Recordset1['Address']; ?></td>
     
    </tr>
    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>


</body>
</html>
<?php
mysql_free_result($Recordset1);
?>






0
Comment
Question by:DomerBill
  • 2
3 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24370429
You have this code:

$searchterm_Recordset1 = "4X4X4";
if (isset($_POST['searchterm'])) {
  $searchterm_Recordset1 = $_POST['searchterm'];
}

Because you use $_POST, you are only fetching this on the first submit. Use $_REQUEST instead of $_POST, then it will work both for form submission and link clicks.

Then just before the DOCTYPE, you have this:

$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);

Here you need to add the searchterm as a parameter:

$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s&%s", $totalRows_Recordset1, $queryString_Recordset1,urlencode($searchterm_Recordset1));
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24370436
Sorry, it should be like this:
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s&searchterm=%s", 
  $totalRows_Recordset1, $queryString_Recordset1, urlencode($searchterm_Recordset1));

Open in new window

0
 

Author Closing Comment

by:DomerBill
ID: 31580789
It works perfectly!
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question