SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member DroppedAtBirth's Avatar
    Join Date
    Apr 2001
    Location
    Tempe, Az
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    HTTP form passed variables problem

    I am working on a php script and have run into a problem. On one page I have checkboxes being selected and then submited via a form post. The problem is the values and names of the checkboxes change each time the script is ran. So I have to know what they are to call them on the next page. Is there any way to retrieve all the HTTP passed variables in php that where passed without knowing the variable name?
    ~~~~~~~~~~~~~~~~~~~~~
    Robert Ward
    webmaster@droppedatbirth.com
    ~~~~~~~~~~~~~~~~~~~~~

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes there is. All the POST data will be in the associative array $HTTP_POST_VARS

    So you can traverse the array and extract the $key/value pairs of all the associative elements.
    PHP Code:
    <?

    while( list($key$value) = each($HTTP_POST_VARS) )
    {
       echo 
    "element $key has the value $value <br>";
    }
    ?>
    You can perform any other array function on the array $HTTP_POST_VARS -such as
    count($HTTP_POST_VARS)
    to tell you the number of form elements that were returned.

    Another thing that I like to do when generating the form with a variable number of checkboxes with different names is to put them into an array and embed in the form a synchronized array that holds the keys.

    Here is an example of what I mean:
    PHP Code:
    <?
    // lets say this is the current set of values
    // and we are going to
    $currentSettings = array("foo"=>1"bar"=>0"zoot"=>0);

    // now print out a form from which the user can
    // modify the settings using checkboxes

    echo '<form name="settings" method="POST" action="foo.php">';

    while( list(
    $key,$value) = each($currentSettings) )
    {
       echo 
    '<input type="hidden" name="newSettingKeys[]" value="'$key'">',
            
    '<input type="checkbox" name="newSettingValues[]" value="1"',
            
    $value 'CHECKED' '',
            
    '>';
    }
    echo 
    '<input type="submit" value="submit"> </form>';
    ?>
    The above should (if I haven't made too many syntax errors) produce a form like:
    Code:
    <form name="settings" method="POST" action="foo.php">
    <input type="hidden" name="newSettingKeys[]" value="foo">
    <input type="checkbox" name="newSettingValues[]" value="1" CHECKED>
    <input type="hidden" name="newSettingKeys[]" value="bar">
    <input type="checkbox" name="newSettingValues[]" value="1">
    <input type="hidden" name="newSettingKeys[]" value="zoot">
    <input type="checkbox" name="newSettingValues[]" value="1">
    <input type="submit" value="submit"> </form>
    Now when you process that form data in "foo.php"
    PHP Code:
    <?
    //pack the data back up into an associative array
    $newSettings = array();

    for ( 
    $i 0; $ < count($newSettingsKeys); $i++ )
    {
       
    $key $newSettingsKeys[$i];
       
    $value $newSettingsValues[$i];
       
    $newSettings[$key] = $value;
    }
    ?>
    Last edited by freakysid; Sep 10, 2001 at 03:07.

  3. #3
    SitePoint Member DroppedAtBirth's Avatar
    Join Date
    Apr 2001
    Location
    Tempe, Az
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DAH! The same thing as in perl. I should have known that.

    Thanks

    The help is greatly appricated
    ~~~~~~~~~~~~~~~~~~~~~
    Robert Ward
    webmaster@droppedatbirth.com
    ~~~~~~~~~~~~~~~~~~~~~


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
  •