Jump to content

Dropdowns And Selections


SteveNI

Recommended Posts

I hope someone can offer me some guidance

I am looking to populate a dropdown box with values from a database column

Then when the user selects one of the dropdown options it then runs a second query that will produce a results table.

I have my code to populate the dropdown

<?php
include ('connect-To-db.php');

$query = $mysqli->query("Select distinct`catagory` FROM `transactions` WHERE `Debit`!=0");

echo"<select name = 'catagory'>";

while ($row = $query->fetch_object()){
   echo "<option value= '".$row->catagory."'>".$row->catagory."</option>";
}
echo "</select>";

?>

 

And I also have my code that will query the database to produce the results I need

<?php

include ('connect-To-db.php');

 

if($query = $mysqli->query("Select `ThirdParty`,`Debit`,`Date` FROM `transactions` WHERE `Catagory` = 'shopping'")){

if($query->num_rows>0){

echo"<table border ='1' cellpadding='10'>";

echo "<tr><th>Payee</th><th>Amount</th><th>Date</th></tr>";

 

while($row=$query->fetch_object()){

echo "<tr>";

echo "<td>".$row->ThirdParty."</td>";

echo "<td>".$row->Debit."</td>";

echo "<td>".$row->Date."</td>";

echo "</tr>";

}

echo "</table>";

}

else{

echo 'No results to return';

}

}

else{

echo 'ERROR'.$mysqli->error;

}

 

?>[code]

 

What I dont quite understand is what I need to do to get the two pieces of code to run together. Can anyone point me in the right direction?

Link to comment
Share on other sites

Unless I'm misunderstanding you... I'd think you would want to split it into two pages:

 

-- On the first page, you'd have your generated dropdown menu within a form. The user would then select from the dropdown menu, and press the submit button, which would redirect them to a new page using the form's "action" value

 

-- On the second page, you'd retrieve the value from the dropdown by using $_POST, and display the results to the user.

 

Hope that helps get you started?

Link to comment
Share on other sites

Unless I'm misunderstanding you... I'd think you would want to split it into two pages:

 

-- On the first page, you'd have your generated dropdown menu within a form. The user would then select from the dropdown menu, and press the submit button, which would redirect them to a new page using the form's "action" value

 

-- On the second page, you'd retrieve the value from the dropdown by using $_POST, and display the results to the user.

 

Hope that helps get you started?

Thanks Ben

 

I was hoping to refrain from making the user press a submit button, I was hoping that they could could simply select an option in the dropdown menu and this would in turn would run the necessary query to produce a table.

I am reading things about ajax and javascript onClick but to be honest I dont really understand what needs to be done.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...