Page 1 of 1

POST variables not getting set

Posted: Tue Jun 27, 2006 6:14 pm
by jmill
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Wassup all,

I am writing a simple reporting system that updates a status and records the date it was updated.  The problem i'm running into is that my POST variables are not being set.  I cannot figure out why.  The page is in an iframe and it submits the form data to itself.  However, I have tried testing it outside of iframes and submitting to separate pages but still get the same problem.  PLEASE HELP!!  Here is the necessary code from my update page.

Code: Select all

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "status")) {
  mysql_select_db($database_DB_bonghead, $DB_bonghead);
  for ($x=0;x<$_POST['row_count'];$x++) {
  $updateSQL = sprintf("UPDATE cases SET status=%s, date_updated=%s WHERE idnum=%s",
                       GetSQLValueString($_POST['status[$x]'], "text"),
                       GetSQLValueString($_POST['date_updated[$x]'], "date"),
					   GetSQLValueString($_POST['idnum[$x]'], "int"));
					   
  $Result1 = mysql_query($updateSQL, $DB_bonghead) or die(mysql_error());
  }
  $updateGoTo = "show_cases.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="status" target="_self">
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <th scope="col">Client Name</th>
        <th scope="col">Case Type </th>
        <th scope="col">Case Status </th>
      </tr>
      <?php $i=0; ?>
	  <?php do { ?>
        <tr>
            <td><div align="center">
              <input type="hidden" name="idnum[<?php echo $i; ?>]" value="<?php echo $row_cases['idnum']; ?>" />
			  <input type="hidden" name="date_updated[<?php echo $i; ?>]" value="<?php date(Y,m,d); ?>" />
			  <?php echo $row_cases['client_fname']; ?> <?php echo $row_cases['client_lname']; ?></div></td>
          <td><div align="center"><?php echo $row_cases['type']; ?></div></td>
          <td><div align="center">
            <select name="status[<?php echo $i; ?>]">
              <option value="Waiting for Profile" <?php if (!(strcmp("Waiting for Profile", $row_cases['status']))) {echo "selected=\"selected\"";} ?>>Waiting for Profile</option>
              <option value="Reviewing profile" <?php if (!(strcmp("Reviewing profile", $row_cases['status']))) {echo "selected=\"selected\"";} ?>>Reviewing profile</option>
              <option value="Profile signed" <?php if (!(strcmp("Profile signed", $row_cases['status']))) {echo "selected=\"selected\"";} ?>>Profile signed</option>
              <option value="Signed profile sent in" <?php if (!(strcmp("Signed profile sent in", $row_cases['status']))) {echo "selected=\"selected\"";} ?>>Signed profile sent in</option>
              <option value="Being revised" <?php if (!(strcmp("Being revised", $row_cases['status']))) {echo "selected=\"selected\"";} ?>>Being revised</option>
              <option value="Waiting to close" <?php if (!(strcmp("Waiting to close", $row_cases['status']))) {echo "selected=\"selected\"";} ?>>Waiting to close</option>
              <option value="Closed" <?php if (!(strcmp("Closed", $row_cases['status']))) {echo "selected=\"selected\"";} ?>>Closed</option>
            </select>
</div></td>
        </tr>
        <?php $i++; ?>
		<?php } while ($row_cases = mysql_fetch_assoc($cases)); ?>
    </table></td>
  </tr>
  <tr>
    <td><div align="center">
	  <input name="submit" type="submit" id="submit" value="Update" />
      <input type="reset" name="Reset" value="Reset" />
    </div></td>
  </tr>
  <input type="hidden" name="MM_update" value="status" />
  <input type="hidden" name="row_count" value="<?php echo $totalRows_cases; ?>" />
  </form>
</table>
</body>
</html>
<?php
mysql_free_result($id);

mysql_free_result($cases);
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Jun 27, 2006 6:18 pm
by RobertGonzalez
First things first, use [ php ] tags instead of code tags for PHP. Next, use $_REQUEST['QUERY_STRING']. Last, ave you echoed out any post vars to see what the form is sending? Maybe even before checking for isset() try doing a print_r($POST) and see what that does.

Posted: Tue Jun 27, 2006 6:22 pm
by feyd
try $_POST['status'][$x] and so forth.

Posted: Thu Jun 29, 2006 5:31 pm
by jmill
Hey Guys, thanks for the replys. Sry 'bout the code, i'm a forum newb. I tested the $_POST variables by placing an "else die" statement after the "if" statement that tests for the $_POST['MM_update'] variable. It dies everytime, so I figure that variable isn't set. I will try the print_r once i get back to my workstation. I will also try the two dimensional POST array, but it doesn't make sense to me how that would fix the problem.

Posted: Fri Jun 30, 2006 4:32 am
by jmill
I did a print_r before the POST variables are checked and after submitting the form, this is what is displayed.

Array ( [idnum] => Array ( [0] => 1 ) [date_updated] => Array ( [0] => 0 ) [status] => Array ( [0] => Signed profile sent in ) [submit] => Update [MM_update] => status [row_count] => 1 )
Warning: Cannot modify header information - headers already sent

The header warning part is not a problem in this case. It shows that they are being set correctly, so I must not be accessing them properly somewhere. I will go through it all again, but ANY help is appreciated.

Posted: Fri Jun 30, 2006 4:43 am
by jamiel
Don't use isset on the $_POST array. Try strlen instead. In fact your isset condition is not even neccassry if you are checking if the value is "status" anyway.

Posted: Fri Jun 30, 2006 8:25 am
by RobertGonzalez
Isset is necessary if you are checking for a POST array var that may not have an index. The system will throw an undefined index warning at you.

Code: Select all

Array (
	[idnum] => Array ( 
		[0] => 1) 
		
	[date_updated] => Array ( 
		[0] => 0 ) 
		
	[status] => Array ( 
		[0] => Signed profile sent in ) 
		
	[submit] => Update 
	
	[MM_update] => status 
	
	[row_count] => 1 )
This array output is telling me that the fieds 'idnum', date_updated' and 'status' are being sent as arrays instead of string vars. That means that you have your form fields named like 'name="fieldname[]"'. Is there a reason why these fields are arrays and the other are strings? If this is the intent of the script, maybe what you could do is at certain points within the script, 'break' your script with a call to die() ever now and again and echoing your vars to see what is being worked with.

Posted: Sat Jul 01, 2006 6:25 am
by jmill
I've tried various varieties of the same $POST variables check using isset, strlen, and so forth, but they all fail. The 'idnum', 'date_updated', and 'status' inputs are submitted as arrays because they are the three fields I am updating and I may need to update them on multiple records. So I have to use them as arrays to access them and make sure the corrct field is updated. They shouldn't have any affect on the "if" statement failing in my opinion however.

Posted: Sat Jul 01, 2006 6:41 am
by Benjamin
That is some weird code. There were a few errors in it, I'm not sure I found them all, but try this...

You were missing a $ in front of an x in a for loop, also you didn't have a closing ?> tag when you transitioned back into html. Is your error reporting turned off?

Code: Select all

<?php
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "status")) {
  mysql_select_db($database_DB_bonghead, $DB_bonghead);
  for ($x=0; $x <$_POST['row_count'];$x++) { // was for ($x=0;x <$_POST['row_count'];$x++) {
  $updateSQL = sprintf("UPDATE cases SET status=%s, date_updated=%s WHERE idnum=%s",
                       GetSQLValueString($_POST['status[$x]'], "text"),
                       GetSQLValueString($_POST['date_updated[$x]'], "date"),
                                           GetSQLValueString($_POST['idnum[$x]'], "int"));
                                          
  $Result1 = mysql_query($updateSQL, $DB_bonghead) or die(mysql_error());
  }
  $updateGoTo = "show_cases.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}
 // no closing php tag??
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="status" target="_self">
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <th scope="col">Client Name</th>
        <th scope="col">Case Type </th>
        <th scope="col">Case Status </th>
      </tr>
      <?php $i=0; ?>
          <?php do { ?>
        <tr>
            <td><div align="center">
              <input type="hidden" name="idnum[<?php echo $i; ?>]" value="<?php echo $row_cases['idnum']; ?>" />
                          <input type="hidden" name="date_updated[<?php echo $i; ?>]" value="<?php date(Y,m,d); ?>" />
                          <?php echo $row_cases['client_fname']; ?> <?php echo $row_cases['client_lname']; ?></div></td>
          <td><div align="center"><?php echo $row_cases['type']; ?></div></td>
          <td><div align="center">
            <select name="status[<?php echo $i; ?>]">
              <option value="Waiting for Profile" <?php if (!(strcmp("Waiting for Profile", $row_cases['status']))) {echo "selected=\"selected\"";} ?>>Waiting for Profile</option>
              <option value="Reviewing profile" <?php if (!(strcmp("Reviewing profile", $row_cases['status']))) {echo "selected=\"selected\"";} ?>>Reviewing profile</option>
              <option value="Profile signed" <?php if (!(strcmp("Profile signed", $row_cases['status']))) {echo "selected=\"selected\"";} ?>>Profile signed</option>
              <option value="Signed profile sent in" <?php if (!(strcmp("Signed profile sent in", $row_cases['status']))) {echo "selected=\"selected\"";} ?>>Signed profile sent in</option>
              <option value="Being revised" <?php if (!(strcmp("Being revised", $row_cases['status']))) {echo "selected=\"selected\"";} ?>>Being revised</option>
              <option value="Waiting to close" <?php if (!(strcmp("Waiting to close", $row_cases['status']))) {echo "selected=\"selected\"";} ?>>Waiting to close</option>
              <option value="Closed" <?php if (!(strcmp("Closed", $row_cases['status']))) {echo "selected=\"selected\"";} ?>>Closed</option>
            </select>
</div></td>
        </tr>
        <?php $i++; ?>
                <?php } while ($row_cases = mysql_fetch_assoc($cases)); ?>
    </table></td>
  </tr>
  <tr>
    <td><div align="center">
          <input name="submit" type="submit" id="submit" value="Update" />
      <input type="reset" name="Reset" value="Reset" />
    </div></td>
  </tr>
  <input type="hidden" name="MM_update" value="status" />
  <input type="hidden" name="row_count" value="<?php echo $totalRows_cases; ?>" />
  </form>
</table>
</body>
</html>
<?php
mysql_free_result($id);

mysql_free_result($cases);
?>

Posted: Sat Jul 01, 2006 9:15 pm
by jmill
Wow, good job catching that $ before the x, Thanks. The closing tags are there, but I have a lot of code on that page so I only posted parts and pieces of it. That if statement is the big problem though, so I put up all the code relevant to it.

Posted: Sat Jul 01, 2006 11:37 pm
by RobertGonzalez
Keep the code the way it is. Just make sure to run through your array vars as arrays, not strings, and all the errors should be fixed.