ltbaggz Posted January 12, 2007 Share Posted January 12, 2007 Here is what im trying to do, populate a drop down/list menu from a database query(this is done). having a name and a associated value with it. when it changes i want to be able to access the value associated with the chosen element. however i dont want to have a standard form with a submit button. the process will be, pick a value from the dropdown or list then associate this value in a GET style link. I am thinking that im going to need some kind of javascript function using the onchange attribute of the dropdown but then im not sure how to get it into php variable that does the query. ahh well i just read over it and it makes sense to me but someone who has no idea what im talking about might not...hopefully this makes sense. Link to comment https://forums.phpfreaks.com/topic/33842-drop-down-menu/ Share on other sites More sharing options...
eric_ Posted January 18, 2007 Share Posted January 18, 2007 I'm trying to do something similar. I'm trying to set up a form-based dropdown list so that the item you select determines which table in the database gets populated. Out of curiosity, is $result some kind of default variable for information submitted via a form? If so, I think all I have left to do is set up an if...then statement that changes $result to a variable associated with each table. Another possibility is that there's a way to use the option value definition as a variable and I just don't know how.FYI - All of my database connection data is kept in an include file. What you see below is only the code pertaining to the form and form submission. Also, the form successfully submits data to the database, but it submits it to every table, not just the one selected in the dropdown list.Any help will be much appreciated!Here's the code:[quote]<div id="nonFooter"><?php require("include/header.php"); ?> <div id="main"> <h1>Lodging submission form</h1><?php// code that will be executed if the form has been submitted:if ($submit) {//here's where I need to figure out how to associate $result with something. Is there a way to create a variable out of the option value in the form? $hotel=MYSQL_QUERY("INSERT INTO `Hotels` (area,name,link,phone,address,email) ". "VALUES ('$form_area','$form_name','$form_link','$form_phone','$form_address','form_email')"); $bb=MYSQL_QUERY("INSERT INTO `Bed and Breakfast` (area,name,link,phone,address,email) ". "VALUES ('$form_area','$form_name','$form_link','$form_phone','$form_address','form_email')"); $house=MYSQL_QUERY("INSERT INTO `House Rentals` (area,name,link,phone,address,email) ". "VALUES ('$form_area','$form_name','$form_link','$form_phone','$form_address','form_email')"); $other=MYSQL_QUERY("INSERT INTO `Other Lodging Options` (area,name,link,phone,address,email) ". "VALUES ('$form_area','$form_name','$form_link','$form_phone','$form_address','form_email')"); $id = mysql_insert_id(); print "<p><a href=\"lodging.php\">View your post</a></p>"; MYSQL_CLOSE();} else { // else show the form to submit new data: $hotel_table = mysql_query("SELECT * FROM `Hotels`"); $hotel_name = mysql_field_table($hotel_table, 'name'); $bb_table = mysql_query("SELECT * FROM `Bed and Breakfast`"); $bb_name = mysql_field_table($bb_table, 'name'); $house_table = mysql_query("SELECT * FROM `House Rentals`"); $house_name = mysql_field_table($house_table, 'name'); $other_table = mysql_query("SELECT * FROM `Other Lodging Options`"); $other_name = mysql_field_table($other_table, 'name');?> <form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data"> <select name="category"> <option value="hotel"><?php echo $hotel_name; ?></option> <option value="bb"><?php echo $bb_name; ?></option> <option value="house"><?php echo $house_name; ?></option> <option value="other"><?php echo $other_name; ?></option> </select><br /> Location:<br /> <input type="text" name="form_area" size="40"> <br>Name:<br> <input type="text" name="form_name" size="40"> <br>Link:<br> <input type="text" name="form_link" size="40"> <br>Phone:<br> <input type="text" name="form_phone" size="40"> <br>Address:<br> <input type="text" name="form_address" size="40"> <br>E-mail:<br> <input type="text" name="form_email" size="40"> <p><input type="submit" name="submit" value="submit"> </form> </div></div><?php//closes else statement}?>[/quote] Link to comment https://forums.phpfreaks.com/topic/33842-drop-down-menu/#findComment-163649 Share on other sites More sharing options...
Meep3D Posted January 18, 2007 Share Posted January 18, 2007 This may be what you want.[code] <form action="admin.php" method="post"> <select name="gallery[select_gallery]" onChange="this.form.submit()" > <option value="Default" >Default</option> <option value="Index" >Index</option> <option value="Gallery" >Gallery</option> <option value="Info" >Info</option> <option value="WinterHols" selected>WinterHols</option> <option value="Villages" >Villages</option> <option value="OtherInfo" >OtherInfo</option> <option value="Beaches" >Beaches</option> <option value="Links" >Links</option> </select> <input type="hidden" name="gallery[select_image]" value="" /> <input type="submit" name="formsubmit" value="Submit" class="submit"/> </form>[/code]It automagically changes page as soon as a new item is selected from the drop down. All you need to do is put [b] onChange="this.form.submit()"[/b] within the <select> tag. I have included a submit button for usability's sake - without it it's impossible to change menu without Javascript. Note: You cannot call the submit button 'submit' or auto-submit doesn't work, but you can get rid of it entirely if you want, it'll still work. Link to comment https://forums.phpfreaks.com/topic/33842-drop-down-menu/#findComment-163661 Share on other sites More sharing options...
eric_ Posted January 18, 2007 Share Posted January 18, 2007 Thanks!Hrm. I set that up, but the info doesn't populate the database upon autosubmission. Also, I'd like to be able to have the selection from the dropdown list determine where the info goes when you hit submit at the bottom, rather than having the info submit when you make your selection... Link to comment https://forums.phpfreaks.com/topic/33842-drop-down-menu/#findComment-163671 Share on other sites More sharing options...
eric_ Posted January 18, 2007 Share Posted January 18, 2007 What about the document.write javascript function? Could I use that in the <option> lines to change $result to the relevant variable ($hotel, $house, etc)?I tried constructing the string, but I hardly know javascript at all. Here's what I came up with:[code]<option value="hotel" onClick="javascript:document.write('<a href="lodging.php?result='+hotel+'"><?php echo $hotel_name; ?></a>')"></option>[/code]OR[code]<option value="hotel" onClick="javascript:document.write('<a href="lodging.php?result='+hotel+'"><?php echo $hotel_name; ?></a>')"></option>[/code]OR[code]<option value="hotel"><javascript:document.write('<a href="lodging.php?result='+hotel+'"><?php echo $hotel_name; ?></a>')></option>[/code]In the second example, I used " around the url to which the variable should be submitted. The first example results in some code displaying in the drop down (i.e. - Hotel')"> ), the second example results in a blank space where "Hotel" should be, and the third example displays like the first one. At any rate, am I at all on the right track? Link to comment https://forums.phpfreaks.com/topic/33842-drop-down-menu/#findComment-163694 Share on other sites More sharing options...
eric_ Posted January 18, 2007 Share Posted January 18, 2007 Sorry, one more...I feel like I'm close...I just can't figure out how to make it so selecting a drop down option will set the variable[code]<div id="nonFooter"><?php require("include/header.php"); ?> <div id="main"> <h1>Lodging submission form</h1><?php// code that will be executed if the form has been submitted:if ($submit) { if ($hotel) { $hotel=MYSQL_QUERY("INSERT INTO `Hotels` (area,name,link,phone,address,email) ". "VALUES ('$form_area','$form_name','$form_link','$form_phone','$form_address','form_email')");} elseif ($bb) { $bb=MYSQL_QUERY("INSERT INTO `Bed and Breakfast` (area,name,link,phone,address,email) ". "VALUES ('$form_area','$form_name','$form_link','$form_phone','$form_address','form_email')");} elseif ($house) { $house=MYSQL_QUERY("INSERT INTO `House Rentals` (area,name,link,phone,address,email) ". "VALUES ('$form_area','$form_name','$form_link','$form_phone','$form_address','form_email')");} elseif ($other) { $other=MYSQL_QUERY("INSERT INTO `Other Lodging Options` (area,name,link,phone,address,email) ". "VALUES ('$form_area','$form_name','$form_link','$form_phone','$form_address','form_email')");} $id = mysql_insert_id(); print "<p><a href=\"lodging.php\">View your post</a></p>"; MYSQL_CLOSE();} else { // else show the form to submit new data: $hotel_table = mysql_query("SELECT * FROM `Hotels`"); $hotel_name = mysql_field_table($hotel_table, 'name'); $bb_table = mysql_query("SELECT * FROM `Bed and Breakfast`"); $bb_name = mysql_field_table($bb_table, 'name'); $house_table = mysql_query("SELECT * FROM `House Rentals`"); $house_name = mysql_field_table($house_table, 'name'); $other_table = mysql_query("SELECT * FROM `Other Lodging Options`"); $other_name = mysql_field_table($other_table, 'name');?> <form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data"> <select name="category" onChange="<?php $result=$select; ?>"> <option value="hotel"> <?php echo $hotel_name; $select=("$hotel"); ?> </option> <option value="bb"> <?php echo $bb_name; $select=("$bb"); ?> </option> <option value="house"> <?php echo $house_name; $select=("$house"); ?> </option> <option value="other"> <?php echo $other_name; $select=("$other"); ?> </option> </select><br /> Location:<br /> <input type="text" name="form_area" size="40"> <br>Name:<br> <input type="text" name="form_name" size="40"> <br>Link:<br> <input type="text" name="form_link" size="40"> <br>Phone:<br> <input type="text" name="form_phone" size="40"> <br>Address:<br> <input type="text" name="form_address" size="40"> <br>E-mail:<br> <input type="text" name="form_email" size="40"> <p><input type="submit" name="submit" value="submit"> </form> </div></div><?php//closes else statement}?>[/code] Link to comment https://forums.phpfreaks.com/topic/33842-drop-down-menu/#findComment-163759 Share on other sites More sharing options...
eric_ Posted January 18, 2007 Share Posted January 18, 2007 OK, my cousin and I figured it out:[code]<?php// code that will be executed if the form has been submitted://THIS IS THE KEY TO MAKING IT WORK!$select = $HTTP_POST_VARS['category'];if ($submit) { // the $select variable definition allows you to write to each table individually using just this one line $query=MYSQL_QUERY("INSERT INTO `$select` (area,name,link,phone,address,email) ". "VALUES ('$form_area','$form_name','$form_link','$form_phone','$form_address','form_email')"); $id = mysql_insert_id(); print "<p><a href=\"lodging.php\">View your post</a></p>"; MYSQL_CLOSE();} else { // else show the form to submit new data: $hotel_table = mysql_query("SELECT * FROM `Hotels`"); $hotel_name = mysql_field_table($hotel_table, 'name'); $bb_table = mysql_query("SELECT * FROM `Bed and Breakfast`"); $bb_name = mysql_field_table($bb_table, 'name'); $house_table = mysql_query("SELECT * FROM `House Rentals`"); $house_name = mysql_field_table($house_table, 'name'); $other_table = mysql_query("SELECT * FROM `Other Lodging Options`"); $other_name = mysql_field_table($other_table, 'name');?> <form method="post" target="_self" enctype="multipart/form-data"> <select name="category"> <option value="<?php echo $hotel_name; ?>"><?php echo $hotel_name; ?> </option> <option value="<?php echo $bb_name; ?>"><?php echo $bb_name; ?> </option> <option value="<?php echo $house_name; ?>"><?php echo $house_name; ?> </option> <option value="<?php echo $other_name; ?>"><?php echo $other_name; ?> </option> </select><br /> Location:<br /> <input type="text" name="form_area" size="40"> <br>Name:<br> <input type="text" name="form_name" size="40"> <br>Link:<br> <input type="text" name="form_link" size="40"> <br>Phone:<br> <input type="text" name="form_phone" size="40"> <br>Address:<br> <input type="text" name="form_address" size="40"> <br>E-mail:<br> <input type="text" name="form_email" size="40"> <p><input type="submit" name="submit" value="submit"> </form> </div></div><?php//closes else statement}?>[/code] Link to comment https://forums.phpfreaks.com/topic/33842-drop-down-menu/#findComment-163856 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.