Jump to content

$_POST vars being unset after 2nd form submission. pre-selects form vals lost???


boo_lolly

Recommended Posts

i'm working on CMS for a bridal registry. i'm working on the page where the user can add items to a bridal registry. i wrote this page back when i was a noob and now i'm going back to make it more versatile and just plain better! i used to have this page post to a redirect page which executed the query (which is inserting items into the registry database), and redirected the user back to the addItems.php page. that is a terribly noob way to do it. so i'm re-coding it to post to itself.

 

basically, the story here is there are 3 forms on this page. the first form is a dropdown menu populated from a shoppingcart database. it populates from the names of categories in the shoppingcart. the user selects an option, and posts. now, the first dropdown menu has the option pre-selected, and next to it there is a second dropdown menu. this second dropdown menu is populated from items that fall under the selected category. all that works just fine. but when the user selects an item from the second dropdown, the first pre-selected form value get's lost, and (as it should) the second dropdown menu isn't displayed. what is supposed to happen, is after the user selects from the second dropdown menu (items), a third form will appear next to it. this form has 2 input fields. once this form is filled out, then the if condition will check to see if the last form has been submitted, then it will insert all the forms information into the database, and display a confirmation message or error message to the user, and the pre-selected values will be unset, and the page will display only the first dropdown menu (as its original state).

 

i'm pretty sure my problems are where i set my $_POST categories and items variables... i'm not sure how to not loose the values, so they will remain while the user is using the forms step by step. i'm sorry there's so much code to look through, but it is heavily commented, so it should be pretty easy to understand what's going on.

 

p.s. i plan on adding features to remove SQL injections and whatnot, and the error/confirmation message, but i will do that later. right now this is the major issue holding me back. thanks for the help in advanced!

 

<?php
        /*connect to database*/
        @ $db = mysql_connect("xxx", "xxx", "xxx");
        if(!$db){
                echo "Error: Could not connect to the database. Please try again later.";
                exit;
        }
        /*select database*/
        mysql_select_db("my_db", $db);

        /*query newlywed's info*/
        $sql = "SELECT * FROM my_search_table WHERE uID = '". $regID ."'";
        $query = mysql_query($sql) OR die(mysql_error());
        $row = mysql_fetch_array($query);

        /*set registry ID*/
        $regID = $row['uID'];

        /*print newlywed info*/
        echo "<B>Bride and Groom's Name: </B>". $row['brideFname'] ." ". $row['brideLname'] ." & ". $row['groomFname'] ." ". $row['groomLname'] ."<br /><br />";
        echo "<B>Event Date: </B>". $row['event_month'] ."/". $row['event_day'] ."/". $row['event_year'] ."<br /><br />";
        echo "<B>Preferred Shipping Address: </B>". $row['ship_add'] .", ". $row['ship_city'] .", ". $row['ship_state'] .", ". $row['ship_zip'] ."<br /><br />";
        echo "<form action=\"updateRegistry.php?regID=". $regID ."\" method=\"post\">\n";
        echo "<input type=\"submit\" value=\"<< Back to Registry\"></form>\n";

        /*set dropdown vars*/
        if(isset($_POST['categories']) && $_POST['categories'] != ""){
                $categories = $_POST['categories'];
        }
        if(isset($_POST['items']) && $_POST['items'] != ""){
                $items = $_POST['items'];
        }

        /*add item to registry*/
        if(isset($_POST['execute_query'])){
        /*change catID to category name*/
                $sql = "SELECT * FROM categories
                        WHERE id = ". $categories ."";
                $query = mysql_query($sql) OR die(mysql_error());
                $row = mysql_fetch_array($query);
                $catName = $row['name'];

        /*change itemID to item name*/
                $sql = "SELECT * FROM items
                        WHERE id = ". $items ."";
                $query = mysql_query($sql);
                $row = mysql_fetch_array($query) OR die(mysql_error());
                $itemName = $row['name'];

        /*set quantity requested value*/
                if(empty($_POST['still_needs'])){
                        $_POST['still_needs'] = $_POST['qty_req'];
                }elseif($_POST['still_needs'] > $_POST['qty_req']){
                        $_POST['still_needs'] = $_POST['qty_req'];
                }

        /*convert qty_req and still_needs*/
                $qty_req = $_POST['qty_req'];
                $still_needs = $_POST['still_needs'];

        /*grab price and image from shoppingcart table*/
                $sql = "SELECT * FROM items WHERE id = ". $_GET['itemID'] ."";
                $query = mysql_query($sql) OR die(mysql_error());
                $row = mysql_fetch_array($query);
                $itemPhoto = $row['photo'];
                $itemPrice = $row['price'];

        /*generate unique item id*/
                $totalChar = 29;
                $salt = "ABCDEFHIJKLMNOPQRSTUVWXYZ0123456789---------";
                srand((double)microtime()*1000000);
                $UitemID = NULL;
                for($i = 0; $i < $totalChar; $i++){
                        $UitemID = $UitemID . substr($salt, rand() % strlen($salt), 1);
                }

        /*insert item into registry*/
                $addQuery = "INSERT INTO my_reg_table (id, uID, category, item, qty_req, still_needs, UregID, image, price)
                             VALUES('', '". $_GET['regID'] ."', '". $categories ."', '". $items ."',
                                    '". $qty_req ."', '". $still_needs ."', '". $UitemID ."',
                                    '". $itemPrice ."', '". $itemPhoto ."')";
                mysql_query($addQuery) OR die(mysql_error());

        /*unset variables*/
                unset($categories, $items, $_POST['execute_query'], $qty_req, $still_needs);
        }

        echo "<CENTER><HR WIDTH=\"250\"></CENTER><br />\n";

        echo "<TABLE BORDER=\"0\"><TR>\n";
        echo "<TD align=\"left\" valign=\"bottom\">";

        echo "<form action=\"". $_SERVER['php_self'] ."?regID=". $regID ."\" method=\"post\">\n";
        echo "<SELECT NAME=\"categories\">\n";
        echo "<OPTION VALUE=\"\">Choose a Category\n";

        /*query category info*/
        $cat_sql = mysql_query("SELECT * FROM categories") or die(mysql_error());

        /*populate dropdown menu from the shopping cart product table*/
        while($cat_row = mysql_fetch_array($cat_sql)){
                echo "<OPTION VALUE=\"". $cat_row['id'] ."\"". (($categories == $cat_row['id']) ? (" SELECTED") : ("")) .">". $cat_row['name']. "\n";
        }

        echo "</SELECT>\n";

        echo "<input type=\"submit\" value=\">>\">\n";
        echo "</form>\n";
        echo "</TD>";

        /*if a category hasn't been chosen, don't display anything else, yet*/
        if(!isset($categories) || $categories == ""){
                mysql_close($db);
                exit;
        }else{
       /*
        *if a category has been chosen,
        *use this switch statement to populate
        *the item list that falls under the
        *chosen category
        */
                switch($categories){
                        case $categories:
                                /*connect to database*/
                                $db = mysql_connect("xxx", "xxx", "xxx");
                                if(!$db){
                                        echo "Error: Could not connect to the database. Please try again later.";
                                        exit;
                                }
                                /*select database*/
                                mysql_select_db("my_db", $db) OR die("Error: Could not connect to the database");

                                /*now, we get the items that are in that category*/
                                $item_sql = mysql_query("SELECT id, name, cat FROM items WHERE cat = ". $categories ."") or die(mysql_error());
                                $item_row = mysql_fetch_array($item_sql);

                                echo "<TD align=\"left\" valign=\"bottom\">";
                                echo "<form action=\"". $_SERVER['php_self'] ."?regID=". $regID ."\" method=\"post\">\n";
                                echo "<SELECT NAME=\"items\">\n";
                                echo "<OPTION VALUE=\"\">Select an Item\n";

                                /*populate the matching category's item list here*/
                                while($item_row = mysql_fetch_array($item_sql)){
                                        echo "<OPTION VALUE=\"". $item_row['id'] ."\"". (($items == $item_row['id']) ? (" SELECTED") : ("")) .">". $item_row['name'] ."\n";
                                }
                                echo "</SELECT>\n";

                                echo "<input type=\"submit\" value=\">>\"><br />\n";
                                echo "</form>\n";
                                echo "</TD>";

                                break;

                        default:
                                break;
                }
        }

       /*
        *the $item value is set within the switch statement.
        *none of this below is displayed without the user first
        *selecting a category, then through the switch statement
        *selecting an item within that category.
        *if an item isn't set, don't display anything.
        *if an item has been seleceted, show the form for
        *quantity requested, and still needs
        */
        if(isset($_POST['items'])){
                echo "<form action=\"". $_SERVER['php_self'] ."?regID=". $regID ."\" method=\"post\">";
                echo "<TD align=\"right\" valign=\"bottom\">\n";
                echo "<B>Quantity Requested:</B> ";
                echo "<input type=\"text\" name=\"qty_req\" size=\"3\"><br />\n";
                echo "<B>Still Needs:</B> ";
                echo "<input type=\"test\" name=\"still_needs\" size=\"3\">\n";
                echo "</TD></TR>";
                echo "<TR><TD COLSPAN=\"3\" align=\"right\">\n";
                echo "<input type=\"submit\" value=\"Submit\"></TD></TR></TABLE>\n";
                echo "<input type=\"hidden\" name=\"execute_query\" value=\"execute\">\n";
                echo "</form>\n";
        }
?>

Link to comment
Share on other sites

this is very weird activity that i cannot explain. i added some debugging features...

<?php
        echo "before ifcondition...<br />\n";
        echo "\$\_POST['cat'] = ". $_POST['categories'] ."<br />\n";
        echo "\$\_POST['items'] = ". $_POST['items'] ."<br />\n";

        /*set dropdown vars*/
        if(isset($_POST['categories']) && $_POST['categories'] != "" && $_POST['categories'] != NULL){
                $categories = $_POST['categories'];
                if(isset($_POST['items']) && $_POST['items'] != "" && $_POST['items'] != NULL){
                        $items = $_POST['items'];
                }
        }

        echo "after if condition<br />\n";
        echo "categories = ". $categories ."<br />\n";
        echo "items = ". $items ."<br />\n";
?>

 

what happens is when the first dropdown menu is selected, both $_POST['categories'] and $categories have the same value... but when the items dropdown is selected, both $_POST['categories'] and $categories lose their value... but $_POST['items'] and $items have the same value... why would they even have a value, if their values won't be set unless $_POST['categories'] is set? this doesn't make any sense...

 

 

is $categories being unset everytime php runs through this if statement? i'm trying to hold on to this value once it is set...

 

the out put after submitting the first drop down is:

before ifcondition...
$_POST['cat'] = 2
$_POST['items'] =

after if condition
categories = 2
items = 

 

after submitting the second dropdown:

before ifcondition...
$_POST['cat'] = 
$_POST['items'] = 13

after if condition
categories = 
items = 13

 

that doesn't make any sense...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.