Very confusing problem with a POST variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DjMikeWatt
Forum Newbie
Posts: 3
Joined: Thu May 22, 2008 8:33 pm

Very confusing problem with a POST variable

Post by DjMikeWatt »

I've got an interesting problem that has me baffled. Normally under these conditions it's the simple things that I completely overlook while hunting for something much more difficult.

I have an internal search page that lets me search the entire database without worrying about authentication. This form in particular has only four fields and it searches the (ClientReferrals) table. The results are returned on a standard table that's set up with repeating regions. One of the fields in the (ClientReferrals) table is (AEid). This field shows up in the record set that is returned when searching the table.

Below the table, I have a button that allows me to call-up the record of the AE (as identified by the AEid) from another table (AEinput). To accomplish this, I simply put a hidden field and set the value to

Code: Select all

<?php echo $row_SearchResults['AEid']; ?>
. Keeping in mind that this variable is already visable on the page in the table directly above this hidden field. The button kept failing, returning 0 results so I switched from POST to GET so I could examine the URL and it's not passing on the variable. Next I created an addition text field next to the button and populated it the same way. When the page loads, it remains blank. So, it works in the table above, but not here... and I simply don't know why. I have noticed that if I move this line:

Code: Select all

<?php } while ($row_SearchResults = mysql_fetch_assoc($SearchResults)); ?>
down below the hidden field and button the variable shows up, but that screws up the layout of the results when there is more than one record returned (screws up the repeating region)...

Here is the code for the page... I would really appreciate it if someone notices anything... I'm teaching myself as I go and every so often I hit a brick wall. The page on the test server is at http://www.wattproductions.com/test/client_search.php (this is the code below).
Some search criteria that will return results are (Zip=11377) or (Zip=34787). Then the button labeled "Pull AE Record" sends the data via URL Variable to (/test/client_search_result.php), but, as I said, it's not currently passing that variable.

Thanks so much...
Michael

Code: Select all

<?php require_once('Connections/AEinput.php'); ?>
<?php
$colname_SearchResults = "%";
if (isset($HTTP_POST_VARS['BusName'])) {
  $colname_SearchResults = (get_magic_quotes_gpc()) ? $HTTP_POST_VARS['BusName'] : addslashes($HTTP_POST_VARS['BusName']);
}
$colname2_SearchResults = "%";
if (isset($HTTP_POST_VARS['Contact'])) {
  $colname2_SearchResults = (get_magic_quotes_gpc()) ? $HTTP_POST_VARS['Contact'] : addslashes($HTTP_POST_VARS['Contact']);
}
$colname3_SearchResults = "%";
if (isset($HTTP_POST_VARS['Phone'])) {
  $colname3_SearchResults = (get_magic_quotes_gpc()) ? $HTTP_POST_VARS['Phone'] : addslashes($HTTP_POST_VARS['Phone']);
}
$colname4_SearchResults = "%";
if (isset($HTTP_POST_VARS['Zip'])) {
  $colname4_SearchResults = (get_magic_quotes_gpc()) ? $HTTP_POST_VARS['Zip'] : addslashes($HTTP_POST_VARS['Zip']);
}
mysql_select_db($database_AEinput, $AEinput);
$query_SearchResults = sprintf("SELECT * FROM ClientReferrals WHERE (BusName LIKE '%%%s%%') AND (Contact LIKE '%%%s%%') AND (Phone LIKE '%%%s%%') AND (Zip LIKE '%%%s%%')", $colname_SearchResults,$colname2_SearchResults,$colname3_SearchResults,$colname4_SearchResults);
$SearchResults = mysql_query($query_SearchResults, $AEinput) or die(mysql_error());
$row_SearchResults = mysql_fetch_assoc($SearchResults);
$totalRows_SearchResults = mysql_num_rows($SearchResults);
?>
 
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body>
 
  
  <!--DWLayoutTable-->
  <tr> 
    <td width="2330" height="27" valign="top"><form action="view_info_result.php" method="post" name="internalform" id="internalform">
      <table width="992" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
  <!--DWLayoutTable-->
  <tr> 
    <td width="992" height="37" valign="top" bgcolor="#000000"> <div align="center"><font color="#FFFFFF" size="+3" face="Geneva, Arial, Helvetica, sans-serif">Search 
        Results</font></div></td>
  </tr>
</table>
 
<table border="2" cellpadding="2" cellspacing="1">
  <tr> 
    <td>ID</td>
    <td>Name</td>
    <td>Industry</td>
    <td>Contact</td>
    <td>Email</td>
    <td>Phone</td>
    <td>Address1</td>
    <td>Address2</td>
    <td>City</td>
    <td>ST</td>
    <td>Zip</td>
    <td>Billing</td>
    <td>Referral</td>
    <td>AE</td>
    <td>ReferredOn</td>
  </tr>
  <?php do { ?>
  <tr> 
    <td><?php echo $row_SearchResults['ClientID']; ?></td>
    <td><?php echo $row_SearchResults['BusName']; ?></td>
    <td><?php echo $row_SearchResults['Industry']; ?></td>
    <td><?php echo $row_SearchResults['Contact']; ?></td>
    <td><?php echo $row_SearchResults['Email']; ?></td>
    <td><?php echo $row_SearchResults['Phone']; ?></td>
    <td><?php echo $row_SearchResults['Address1']; ?></td>
    <td><?php echo $row_SearchResults['Address2']; ?></td>
    <td><?php echo $row_SearchResults['City']; ?></td>
    <td><?php echo $row_SearchResults['State']; ?></td>
    <td><?php echo $row_SearchResults['Zip']; ?></td>
    <td><?php echo $row_SearchResults['Billing']; ?></td>
    <td><?php echo $row_SearchResults['Referral']; ?></td>
    <td><?php echo $row_SearchResults['AEid']; ?></td>
    <td><?php echo $row_SearchResults['ReferredOn']; ?></td>
  </tr>
  <?php } while ($row_SearchResults = mysql_fetch_assoc($SearchResults)); ?>
</table>
<table width="2330" border="0" cellpadding="0" cellspacing="0">
      <p> 
        <input name="AEid" type="hidden" id="AEid" value="<?php echo $row_SearchResults['AEid']; ?>">
        <input type="submit" name="Submit" value="Pull AE Record">
        <input name="textfield" type="text" value="<?php echo $row_SearchResults['AEid']; ?>">
      </p>
    </form></td>
  </tr>
</body>
</html>
<?php
mysql_free_result($SearchResults);
?>
 
 
Dutchben
Forum Newbie
Posts: 12
Joined: Wed May 14, 2008 10:19 am

Re: Very confusing problem with a POST variable

Post by Dutchben »

In line 22 you setting $row_SearchResults = mysql_fetch_assoc($SearchResults); for the first time. In line 64 you are starting to iterate over you search results using do while. When using a 'do while' loop the code is executed first and then the condition is checked, so lines 65 to 81 are always executed once even if there are no results - i don't know if this is the behaviour you want. On the last iteration $row_SearchResults is set to nothing which fails the condition and ends the loop. After the loop $row_SearchResults['AEid'] is empty so when you call in line 88 there's nothing.

As i understand it you want to use a selected value of AEid to pull results from a table to be displayed on the page http://www.wattproductions.com/test/vie ... textfield=

When i enter the zip code 34787 into http://www.wattproductions.com/test/client_search.php
i get 2 results (ID 2 with AE = 21 and ID 7 with AE = 18). You would need a button for each of those results in order to get a different result for http://www.wattproductions.com/test/vie ... result.php for AE 21 and AE 18

So i think what your looking for is
# <?php while ($row_SearchResults = mysql_fetch_assoc($SearchResults)){ ?>
# <tr>
# <td><?php =$row_SearchResults['ClientID']; ?></td>
# <td><?php =$row_SearchResults['BusName']; ?></td>
# <td><?php =$row_SearchResults['Industry']; ?></td>
# <td><?php =$row_SearchResults['Contact']; ?></td>
# <td><?php =$row_SearchResults['Email']; ?></td>
# <td><?php =$row_SearchResults['Phone']; ?></td>
# <td><?php =$row_SearchResults['Address1']; ?></td>
# <td><?php =$row_SearchResults['Address2']; ?></td>
# <td><?php =$row_SearchResults['City']; ?></td>
# <td><?php =$row_SearchResults['State']; ?></td>
# <td><?php =$row_SearchResults['Zip']; ?></td>
# <td><?php =$row_SearchResults['Billing']; ?></td>
# <td><?php =$row_SearchResults['Referral']; ?></td>
# <td><?php =$row_SearchResults['AEid']; ?></td>
# <td><?php =$row_SearchResults['ReferredOn']; ?></td>
# <td>
<form action="http://www.wattproductions.com/test/vie ... result.php" method="POST">
<input name="AEid" type="hidden" id="AEid" value="<?php = $row_SearchResults['AEid']; ?>">
<input type="submit" name="Submit" value="Pull AE Record">
</form>
</td>
# </tr>
# <?php } ?>
# </table>
# </body>
# </html>
# <?php
# mysql_free_result($SearchResults);
# ?>
Also drop line 22 and just start reading your result when the while loop starts. Also notice you can use the shorthand = instead of echo which saves some typing :)
DjMikeWatt
Forum Newbie
Posts: 3
Joined: Thu May 22, 2008 8:33 pm

Re: Very confusing problem with a POST variable

Post by DjMikeWatt »

Awesome - thanks... I'm going to play with this tonight and see what I come up with... I really appreciate your detailed response.
Post Reply