Forum Moderators: open

Message Too Old, No Replies

Query MySql Database with Dropdown

dropdown not dynamically populated

         

wbrother

12:50 pm on Aug 31, 2008 (gmt 0)

10+ Year Member



I have been trying for days to get this to work. I'm sure it's chopped up by now and I desparately need help to get the script to work.. The query it'self works when I put a number in place of $chooser. The problem is getting the query to display when it is selected from the dropdown menu and the 'select' button is pushed. Since the query only returns one number I could also have a designated place on the page where the result would appear. I was told that AJAX would probably be best to prevent the page from refreshing when the query was done. I don't know anything about AJAX and could use some real help to get this to work. Thank you to everyone...
[code]
<?php

mysql_connect('localhost', '', '') or die(mysql_error());
mysql_select_db("dwbrothe_plst1") or die(mysql_error());

$select = $_POST['select'];

if (!empty($select)) {

$chooser = $_POST['chooser'];

if(isset($_POST['chooser'])) {
$query = "SELECT COUNT(*) AS Total FROM phplist_user_user_attribute puua INNER JOIN phplist_listattr_visitor pllv ON puua.value = pllv.id WHERE pllv.id = $chooser";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

while($row = mysql_fetch_assoc($result)){
foreach($row as $key => $value){
echo $key.': '.$value.'<br>';
}
echo '<br>';
}
}
}
?>

<form action="dropdown.php" method="post">

<select name="chooser">

<option value="2">Visitor</option>
<option value="3">Parent</option>
<option value="4">Student</option>
<option value="5">Alumni</option>
<option value="6">Sponsor</option>

</select>

<input type="submit" value="Select" name="select">

</form>

Fotiman

2:11 pm on Sep 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The example you posted looks correct to me. That is, if you open the HTML page, select a value for 'chooser', and click the submit button, you should be taken to dropdown.php which would display the results. That's good. You want your AJAX solution to be a progressive enhancement so that the page will continue to work even if someone has JavaScript disabled.

I would recommend using a framework to handle the AJAX calls. The Yahoo UI Library has it's Connection Manager [developer.yahoo.com] which is well documented. Essentially, what you'll want to do is add an onsubmit event listener to your form that will stop the event (preventing the default form submit from happening), and then using AJAX to submit the form data. You'll create a callback method that handles the output of the dropdown.php page, which you can then insert into your page. I know I've been a little vague, but I'm hoping that you'll be able to figure it out based on this suggestion. Let us know if you need more help.