RE: [PHP-DB] Update fields on same page

From: Date: Wed, 20 Sep 2000 18:09:22 +0000
Subject: RE: [PHP-DB] Update fields on same page
Groups: php.db 
Request: Send a blank email to php-db+get-2997@lists.php.net to get a copy of this message
> This is a newbie question so please bear with me. > What is the best way to update text fields that are on the > same page as a drop-down list, when a user selects data > from a drop-down list? From the input of the drop-down > list I plan to query a table and populate the text fields, but > I want to do this without having to use a submit button > and going to a different page. You could do something like this (pardon the ugliness): <script language="php"> if( isset( $submit_form )) { // process form when user actually clicks // the "submit" button } echo "<form method=\"POST\" action=\"$PHP_SELF" target=\"_self\" name=\"productform\">\n"; $productsQuery = "SELECT product_name FROM products"; $productsResult = mysql( $dbname, $productsQuery ); echo "<select name=\"product_name\" onchange=\"document.productform.submit()\">\n"; while( $data = mysql_fetch_array( $productsQuery )) { echo "<option value=\"$data[product_name]\">$data[product_name]</option>\n"; } echo "</select>\n"; $itemQuery = "SELECT * FROM items WHERE product_name = \"$product_name\""; $itemResult = mysql( $dbname, $itemQuery ); $itemInfo = mysql_fetch_array( $itemResult ); $item_name = $itemInfo[item_name]; echo "Name: "; echo "<input type=\"text\" name=\"item_name\" value=\"$item_name\">\n"; $item_value = $itemInfo[item_value]; echo "Value: "; echo "<input type=\"text\" name=\"item_value\" value=\"$item_value\">\n"; echo "</form>\n"; </script> Disclaimer: there is no error checking in the above. It is just a basic guide to how this can be done. Basically what's happening is that whenever the user selects a new option, the form get submitted with the action being the same page. Once the form is submitted, $product_name will have a value and the query will pull the appropriate item. The first time through when $product_name does not have a value, nothing will be displayed because the query will fail. Chris

« previous php.db (#2997) next »