Problem with "select" and javascript

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
Infinity
Posts: 45
Joined: Sun Oct 31, 2010 12:27 pm

Problem with "select" and javascript

Post by Infinity »

Hi, I want to use a select list to let the player select from a list of jobs (like on the example of halls) but I want to using javascript and innerphp show a different picture of the job depending of the choose, and I want it without refreshing, but... I have messed a lot arround and I can't get what I want...
I mean using php for the select make it different than some other examples that I seen, here you have the "select" code:

Code: Select all

 <?php
      print "<select id='classchoice' name='classchoice' length='20'>";
      $classinfo="SELECT * from classes";
      $classinfo2=mysql_query($classinfo) or die("Could not select classes");
      while ($classinfo3=mysql_fetch_array($classinfo2))
      {
        print "<option>$classinfo3[name]</option>";
      }
      print "</select><br>";
?>
Can anyone help me?

lot of thanks
User avatar
kaos78414
Posts: 507
Joined: Thu Jul 22, 2010 5:36 am

Re: Problem with "select" and javascript

Post by kaos78414 »

Firstly, is there a reason you're using print over echo?

Secondly, what you need is AJAX, and as has been said before, the most noob-friendly way to implement it is with jQuery. http://jquery.com/

The docs are VERY easy to follow, you'll be up and running in no time. You can do this without AJAX, but it will still require some JS. What you could do is have div's that are all have style="display:none;" and an id, save for the default choice. Assuming you're using jquery anyway, get the value of the select menu, and unhide the div that corresponds, while hiding the one that was showing.

Code: Select all

$("#select_id").click(function(){
  selected = $("#select_id").val();
  switch(selected)
  {
    // I'm not gunna write the whole thing for ya
    // But here you would write the code that shows
    // The div you wanna show, and adds a class or CSS
    // That hides the div that was already shown
  }
});
Note - That was just thrown together in a few seconds. There's probably better ways of doing the non-AJAX method. I would just go for AJAX for now, it'd be a good learning experience.
w00t
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Problem with "select" and javascript

Post by Jackolantern »

I agree with the vote of jQuery. jQuery is an amazing tool that you will eventually need to learn to make an engaging browser-based game, so why not learn it now?

And as for some additional info, the reason why you should use "echo" instead of "print" is because they have the same functionality, except for one thing: "print" returns a true/false value depending on whether or not the printing to the page succeeded or failed. This does have a slight performance impact, particularly once you start getting more traffic to your site. And since adding content to the webpage rarely ever fails, it leaves "print" fairly useless.
The indelible lord of tl;dr
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Problem with "select" and javascript

Post by Callan S. »

Print needs love too!

lol
User avatar
PaxBritannia
Posts: 680
Joined: Sun Apr 18, 2010 1:54 pm

Re: Problem with "select" and javascript

Post by PaxBritannia »

Print has slightly more overhead than echo (in theory). :) Print returns if it was successful or not while echo doesn't.

And to clear up any myths, both are language constructs and not functions.

pax.
User avatar
Infinity
Posts: 45
Joined: Sun Oct 31, 2010 12:27 pm

Re: Problem with "select" and javascript

Post by Infinity »

Lot of thanks for answering, i'll try it right now :)
Post Reply

Return to “Advanced Help and Support”