Pass variable through hidden field or form action?

This seems like such a simple problem but it’s been frustrating me for days!

There are 3 pages involved.

Page 1: user selects a location and is directed to 2nd page

Page 2: location selected from page 1 is displayed, user completes form with further data and is directed to page 3.

Page 3: all records for selected location is displayed, user has option to return to Page 2 to insert another record.

What happens is Page 1 works, Page 2 works, Page 3 doesn’t doesn’t “catch” the variable.

Here’s what I have (I’m not including Page 1 because there’s no issue with it)
Page 2:

<?php require_once('../Connections/conn_taylor.php');
$loc = $_POST['locID'];

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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;
}

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

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO testname (locID, name, number) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['locID'], "int"),
                       GetSQLValueString($_POST['name'], "text"),
                       GetSQLValueString($_POST['number'], "text"));

  mysql_select_db($database_conn_taylor, $conn_taylor);
  $Result1 = mysql_query($insertSQL, $conn_taylor) or die(mysql_error());

?>

<form action="test3a.php?locID=<?php echo $_POST['locID']" name="form1" method="POST">
<p>page 2a </p>
<p>Add name to # <?php echo $loc; ?>
<input name="locID" type="hidden" id="locID" value="<?php echo $loc; ?>"></p>
  Name:  <input name="name" type="text" id="name>
  Number:  <input name="number" type="text" id="number
<p>
    <input type="submit" name="Submit" value="Submit">
</p>
  <input type="hidden" name="MM_insert" value="form1">
</form>

Page 3:

<?php
$loc = $_POST['locID'];
?>
<form action="test2a.php?locID=<?php echo $loc; ?>" name="form1" method="POST">
  <p>Page 3</p>
  <p>the location is <?php echo $loc; ?>
    <input name="locID" type="hidden" id="locID" value="<?php echo $loc; ?>">
</p>
  <p>go to page 2
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>

I have done so many permutations of this, this is just the current one. I have had it working to page 3 where the location is displayed but going back to page 2 gets an error message. Other times it all seems to work, going from page to page with no problems, but the data isn’t inserted into the table. I’m about to go crazy! Any and all help appreciated, for the sake of my sanity.

Oh yes, the title of this post is one of my questions: I’m not sure whether page 2 should have the locID variable in the form action or pass it in a hidden field. I’ve tried both/and with sketchy results. My main problem at this point is why isn’t Page 3 catching the variable?

Okay, I can see where you’re getting confused.

If you put the variable in the form’s ACTION, then it’s in the $_GET array, no matter what method attribute you use.

If it’s on the URL, it’s in $_GET.
If it was passed from a form field (Note: the form’s own Action is NOT a form field) with method=post, then it’s in $_POST.

It is -entirely- possible to have both $_GET and $_POST variables in the same request.

So. Page1 the value is a form field, so on page 2, $_POST[‘locID’] is the correct way to retrieve it. Page 2 then puts that information into the action URL for page 3. It then should be looking for $_GET[‘locID’], if you’re going to pass it this way. If you’re going to use the hidden field, see below.

PS: You forgot to close your PHP tag at the end of the action on page 2, which is probably causing you errors.

That’s it!! Thank you so much Starlion. Now, hopefully it’ll still work when I have to put other stuff in :slight_smile:

BTW, see below where?

That was actually an editting error when I was copying the code. It’s correct on the actual page.

Well without seeing the actual code, if you’ve got it in both places it should be being passed into BOTH arrays (which is a bit redundant).

Based on how Page2 gets its data, and the fact you want Page3 to point back to Page2, I would recommend going with just the form field element. Page1 -will- be passing it as a formfield, so page2 is listening for that. Page3 therefore should use a formfield to send it back to page2. And page 2 should use it to pass to page3 just for uniformity’s sake.

I have tried that, using the hidden field and _POST in both pages, and while the correct location shows up, the record doesn’t get inserted. Which tells me that somehow mysql isn’t seeing the info. I can’t post the code here at the moment because I’m not at my own computer. I’ll post it when I’m back.

If it seems mysterious what PHP is accepting from your POST forms then start doing this temporarily at the top of the page dealing with the insert:


// view what is being passed
// comment the line out when you get fed up
// of seeing it, remove it completely before 
// going 'live'
var_dump($_POST);

That way you will have some idea which direction to go looking in,whether it is back your HTML form, PHP itself or forward on to Mysql which is giving you problems.

Another line of debug you might add just prior to your mysql_select_db line is:


// again, comment it out when the prob is fixed
echo  $insertSQL;

Just what is PHP telling mysql to do?

You can then copy the output of the echo, paste it into whatever tool you use to manage your database and then also prove that there is some corresponding data in your database.