5 Replies - 734 Views - Last Post: 11 January 2009 - 10:34 AM

#1 jason-pc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 61
  • Joined: 10-January 09

selecting option on menu A changes the options available in menu B

Posted 10 January 2009 - 02:55 PM

i wish to be able to move topics around in my forum i have created, in my admin i done i have two options the category pull down menu and a topic menu, what i would like to do it when the category menu is selected the topic menu changes to show the topics available in that category and these topic 'titles' i got from the mysql database.

i have been told that i should use ajax but have no idea where i start to get data from the database using this.

can someone please help start me off.

<select name="categoryid"><? // get category_id
$get_category_titles = mysql_query("SELECT * FROM `forum_category` WHERE `confirmed` = '1' ORDER BY `created_on` DESC");
$get_topic_titles = mysql_query("SELECT * FROM `forum_topics` WHERE `confirmed` = '1' ORDER BY `created_on` DESC");

for ($i = 0; $i < mysql_num_rows($get_category_titles); $i ++) { ?><option<?
if ($posts['categoryid'] == mysql_result($get_category_titles, $i, 'category_id')) {echo(" selected");} ?> value="<?=mysql_result($get_category_titles, $i, 'category_id');?>"><?=mysql_result($get_category_titles, $i, 'category');?></option><?
} ?></select>


<select name="topicid"><? // get topics
for ($i = 0; $i < mysql_num_rows($get_topic_titles); $i ++) {
?><option<? if ($posts['topicid'] == mysql_result($get_topic_titles, $i, 'topic_id')) {echo(" selected");}
?> value="<?=mysql_result($get_topic_titles, $i, 'topic_id');?>"><?=mysql_result($get_topic_titles, $i, 'topic');?></option><?
} ?></select>


Is This A Good Question/Topic? 0
  • +

Replies To: selecting option on menu A changes the options available in menu B

#2 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,132
  • Joined: 07-September 06

Re: selecting option on menu A changes the options available in menu B

Posted 10 January 2009 - 04:38 PM

You will need to have your main page which will display all the stuff for the user, and then also have a php file that is used to query the database and output the database information in the format you want to be able to use.

w3schools has an example on how to use Ajax:
http://www.w3schools...jax/default.asp

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 jason-pc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 61
  • Joined: 10-January 09

Re: selecting option on menu A changes the options available in menu B

Posted 10 January 2009 - 10:35 PM

View PostBetaWar, on 10 Jan, 2009 - 03:38 PM, said:

You will need to have your main page which will display all the stuff for the user, and then also have a php file that is used to query the database and output the database information in the format you want to be able to use.

w3schools has an example on how to use Ajax:
http://www.w3schools...jax/default.asp

Hope that helps.


my script i currently have works, apart from one thing i can change the category field and that would mean the category would be updated in the databse but then the script would fail due to the topic title not being valid as this would not have been updated to contain the topics in that category.

i need the page to grab data from the database when the option is changed client side.

how do i get this to work? are there any examples of code that show data being got from mysql from the client side?

am i clear on what i am after? sorry to ask but sometimes i do not come across very clear when explaining things.
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,132
  • Joined: 07-September 06

Re: selecting option on menu A changes the options available in menu B

Posted 10 January 2009 - 11:27 PM

I understood what you were talking about. Here is an example of getting things from a database and outputting them via ajax to the client side page without a refresh:

http://www.w3schools...ax_database.asp

All you have to do is look at the bottom block of code, it is labeled "The AJAX Server Page" and is in ASP, but it is pretty much the same between ASP and PHP for what is being done (a query to the database followed by outputtingthe data as you want it to appear on the client side)

All you have to do then is append your second selection box (the one with the topic titles in it) so that the data returned populates it instead of what was originally in it.

For that you will need to give the second (titles) selection box an id like "titles" or something and use some code like so:

document.getElementById("titles").innerHTML = xmlHttp.responseText;


Make sure that your output from the php file looks like so:

<option value="<TOPIC_ID>"><TOPIC_TITLE></option>


for each one so it looks right, and have the selection box have an onchange event that calls to a Javascript /Ajax function to do whatever you need to have it do, like so:

<script>
function oc(){
  alert("It has changed!!!");
}
</script>
<select onchange="oc()">
  <option value="WHATEVER">Topic 1</option>
  <option value="WHATEVER">Topic 2</option>
  <option value="WHATEVER">Topic 3</option>
  <option value="WHATEVER">Topic 4</option>
</select>


Where the <option> tags have the value of each topic's unique ID (for easy access and editing) and their text is the topic title.

Hope that makes sense.
Was This Post Helpful? 0
  • +
  • -

#5 jason-pc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 61
  • Joined: 10-January 09

Re: selecting option on menu A changes the options available in menu B

Posted 11 January 2009 - 05:35 AM

SOLVED: i had incorrect quote marks for the database firelds. i used ' instead of `

thanks for your help tho


wow that works, thanks.

just one thing...

i am able to change the category option once and thats it, if i change it to another or back the original the topic options do not go back or change the second time round.

This post has been edited by jason-pc: 11 January 2009 - 06:34 AM

Was This Post Helpful? 0
  • +
  • -

#6 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,132
  • Joined: 07-September 06

Re: selecting option on menu A changes the options available in menu B

Posted 11 January 2009 - 10:34 AM

Hm, can I see the ajax function you are using to place the responseText inside the select box? That is where the problem is likely going to be.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1