Forum Moderators: coopster

Message Too Old, No Replies

Dropdown to trigger MySql Query

get dropdown to query database

         

wbrother

1:04 pm on Aug 30, 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. The query counts the number of entries and displays the total on the page.

<?php

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

$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());

$select = $_POST['select'];

if (!empty($select)) {

$chooser = $_POST['chooser'];

if(isset($_POST['chooser'])) {
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>
?>

Help is greatly appreciated!

[edited by: eelixduppy at 2:55 pm (utc) on Aug. 30, 2008]
[edit reason] removed credentials [/edit]

cameraman

4:43 pm on Aug 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World, wbrother.

You need to move the query down below where you define $chooser:
$chooser = $_POST['chooser'];

if(isset($_POST['chooser'])) {
$query = "SELECT COUNT(*) AS Total...

wbrother

7:05 pm on Aug 30, 2008 (gmt 0)

10+ Year Member



Thankyou for your kind welcome. I have tried to get this to work for so long...I am hoping to move on soon... I now get the dropdown and button to load but when I 'select' The page reloads and I get.. "page not found". If possible I would rather have a "place" for the number rather than reloading the page each time since there will be three instances of this script on the same page. Only one number is returned from the query.
[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>

cameraman

7:28 pm on Aug 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If it's saying page not found then most likely the action attribute of your form isn't quite right. Web servers are often case sensitive, so if your script is Download or downLoad or DownLoad etc. you need to match that in ACTION.

The only way to keep the page from reloading is to use AJAX (javascript) to submit the form.

wbrother

7:53 pm on Aug 30, 2008 (gmt 0)

10+ Year Member



So how do I use AJAX and am I ever going to get this done? It seemed so simple at the time.

cameraman

8:03 pm on Aug 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The folks in the javascript forum can do a better job of telling you how to work AJAX. There are several javascript frameworks out & about that can simplify things. Yahoo has one that I found a bit large for my taste, and the only other one I've used is mootools, which I like quite a bit. Look at the mootools request object (you can google mootools). There's also a mootools tutorial at cnet (google cnet mootools).

Does it have to be three separate forms? You can have three selects on one form, that would simplify things quite a bit.

wbrother

8:10 pm on Aug 30, 2008 (gmt 0)

10+ Year Member



three selects in one form would be neater but I thought it would be more complex... I wanted to get just one done first (an you can see the frustration I've had with that!). The search is the same for all three except the table that is queried is different.