JavaScript and MySQL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jens Körte

    JavaScript and MySQL

    Hi!

    I've got a typical newbie-problem, I guess.
    I got some selectboxes on my site. these boxes get their data from a
    databse. everything works fine, but when a user selects a value from
    one of the boxes i want to change the values from the other boxes
    dynamically. The values are derived from the database. Initially the
    boxes get for example those inputs:
    Box1 --> SELECT * FROM VEHICLE
    Box2 --> SELECT * FROM CAR

    If a user selects "car" from Box1, Box2 must change his content so
    that only cars are shown, i.e. no trucks, no bicylces, etc...
    Something like that:
    SELECT c.name FROM CAR c WHERE VEHICLE = 'car'

    With the onChange-event I can handle the values. No problem with that.
    But how do I get the data from the database into the
    javascript-statement. How do I have to handle this. I have no idea.
    Please help...


    Jens

    P.S. I only want to use PHP, HTML and Javascript.
  • Joakim Braun

    #2
    Re: JavaScript and MySQL

    "Jens Körte" <jk060674@gmx.d e> skrev i meddelandet
    news:ckprn0ppml gudmppbq1biugbo a4gbh9a1h@4ax.c om...[color=blue]
    > Hi!
    >
    > I've got a typical newbie-problem, I guess.
    > I got some selectboxes on my site. these boxes get their data from a
    > databse. everything works fine, but when a user selects a value from
    > one of the boxes i want to change the values from the other boxes
    > dynamically. The values are derived from the database. Initially the
    > boxes get for example those inputs:
    > Box1 --> SELECT * FROM VEHICLE
    > Box2 --> SELECT * FROM CAR
    >
    > If a user selects "car" from Box1, Box2 must change his content so
    > that only cars are shown, i.e. no trucks, no bicylces, etc...
    > Something like that:
    > SELECT c.name FROM CAR c WHERE VEHICLE = 'car'
    >
    > With the onChange-event I can handle the values. No problem with that.
    > But how do I get the data from the database into the
    > javascript-statement. How do I have to handle this. I have no idea.
    > Please help...
    >
    >
    > Jens[/color]

    Unless you want to do a reload whenever Box1 changes (and write the
    <options> directly server-side), you can write a dynamic Javascript with,
    for instance, PHP. Something like this (PHP pseudo-code, but you get the
    idea):

    function writeDynamicJav ascript(){

    $result = 'function box1changed(inN ewValue){' . "\n";
    $box1Values = /* get possible values from MySQL as PHP array */;

    $result .= 'var box2Values = new Array();' . "\n";
    $result .= 'var ctr = 0;' . "\n";
    $result .= 'switch(inNewVa lue){' . "\n"

    foreach($box1Va lues as $box1Value){
    $result .= 'case "' . $box1Value . '":' . "\n";
    /* fill the JS theResult variable of the dynamic JS function with
    values box2 can take */
    /* if box 1:s value is $box1Value - a loop something like:
    $result .= 'box2Values[ctr++] = "' . /* some value */ . '";' . "\n";
    $result .= 'break;' . "\n";

    }

    $result .= '}' . "\n"; // close JS switch

    /* Add JS code here to reload box2 from the array elements now in
    box2Values */

    $result .= '}' . "\n"; // close JS function
    return $result;
    }

    You echo the result of writeDynamicJav ascript() into the <head> of the
    document. Then you add onchange="box1c hanged(this.val ue);" (the name of the
    JS function that writeDynamicJav ascript() generates) to box 1.

    Note that if either of the <select> boxes can take a lot of values, this may
    result in an arbitrarily large Javascript in the <head>... But for a limited
    number of choices, it works well.

    Joakim Braun


    Comment

    • Joakim Braun

      #3
      Re: JavaScript and MySQL


      "Joakim Braun" <joakim.braun@j fbraun.removeth is.com> skrev i meddelandet
      news:8Pofd.8992 $1p.7706@nntpse rver.swip.net.. .[color=blue]
      > Unless you want to do a reload whenever Box1 changes[/color]

      <snip>

      What I meant was: Unless you want to post the form and regenerate the
      document whenever Box1 changes...

      Joakim Braun


      Comment

      Working...