DSTR3 Posted January 10, 2013 Share Posted January 10, 2013 I have a button. When I click on the button I want to populate a listbox in AS order. (I would like the listbox to start out blank. Then when the button is clicked, the listbox populates. I have this so far, but when I open the page the box is already populated and the order is far from ASC. Any help s appreciated. Thank you. <?php try { $objDb = new PDO('mysql:host="";dbname=""', '', [email="'@1973'"]'[/email]); $objDb->exec('SET CHARACTER SET utf8'); $sql = "SELECT * FROM `tblRestaurants` WHERE `RestStatus` = 'YES' ORDER BY 'RestName' ASC"; $statement = $objDb->query($sql); $list = $statement->fetchAll(PDO::FETCH_ASSOC); } catch(PDOException $e) { echo 'There was a problem'; } ?> <!DOCTYPE HTML> <html lang="en"> <head> <meta charset="utf-8" /> <title>First Box</title> <meta name="description" content="First Box" /> <meta name="keywords" content="First Box" /> <link href="/BigStuff/css/core.css" rel="stylesheet" type="text/css" /> <!--[if lt IE 9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> </head> <body> <div id="wrapper"> <div align="center"> <form name="form1" method="post" action=""> <div align="left">First Box <input type="submit" name="First Box" id="First Box" value="Submit"> <select name="Place" size="25" id='Place' class="update"> <option value="">Select one</option> <?php if (!empty($list)) { ?> <?php foreach($list as $row) { ?> <option value="<?php echo $row['RestID']; ?>"> <?php echo $row['RestName']; ?> </option> <?php } ?> <?php } ?> </select> </div> <script src="/BigStuff/js/jquery-1.6.4.min.js" type="text/javascript"></script> <script src="/BigStuff/js/core.js" type="text/javascript"></script> </body> </html> Quote Link to comment Share on other sites More sharing options...
Madchen Posted January 10, 2013 Share Posted January 10, 2013 Because when page is loading your part of code for retrieving data from database is run and your $list is populated resulting in populating select box. Now to do that with button perfect solution would be AJAX or JS. Quote Link to comment Share on other sites More sharing options...
DSTR3 Posted January 10, 2013 Author Share Posted January 10, 2013 (edited) Thank you for your reply. But how do I stop my listbox from loading, and how do I add the button? I am new to all of this, so your help by example would be greatly appreciated! Thank you, once again. Also I greatly reduced the code to make this whole thing clearer. <?php $dbc = mysql_connect('', '',') or die('Error connecting to MySQL server.'); mysql_select_db(''); $result = mysql_query("select * from tblRestaurants order by RestName ASC"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>MENUHEAD</title> </head> <body> <p><center>SEARCH</CENTER></P> <select name="RestName"> <?php while ($nt= mysql_fetch_assoc($result)) { echo '<option value="' . $nt['RestID'] . '">' . $nt['RestName'] . '</option>'; } ?> </select> <p>Click "SUBMIT" to display the calculation results</p> <input type="submit" name="Submit" value="Submit" /> <br /> </form> </body> </html> Edited January 10, 2013 by DSTR3 Quote Link to comment Share on other sites More sharing options...
Barand Posted January 10, 2013 Share Posted January 10, 2013 An easier alternative, without AJAX, is to have two divs or selects 1 - visible with an empty select box 2- hidden with a populated select box On clicking the the button hide one and show the other Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.