Page 1 of 1

Populate DB with results from dropdown menus?

Posted: Thu Jun 22, 2006 7:53 am
by JimiH
Hi probably real easy solution, but i cant find the anwser

I have two dropdown menus on a form this pulls in values from a DB, how do I record the selected values
and insert them into another table.

Code: Select all

///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category"); 
///////////// End of query for first list box////////////

/////// for second drop down list we will check if category is selected else we will display all the subcategory///// 
if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory"); 
}else{$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory order by subcategory"); } 
////////// end of query for second subcategory drop down list box ///////////////////////////

echo "<form method=post name=f1 action='dd-check.php'>";
/// Add your form processing page address to action in above line. Example  action=dd-check.php////
//////////        Starting of first drop downlist /////////
echo "<select name='cat' onchange=\"reload(f1)\"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) { 
if($noticia2['cat_id']==@$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}
else{echo  "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}
}
echo "</select>";
//////////////////  This will end the first drop down list ///////////

//////////        Starting of second drop downlist /////////
echo "<select name='subcat'><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) { 
echo  "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
}
echo "</select>";
//////////////////  This will end the second drop down list ///////////
//// Add your other form fields as needed here/////
Thanks

Geoff

Posted: Thu Jun 22, 2006 8:00 am
by GM
After you submit the form, the values become available to the dd-check.php script in the $_POST superglobal array.

More specifically, $_POST['cat'] will contain the selected entry from the first dropdown, and $_POST['subcat'] will contain the selected entry from the second dropdown.

You can therefore use these values in an "INSERT" sql statement to add a new record to your table.

Posted: Thu Jun 22, 2006 9:33 am
by JimiH
How do I make the values available within the current php file, not dd-check.php which doesn't exist.

I added the following.

Code: Select all

$cat=$_POST['category'];
$subcat=$_POST['subcategory'];

    }  

    $query = "INSERT INTO upload2 (name, size, type, path, description, AuthorID, category, subcategory ) ".
             "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$description', '$session->username', '$cat', '$subcat')";

    mysql_query($query) or die('Error, query failed : ' . mysql_error());  

	
echo $query;
This produces the following

Code: Select all

INSERT INTO upload2 (name, size, type, path, description, AuthorID, category, subcategory ) VALUES ('test.doc', '10752', 'application/msword', 'C:/websites/user_login/uploads/8e750ae34bfc1c259fbce166afdb90c3.doc', 'qrqrqereqrerqerqe', '', '', '')
As you can see $cat & $subcat are empty?

Thanks

Geoff

Posted: Thu Jun 22, 2006 9:56 am
by GM
PHP cannot know what values the user has selected until the form has been submitted. The user must click some sort of "submit" button before PHP can be aware of their choice. You can perform some client side processing using Javascript, and you can make the page interact with the server using AJAX, but the "traditional" method requires that the form is submitted to the server. At this point, the user's choices are known and can be used by the script in the form's action.

Posted: Thu Jun 22, 2006 4:15 pm
by JimiH
So, how would the two files look?

Sorry for this being to simple, but I cant find anything on the net with this example of selecting from a list then posting it into another table.

Thanks for your help

Geoff

Posted: Fri Jun 23, 2006 3:14 am
by GM
Personally, I tend to use the same document for both the form and the post-processing, so in theory you only need one file, that could look something like this:

Pseudocode:

I'll post the code when the thing will let me do so...


Things to note:
1) In the action of the form, we've put the current script - basically the action of the form defines which script is going to deal with the user's input. This value can be any valid script, which obviously needs to contain the logic for dealing with values introduced by the user. Since we've included that logic in the same script, we can use PHP_SELF to tell the script to call itself when the user clicks "Save". If the action parameter is omitted, it is assumed to be the same as saying "PHP_SELF".

2) The submit button. This allows processing of the form to start. When the user clicks submit, all the values in the form are saved into the superglobal array specified in the form's "METHOD" parameter - in this case "POST". So all the values from the form are passed into the $_POST array, and passed to the script in the form's "ACTION" parameter.

3) The processing logic. We check to see whether certain fields are populated in the $_POST array. If the fields are set, then we know that the form has been submitted. If the fields are not set, then we need to show the form to the user. We check the values of the $_POST variables against the array that we used to populate the dropdowns in the first place - this way we are sure that the values are valid. Once we are sure that the values are valid, we save them to the database, using an SQL insert.

Hope this helps,

GM

Posted: Fri Jun 23, 2006 7:03 am
by JimiH
Thanks GM

Look forward to seeing the code!

Geoff

Posted: Fri Jun 23, 2006 7:51 am
by GM
Sorry - just can't get it to work. I get an internal server error every time I try and post it.

If you PM me an email address, I'll send you it.