SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 Code:
    <?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 Code:
    <?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?
    Last edited by bcalagoure; May 13, 2011 at 10:57. Reason: clarity

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    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.
    Last edited by StarLion; May 13, 2011 at 11:48. Reason: Noticed the form field as well.

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    PS: You forgot to close your PHP tag at the end of the action on page 2, which is probably causing you errors.

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    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.
    That's it!! Thank you so much Starlion. Now, hopefully it'll still work when I have to put other stuff in

    BTW, see below where?
    Quote Originally Posted by StarLion View Post
    PS: You forgot to close your PHP tag at the end of the action on page 2, which is probably causing you errors.
    That was actually an editting error when I was copying the code. It's correct on the actual page.

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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:
    PHP Code:
    // 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:
    PHP Code:
    // 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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •