Dynamic fill form fields depending on dropdown box

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex

    Dynamic fill form fields depending on dropdown box

    Hello list

    This question has probably already been asked, but let me ask again

    I have a mysql database to which I connect with my php scripts. The
    database contains articles. Name, Unit_Price and Reference

    Now my client needs to order 5 items, click a button and a table with 5
    lines and 3 columns appears (until now easy)

    In the first column is a drop down box with a <select > and <options> in
    which I store all possible names.

    I would like than whenever the client changes one of the boxes (in my
    case there are 5) the Unite_Price column and Reference column in the
    line he just choose get automatically updated


    I search all over the web but I could not find it.

    Thank you for your help

    Alex
  • Erwin Moller

    #2
    Re: Dynamic fill form fields depending on dropdown box

    Alex wrote:
    [color=blue]
    > Hello list
    >
    > This question has probably already been asked, but let me ask again
    >
    > I have a mysql database to which I connect with my php scripts. The
    > database contains articles. Name, Unit_Price and Reference
    >
    > Now my client needs to order 5 items, click a button and a table with 5
    > lines and 3 columns appears (until now easy)
    >
    > In the first column is a drop down box with a <select > and <options> in
    > which I store all possible names.
    >
    > I would like than whenever the client changes one of the boxes (in my
    > case there are 5) the Unite_Price column and Reference column in the
    > line he just choose get automatically updated
    >[/color]

    Are you talking about Javascript here?
    Do you want the user to do a select on your page and the page will fill the
    appropriate fields? Then go Javascript.

    Or do you want to go back to the server if somebody touches the selectbox?
    Then it is PHP.


    I think you better do this with Javascript.
    Try comp.lang.javas cript.

    If you want to do it in PHP, well... where to start?
    Do you know how to receive a form in PHP? ($_POST[] and the like)
    Do you know how to put dynamical data into your html-page with PHP?

    If you want to do this in PHP, please be more specific and ask a question
    that is answerable instead of the complete solution to a problem nobody in
    here knows except you. :-)

    [color=blue]
    >
    > I search all over the web but I could not find it.[/color]

    Really?
    This is quite basic stuff (both in Javascript and PHP) so I wonder why you
    didn't find any code that does similar things.
    (No punch intended)

    Good luck.

    Regards,
    Erwin Moller
    [color=blue]
    >
    > Thank you for your help
    >
    > Alex[/color]

    Comment

    • Alex

      #3
      Re: Dynamic fill form fields depending on dropdown box

      Erwin Moller wrote:[color=blue]
      > Alex wrote:
      >[color=green]
      >> Hello list
      >>
      >> This question has probably already been asked, but let me ask again
      >>
      >> I have a mysql database to which I connect with my php scripts. The
      >> database contains articles. Name, Unit_Price and Reference
      >>
      >> Now my client needs to order 5 items, click a button and a table with 5
      >> lines and 3 columns appears (until now easy)
      >>
      >> In the first column is a drop down box with a <select > and <options> in
      >> which I store all possible names.
      >>
      >> I would like than whenever the client changes one of the boxes (in my
      >> case there are 5) the Unite_Price column and Reference column in the
      >> line he just choose get automatically updated
      >>[/color]
      >
      > Are you talking about Javascript here?
      > Do you want the user to do a select on your page and the page will fill the
      > appropriate fields? Then go Javascript.
      >
      > Or do you want to go back to the server if somebody touches the selectbox?
      > Then it is PHP.
      >
      >
      > I think you better do this with Javascript.
      > Try comp.lang.javas cript.
      >
      > If you want to do it in PHP, well... where to start?
      > Do you know how to receive a form in PHP? ($_POST[] and the like)
      > Do you know how to put dynamical data into your html-page with PHP?
      >
      > If you want to do this in PHP, please be more specific and ask a question
      > that is answerable instead of the complete solution to a problem nobody in
      > here knows except you. :-)
      >
      >[color=green]
      >> I search all over the web but I could not find it.[/color]
      >
      > Really?
      > This is quite basic stuff (both in Javascript and PHP) so I wonder why you
      > didn't find any code that does similar things.
      > (No punch intended)
      >
      > Good luck.
      >
      > Regards,
      > Erwin Moller
      >[color=green]
      >> Thank you for your help
      >>
      >> Alex[/color]
      >[/color]
      Well you are almost right, this is not only php it is also a bit of
      Javascript, I need in fact to be able to pass the
      $fields=mysql_f etch_object($da taresults} to a javascript, but I do not
      see how I can implement that since I do not know how to pass the result
      using php, so in that sens it is also php relevant

      as for the punch, I searched the net but I only get either trivial
      results or things that populate a drop down menu (which I already know
      how to do) That's why I ask it here

      Thanks

      Comment

      • Erwin Moller

        #4
        Re: Dynamic fill form fields depending on dropdown box

        Krustov wrote:
        [color=blue]
        > <comp.lang.ph p , Erwin Moller ,
        > since_humans_re ad_this_I_am_sp am...yourself.co m>
        > <43fc876b$0$110 65$e4fe514c@new s.xs4all.nl>
        > <Wed, 22 Feb 2006 16:46:51 +0100>
        >[color=green][color=darkred]
        >> > I search all over the web but I could not find it.[/color]
        >>
        >> Really?
        >> This is quite basic stuff (both in Javascript and PHP) so I wonder why
        >> you didn't find any code that does similar things.
        >> (No punch intended)
        >>[/color]
        >
        > Do you ever actually help anybody on here who asks a question .
        >
        > You only ever seem to comment and give your opinion on 'what' they ask
        > rather than helping them .[/color]

        Bull. Look around more.
        I do actually help people, even this OP, as you could see in the part you
        cutted away. Please go insult somebody else, thanks.

        Erwin Moller




        Comment

        • noone

          #5
          Re: Dynamic fill form fields depending on dropdown box

          Alex wrote:[color=blue]
          > Erwin Moller wrote:
          >[color=green]
          >>Alex wrote:
          >>
          >>[color=darkred]
          >>>Hello list
          >>>
          >>>This question has probably already been asked, but let me ask again
          >>>
          >>>I have a mysql database to which I connect with my php scripts. The
          >>>database contains articles. Name, Unit_Price and Reference
          >>>
          >>>Now my client needs to order 5 items, click a button and a table with 5
          >>>lines and 3 columns appears (until now easy)
          >>>
          >>>In the first column is a drop down box with a <select > and <options> in
          >>>which I store all possible names.
          >>>
          >>>I would like than whenever the client changes one of the boxes (in my
          >>>case there are 5) the Unite_Price column and Reference column in the
          >>>line he just choose get automatically updated
          >>>[/color]
          >>
          >>Are you talking about Javascript here?
          >>Do you want the user to do a select on your page and the page will fill the
          >>appropriate fields? Then go Javascript.
          >>
          >>Or do you want to go back to the server if somebody touches the selectbox?
          >>Then it is PHP.
          >>
          >>
          >>I think you better do this with Javascript.
          >>Try comp.lang.javas cript.
          >>
          >>If you want to do it in PHP, well... where to start?
          >>Do you know how to receive a form in PHP? ($_POST[] and the like)
          >>Do you know how to put dynamical data into your html-page with PHP?
          >>
          >>If you want to do this in PHP, please be more specific and ask a question
          >>that is answerable instead of the complete solution to a problem nobody in
          >>here knows except you. :-)
          >>
          >>
          >>[color=darkred]
          >>>I search all over the web but I could not find it.[/color]
          >>
          >>Really?
          >>This is quite basic stuff (both in Javascript and PHP) so I wonder why you
          >>didn't find any code that does similar things.
          >>(No punch intended)
          >>
          >>Good luck.
          >>
          >>Regards,
          >>Erwin Moller
          >>
          >>[color=darkred]
          >>>Thank you for your help
          >>>
          >>>Alex[/color]
          >>[/color]
          > Well you are almost right, this is not only php it is also a bit of
          > Javascript, I need in fact to be able to pass the
          > $fields=mysql_f etch_object($da taresults} to a javascript, but I do not
          > see how I can implement that since I do not know how to pass the result
          > using php, so in that sens it is also php relevant
          >[/color]

          creating a javascript portion of a page and populating it with the data
          from $fields is no different than creating the select/options from a
          resultset.

          Spend some time to map out your data flow. where does it come from
          (server-side database, client-side selection/entry), where does it go
          browser/client,server into database or other processing.

          [color=blue]
          > as for the punch, I searched the net but I only get either trivial
          > results or things that populate a drop down menu (which I already know
          > how to do) That's why I ask it here
          >[/color]

          Understanding the data flow above allows you to be a good programmer not
          just a copy/paster (while that is not bad, you should not rely on it).
          One should also understand the technologies they are using and why they
          are using them - there may be other solutions to your problem you/we
          have not thought of...
          [color=blue]
          > Thanks[/color]

          Comment

          • noone

            #6
            Re: Dynamic fill form fields depending on dropdown box

            Krustov wrote:[color=blue]
            > <comp.lang.ph p , Erwin Moller ,
            > since_humans_re ad_this_I_am_sp am...yourself.co m>
            > <43fc876b$0$110 65$e4fe514c@new s.xs4all.nl>
            > <Wed, 22 Feb 2006 16:46:51 +0100>
            >[color=green][color=darkred]
            >>>I search all over the web but I could not find it.[/color]
            >>
            >>Really?
            >>This is quite basic stuff (both in Javascript and PHP) so I wonder why you
            >>didn't find any code that does similar things.
            >>(No punch intended)
            >>[/color]
            >
            >
            > Do you ever actually help anybody on here who asks a question .
            >
            > You only ever seem to comment and give your opinion on 'what' they ask
            > rather than helping them .[/color]

            Krustov,

            did you ever think that by forcing someone back to the docs or other
            research DOES help them to better understand what it is they are trying
            to accomplish and maybe - just maybe - learn a lot more than they would
            if we just gave some code snippet? (BTW I have seen WAY TOO MANY
            questions in some forums that are trying to solve almost an identical
            problem - and some in those forums refuse to answer because it is common
            knowledge they are trying to solve an assignment from their CIS/IT
            professor in school. Solving the problem with a code snippet gains
            nothing - understanding the technology gains much knowledge enabling
            them to solve more and more complex isssues. Most programmers/IT
            professionals are neither programmers or professional... )

            I could go on a rant that describes how some generations feel they
            "deserve" to be spoon-fed their entire life - but I do not believe this
            situation warrants that much effort.

            Comment

            • Erwin Moller

              #7
              Re: Dynamic fill form fields depending on dropdown box

              noone wrote:
              [color=blue]
              > Krustov wrote:[color=green]
              >> <comp.lang.ph p , Erwin Moller ,
              >> since_humans_re ad_this_I_am_sp am...yourself.co m>
              >> <43fc876b$0$110 65$e4fe514c@new s.xs4all.nl>
              >> <Wed, 22 Feb 2006 16:46:51 +0100>
              >>[color=darkred]
              >>>>I search all over the web but I could not find it.
              >>>
              >>>Really?
              >>>This is quite basic stuff (both in Javascript and PHP) so I wonder why
              >>>you didn't find any code that does similar things.
              >>>(No punch intended)
              >>>[/color]
              >>
              >>
              >> Do you ever actually help anybody on here who asks a question .
              >>
              >> You only ever seem to comment and give your opinion on 'what' they ask
              >> rather than helping them .[/color]
              >
              > Krustov,
              >
              > did you ever think that by forcing someone back to the docs or other
              > research DOES help them to better understand what it is they are trying
              > to accomplish and maybe - just maybe - learn a lot more than they would
              > if we just gave some code snippet? (BTW I have seen WAY TOO MANY
              > questions in some forums that are trying to solve almost an identical
              > problem - and some in those forums refuse to answer because it is common
              > knowledge they are trying to solve an assignment from their CIS/IT
              > professor in school. Solving the problem with a code snippet gains
              > nothing - understanding the technology gains much knowledge enabling
              > them to solve more and more complex isssues. Most programmers/IT
              > professionals are neither programmers or professional... )
              >
              > I could go on a rant that describes how some generations feel they
              > "deserve" to be spoon-fed their entire life - but I do not believe this
              > situation warrants that much effort.[/color]

              thx mate. :-)

              Erwin

              Comment

              • Erwin Moller

                #8
                Re: Dynamic fill form fields depending on dropdown box

                Alex wrote:
                [color=blue]
                > Erwin Moller wrote:[color=green]
                >> Alex wrote:
                >>[color=darkred]
                >>> Hello list
                >>>
                >>> This question has probably already been asked, but let me ask again
                >>>
                >>> I have a mysql database to which I connect with my php scripts. The
                >>> database contains articles. Name, Unit_Price and Reference
                >>>
                >>> Now my client needs to order 5 items, click a button and a table with 5
                >>> lines and 3 columns appears (until now easy)
                >>>
                >>> In the first column is a drop down box with a <select > and <options> in
                >>> which I store all possible names.
                >>>
                >>> I would like than whenever the client changes one of the boxes (in my
                >>> case there are 5) the Unite_Price column and Reference column in the
                >>> line he just choose get automatically updated
                >>>[/color]
                >>
                >> Are you talking about Javascript here?
                >> Do you want the user to do a select on your page and the page will fill
                >> the appropriate fields? Then go Javascript.
                >>
                >> Or do you want to go back to the server if somebody touches the
                >> selectbox? Then it is PHP.
                >>
                >>
                >> I think you better do this with Javascript.
                >> Try comp.lang.javas cript.
                >>
                >> If you want to do it in PHP, well... where to start?
                >> Do you know how to receive a form in PHP? ($_POST[] and the like)
                >> Do you know how to put dynamical data into your html-page with PHP?
                >>
                >> If you want to do this in PHP, please be more specific and ask a question
                >> that is answerable instead of the complete solution to a problem nobody
                >> in here knows except you. :-)
                >>
                >>[color=darkred]
                >>> I search all over the web but I could not find it.[/color]
                >>
                >> Really?
                >> This is quite basic stuff (both in Javascript and PHP) so I wonder why
                >> you didn't find any code that does similar things.
                >> (No punch intended)
                >>
                >> Good luck.
                >>
                >> Regards,
                >> Erwin Moller
                >>[color=darkred]
                >>> Thank you for your help
                >>>
                >>> Alex[/color]
                >>[/color]
                > Well you are almost right, this is not only php it is also a bit of
                > Javascript, I need in fact to be able to pass the
                > $fields=mysql_f etch_object($da taresults} to a javascript, but I do not
                > see how I can implement that since I do not know how to pass the result
                > using php, so in that sens it is also php relevant
                >
                > as for the punch, I searched the net but I only get either trivial
                > results or things that populate a drop down menu (which I already know
                > how to do) That's why I ask it here
                >
                > Thanks[/color]

                Hi alex, just hang on.
                I made something similar (databasemappin g to JS) just one week back, and am
                fitting it into an example that I will post soon.

                Erwin

                Comment

                • Erwin Moller

                  #9
                  Re: Dynamic fill form fields depending on dropdown box

                  Alex wrote:
                  [color=blue]
                  > Erwin Moller wrote:[color=green]
                  >> Alex wrote:
                  >>[color=darkred]
                  >>> Hello list
                  >>>
                  >>> This question has probably already been asked, but let me ask again
                  >>>
                  >>> I have a mysql database to which I connect with my php scripts. The
                  >>> database contains articles. Name, Unit_Price and Reference
                  >>>
                  >>> Now my client needs to order 5 items, click a button and a table with 5
                  >>> lines and 3 columns appears (until now easy)
                  >>>
                  >>> In the first column is a drop down box with a <select > and <options> in
                  >>> which I store all possible names.
                  >>>
                  >>> I would like than whenever the client changes one of the boxes (in my
                  >>> case there are 5) the Unite_Price column and Reference column in the
                  >>> line he just choose get automatically updated
                  >>>[/color]
                  >>
                  >> Are you talking about Javascript here?
                  >> Do you want the user to do a select on your page and the page will fill
                  >> the appropriate fields? Then go Javascript.
                  >>
                  >> Or do you want to go back to the server if somebody touches the
                  >> selectbox? Then it is PHP.
                  >>
                  >>
                  >> I think you better do this with Javascript.
                  >> Try comp.lang.javas cript.
                  >>
                  >> If you want to do it in PHP, well... where to start?
                  >> Do you know how to receive a form in PHP? ($_POST[] and the like)
                  >> Do you know how to put dynamical data into your html-page with PHP?
                  >>
                  >> If you want to do this in PHP, please be more specific and ask a question
                  >> that is answerable instead of the complete solution to a problem nobody
                  >> in here knows except you. :-)
                  >>
                  >>[color=darkred]
                  >>> I search all over the web but I could not find it.[/color]
                  >>
                  >> Really?
                  >> This is quite basic stuff (both in Javascript and PHP) so I wonder why
                  >> you didn't find any code that does similar things.
                  >> (No punch intended)
                  >>
                  >> Good luck.
                  >>
                  >> Regards,
                  >> Erwin Moller
                  >>[color=darkred]
                  >>> Thank you for your help
                  >>>
                  >>> Alex[/color]
                  >>[/color]
                  > Well you are almost right, this is not only php it is also a bit of
                  > Javascript, I need in fact to be able to pass the
                  > $fields=mysql_f etch_object($da taresults} to a javascript, but I do not
                  > see how I can implement that since I do not know how to pass the result
                  > using php, so in that sens it is also php relevant[/color]

                  Ok, clear.
                  So you need data from the database ready-at-hand in a webpage, usable for
                  javascript? Right?

                  Here follows a simple example, maybe enough to get you going in your own
                  situation.

                  Suppose you have 1 selectbox, and depending on the value the visitor selects
                  you need to populate a second selectbox.
                  eg:
                  [mainselectbox] category
                  [secondary selectbox] item

                  You can now simply fill the first selectbox with appropriate options. (You
                  said you already know how, so I skip that part)

                  Just add an eventhandler to the select, you'll end up with something like
                  this (just the html)
                  <select name="mainselec tbox" onChange="popul ateSecondairy() ;">

                  Here follows a comple example.
                  I faked the queryresults to keep things shorter, but if you understand the
                  used Javascript, you are able to modify it for your own uses.

                  ------------------------------
                  <?

                  // fakeresult from databasequery that returned category and items
                  $rs = array(
                  "fruit" => array("apple"," banana","pineap ple"),
                  "books" => array("HitchHik ersGuideToTheGa laxy","Programm ing
                  Perl","Postgres ql"),
                  "animals" => array("dog","ca t")
                  );

                  ?>
                  <html>
                  <head>
                  <title>test</title>
                  </head>
                  <body>

                  <form action="bla.php " Method="POST" name="testform" >
                  category: <SELECT name="category" onChange="popul ateSecondSelect ();">
                  <?
                  foreach($rs as $cat => $items){
                  ?>
                  <OPTION value="<?= $cat ?>"><?= $cat ?>
                  <?
                  }
                  ?>
                  </SELECT>
                  <hr>
                  Item: <SELECT name="item">
                  <OPTION value="-1">pick category first
                  </SELECT>
                  </form>

                  <script type="text/javascript">
                  // drop all possible item-values in Object
                  var category = new Object();

                  <?
                  // Create all js-object with their items
                  foreach($rs as $cat => $items){
                  // put all $items in JS-readable format.
                  $JSitems = "";
                  $itemsWithQuote s = array();
                  foreach ($items as $oneItem){
                  $itemsWithQuote s[] = "\"$oneItem \"";
                  }

                  ?>
                  category['<?= $cat ?>'] = new Array(<?= implode(",",$it emsWithQuotes) ?>);
                  <?
                  }
                  ?>

                  function populateSecondS elect(){
                  // get selected category
                  var selCat = document.forms["testform"].category.selec tedIndex;
                  var catName = document.forms["testform"].category[selCat].value;

                  // now repopulate the second selectbox
                  var refSecond = document.forms["testform"].item;

                  // remove old options
                  refSecond.lengt h = 0;

                  // add new options
                  var theArr = category[catName];
                  for (var i=0; i< theArr.length ; i++){
                  refSecond.optio ns[refSecond.optio ns.length] = new
                  Option(category[catName][i],"hai");
                  }

                  }

                  </script>

                  </body>
                  </html>
                  --------------------------------------


                  Hope that helps.

                  Good luck,
                  Erwin Moller

                  Comment

                  • Erwin Moller

                    #10
                    Re: Dynamic fill form fields depending on dropdown box

                    Krustov wrote:
                    [color=blue]
                    > <comp.lang.ph p , Erwin Moller ,
                    > since_humans_re ad_this_I_am_sp am...yourself.co m>
                    > <43fc92ed$0$243 73$e4fe514c@new s.xs4all.nl>
                    > <Wed, 22 Feb 2006 17:35:57 +0100>
                    >[color=green][color=darkred]
                    >> > Do you ever actually help anybody on here who asks a question .
                    >> >
                    >> > You only ever seem to comment and give your opinion on 'what' they ask
                    >> > rather than helping them .[/color]
                    >>
                    >> Bull. Look around more.
                    >> I do actually help people, even this OP, as you could see in the part you
                    >> cutted away. Please go insult somebody else, thanks.
                    >>[/color]
                    >
                    > Yes i'd say you do help some people .
                    >
                    > But would you say that grants you the right to spout drivel at others .[/color]

                    drivel?
                    Well thanks.

                    If you don't care for my postings, I suggest you put me on your blocklist. I
                    couldn't care less.
                    Most posters I answer appreciate my effort. If you don't: fine with me.

                    If, however, you don't like my tone, and want to educate me on that: fine
                    too, really, point taken.

                    I am here to help people and get help, not to socialize.

                    [color=blue]
                    >
                    > No big thing and not the end of the world - but perhaps your forgetting
                    > its not just the person you are replying too that sees it .[/color]

                    So what?
                    Do you think I am not aware of that?

                    Seriously: If you think that I for some invalid reason was rude: Sorry for
                    that. But I don't think I was out of line somewhere.

                    I am trying to help people in here, and when I see bad questions (which I
                    see a lot lately) I get irritated.
                    It seems less and less people know how to post in usenet these days.

                    Regards,
                    Erwin Moller

                    Comment

                    • Alex

                      #11
                      Re: Dynamic fill form fields depending on dropdown box

                      Erwin Moller wrote:[color=blue]
                      > Alex wrote:
                      >[color=green]
                      >> Erwin Moller wrote:[color=darkred]
                      >>> Alex wrote:
                      >>>
                      >>>> Hello list
                      >>>>
                      >>>> This question has probably already been asked, but let me ask again
                      >>>>
                      >>>> I have a mysql database to which I connect with my php scripts. The
                      >>>> database contains articles. Name, Unit_Price and Reference
                      >>>>
                      >>>> Now my client needs to order 5 items, click a button and a table with 5
                      >>>> lines and 3 columns appears (until now easy)
                      >>>>
                      >>>> In the first column is a drop down box with a <select > and <options> in
                      >>>> which I store all possible names.
                      >>>>
                      >>>> I would like than whenever the client changes one of the boxes (in my
                      >>>> case there are 5) the Unite_Price column and Reference column in the
                      >>>> line he just choose get automatically updated
                      >>>>
                      >>> Are you talking about Javascript here?
                      >>> Do you want the user to do a select on your page and the page will fill
                      >>> the appropriate fields? Then go Javascript.
                      >>>
                      >>> Or do you want to go back to the server if somebody touches the
                      >>> selectbox? Then it is PHP.
                      >>>
                      >>>
                      >>> I think you better do this with Javascript.
                      >>> Try comp.lang.javas cript.
                      >>>
                      >>> If you want to do it in PHP, well... where to start?
                      >>> Do you know how to receive a form in PHP? ($_POST[] and the like)
                      >>> Do you know how to put dynamical data into your html-page with PHP?
                      >>>
                      >>> If you want to do this in PHP, please be more specific and ask a question
                      >>> that is answerable instead of the complete solution to a problem nobody
                      >>> in here knows except you. :-)
                      >>>
                      >>>
                      >>>> I search all over the web but I could not find it.
                      >>> Really?
                      >>> This is quite basic stuff (both in Javascript and PHP) so I wonder why
                      >>> you didn't find any code that does similar things.
                      >>> (No punch intended)
                      >>>
                      >>> Good luck.
                      >>>
                      >>> Regards,
                      >>> Erwin Moller
                      >>>
                      >>>> Thank you for your help
                      >>>>
                      >>>> Alex[/color]
                      >> Well you are almost right, this is not only php it is also a bit of
                      >> Javascript, I need in fact to be able to pass the
                      >> $fields=mysql_f etch_object($da taresults} to a javascript, but I do not
                      >> see how I can implement that since I do not know how to pass the result
                      >> using php, so in that sens it is also php relevant[/color]
                      >
                      > Ok, clear.
                      > So you need data from the database ready-at-hand in a webpage, usable for
                      > javascript? Right?
                      >
                      > Here follows a simple example, maybe enough to get you going in your own
                      > situation.
                      >
                      > Suppose you have 1 selectbox, and depending on the value the visitor selects
                      > you need to populate a second selectbox.
                      > eg:
                      > [mainselectbox] category
                      > [secondary selectbox] item
                      >
                      > You can now simply fill the first selectbox with appropriate options. (You
                      > said you already know how, so I skip that part)
                      >
                      > Just add an eventhandler to the select, you'll end up with something like
                      > this (just the html)
                      > <select name="mainselec tbox" onChange="popul ateSecondairy() ;">
                      >
                      > Here follows a comple example.
                      > I faked the queryresults to keep things shorter, but if you understand the
                      > used Javascript, you are able to modify it for your own uses.
                      >
                      > ------------------------------
                      > <?
                      >
                      > // fakeresult from databasequery that returned category and items
                      > $rs = array(
                      > "fruit" => array("apple"," banana","pineap ple"),
                      > "books" => array("HitchHik ersGuideToTheGa laxy","Programm ing
                      > Perl","Postgres ql"),
                      > "animals" => array("dog","ca t")
                      > );
                      >
                      > ?>
                      > <html>
                      > <head>
                      > <title>test</title>
                      > </head>
                      > <body>
                      >
                      > <form action="bla.php " Method="POST" name="testform" >
                      > category: <SELECT name="category" onChange="popul ateSecondSelect ();">
                      > <?
                      > foreach($rs as $cat => $items){
                      > ?>
                      > <OPTION value="<?= $cat ?>"><?= $cat ?>
                      > <?
                      > }
                      > ?>
                      > </SELECT>
                      > <hr>
                      > Item: <SELECT name="item">
                      > <OPTION value="-1">pick category first
                      > </SELECT>
                      > </form>
                      >
                      > <script type="text/javascript">
                      > // drop all possible item-values in Object
                      > var category = new Object();
                      >
                      > <?
                      > // Create all js-object with their items
                      > foreach($rs as $cat => $items){
                      > // put all $items in JS-readable format.
                      > $JSitems = "";
                      > $itemsWithQuote s = array();
                      > foreach ($items as $oneItem){
                      > $itemsWithQuote s[] = "\"$oneItem \"";
                      > }
                      >
                      > ?>
                      > category['<?= $cat ?>'] = new Array(<?= implode(",",$it emsWithQuotes) ?>);
                      > <?
                      > }
                      > ?>
                      >
                      > function populateSecondS elect(){
                      > // get selected category
                      > var selCat = document.forms["testform"].category.selec tedIndex;
                      > var catName = document.forms["testform"].category[selCat].value;
                      >
                      > // now repopulate the second selectbox
                      > var refSecond = document.forms["testform"].item;
                      >
                      > // remove old options
                      > refSecond.lengt h = 0;
                      >
                      > // add new options
                      > var theArr = category[catName];
                      > for (var i=0; i< theArr.length ; i++){
                      > refSecond.optio ns[refSecond.optio ns.length] = new
                      > Option(category[catName][i],"hai");
                      > }
                      >
                      > }
                      >
                      > </script>
                      >
                      > </body>
                      > </html>
                      > --------------------------------------
                      >
                      >
                      > Hope that helps.
                      >
                      > Good luck,
                      > Erwin Moller[/color]

                      Thanks Erwin for the code example, I will try it out and figure how to
                      make it work.

                      Ohh, and just to add some oil in the fire. Some people do ask questions
                      not because they want to solve something that there teacher gave them,
                      but because he knows that other people are specialists in the field and
                      hence he will find more easily his answer.

                      You can ask me anything on quantum communications if you like :) That is
                      one of the reasons why usenet even exists. To be a big community helping
                      each other.

                      Any way thank you a lot Erwin

                      Comment

                      • Stephen Poley

                        #12
                        Re: Dynamic fill form fields depending on dropdown box

                        On Wed, 22 Feb 2006 16:10:00 +0100, Alex
                        <alexios.bevera tos@physics.uni ge.ch> wrote:
                        [color=blue]
                        >I have a mysql database to which I connect with my php scripts. The
                        >database contains articles. Name, Unit_Price and Reference
                        >
                        >Now my client needs to order 5 items, click a button and a table with 5
                        >lines and 3 columns appears (until now easy)
                        >
                        >In the first column is a drop down box with a <select > and <options> in
                        >which I store all possible names.
                        >
                        >I would like than whenever the client changes one of the boxes (in my
                        >case there are 5) the Unite_Price column and Reference column in the
                        >line he just choose get automatically updated[/color]

                        I have to agree with Erwin that it's a little hard determining what
                        exactly the requirement is. You make it sound as if the client first has
                        to decide how many items to order, then what items to order, and only
                        then discovers what the price is. That seems very odd.

                        More usual would be to present a table of items, containing name, price
                        and reference for each item, and a checkbox against each item to tick,
                        or an input box to enter how many of each item is needed. That you can
                        do in HTML with no Javascript at all. But maybe you have something else
                        in mind?

                        --
                        Stephen Poley


                        Comment

                        • Erwin Moller

                          #13
                          Re: Dynamic fill form fields depending on dropdown box

                          Alex wrote:
                          [color=blue]
                          > Erwin Moller wrote:[color=green]
                          >> Alex wrote:
                          >>[color=darkred]
                          >>> Erwin Moller wrote:
                          >>>> Alex wrote:
                          >>>>
                          >>>>> Hello list
                          >>>>>
                          >>>>> This question has probably already been asked, but let me ask again
                          >>>>>
                          >>>>> I have a mysql database to which I connect with my php scripts. The
                          >>>>> database contains articles. Name, Unit_Price and Reference
                          >>>>>
                          >>>>> Now my client needs to order 5 items, click a button and a table with
                          >>>>> 5 lines and 3 columns appears (until now easy)
                          >>>>>
                          >>>>> In the first column is a drop down box with a <select > and <options>
                          >>>>> in which I store all possible names.
                          >>>>>
                          >>>>> I would like than whenever the client changes one of the boxes (in my
                          >>>>> case there are 5) the Unite_Price column and Reference column in the
                          >>>>> line he just choose get automatically updated
                          >>>>>
                          >>>> Are you talking about Javascript here?
                          >>>> Do you want the user to do a select on your page and the page will fill
                          >>>> the appropriate fields? Then go Javascript.
                          >>>>
                          >>>> Or do you want to go back to the server if somebody touches the
                          >>>> selectbox? Then it is PHP.
                          >>>>
                          >>>>
                          >>>> I think you better do this with Javascript.
                          >>>> Try comp.lang.javas cript.
                          >>>>
                          >>>> If you want to do it in PHP, well... where to start?
                          >>>> Do you know how to receive a form in PHP? ($_POST[] and the like)
                          >>>> Do you know how to put dynamical data into your html-page with PHP?
                          >>>>
                          >>>> If you want to do this in PHP, please be more specific and ask a
                          >>>> question that is answerable instead of the complete solution to a
                          >>>> problem nobody in here knows except you. :-)
                          >>>>
                          >>>>
                          >>>>> I search all over the web but I could not find it.
                          >>>> Really?
                          >>>> This is quite basic stuff (both in Javascript and PHP) so I wonder why
                          >>>> you didn't find any code that does similar things.
                          >>>> (No punch intended)
                          >>>>
                          >>>> Good luck.
                          >>>>
                          >>>> Regards,
                          >>>> Erwin Moller
                          >>>>
                          >>>>> Thank you for your help
                          >>>>>
                          >>>>> Alex
                          >>> Well you are almost right, this is not only php it is also a bit of
                          >>> Javascript, I need in fact to be able to pass the
                          >>> $fields=mysql_f etch_object($da taresults} to a javascript, but I do not
                          >>> see how I can implement that since I do not know how to pass the result
                          >>> using php, so in that sens it is also php relevant[/color]
                          >>
                          >> Ok, clear.
                          >> So you need data from the database ready-at-hand in a webpage, usable for
                          >> javascript? Right?
                          >>
                          >> Here follows a simple example, maybe enough to get you going in your own
                          >> situation.
                          >>
                          >> Suppose you have 1 selectbox, and depending on the value the visitor
                          >> selects you need to populate a second selectbox.
                          >> eg:
                          >> [mainselectbox] category
                          >> [secondary selectbox] item
                          >>
                          >> You can now simply fill the first selectbox with appropriate options.
                          >> (You said you already know how, so I skip that part)
                          >>
                          >> Just add an eventhandler to the select, you'll end up with something like
                          >> this (just the html)
                          >> <select name="mainselec tbox" onChange="popul ateSecondairy() ;">
                          >>
                          >> Here follows a comple example.
                          >> I faked the queryresults to keep things shorter, but if you understand
                          >> the used Javascript, you are able to modify it for your own uses.
                          >>
                          >> ------------------------------
                          >> <?
                          >>
                          >> // fakeresult from databasequery that returned category and items
                          >> $rs = array(
                          >> "fruit" => array("apple"," banana","pineap ple"),
                          >> "books" => array("HitchHik ersGuideToTheGa laxy","Programm ing
                          >> Perl","Postgres ql"),
                          >> "animals" => array("dog","ca t")
                          >> );
                          >>
                          >> ?>
                          >> <html>
                          >> <head>
                          >> <title>test</title>
                          >> </head>
                          >> <body>
                          >>
                          >> <form action="bla.php " Method="POST" name="testform" >
                          >> category: <SELECT name="category" onChange="popul ateSecondSelect ();">
                          >> <?
                          >> foreach($rs as $cat => $items){
                          >> ?>
                          >> <OPTION value="<?= $cat ?>"><?= $cat ?>
                          >> <?
                          >> }
                          >> ?>
                          >> </SELECT>
                          >> <hr>
                          >> Item: <SELECT name="item">
                          >> <OPTION value="-1">pick category first
                          >> </SELECT>
                          >> </form>
                          >>
                          >> <script type="text/javascript">
                          >> // drop all possible item-values in Object
                          >> var category = new Object();
                          >>
                          >> <?
                          >> // Create all js-object with their items
                          >> foreach($rs as $cat => $items){
                          >> // put all $items in JS-readable format.
                          >> $JSitems = "";
                          >> $itemsWithQuote s = array();
                          >> foreach ($items as $oneItem){
                          >> $itemsWithQuote s[] = "\"$oneItem \"";
                          >> }
                          >>
                          >> ?>
                          >> category['<?= $cat ?>'] = new Array(<?=
                          >> implode(",",$it emsWithQuotes) ?>);
                          >> <?
                          >> }
                          >> ?>
                          >>
                          >> function populateSecondS elect(){
                          >> // get selected category
                          >> var selCat = document.forms["testform"].category.selec tedIndex;
                          >> var catName = document.forms["testform"].category[selCat].value;
                          >>
                          >> // now repopulate the second selectbox
                          >> var refSecond = document.forms["testform"].item;
                          >>
                          >> // remove old options
                          >> refSecond.lengt h = 0;
                          >>
                          >> // add new options
                          >> var theArr = category[catName];
                          >> for (var i=0; i< theArr.length ; i++){
                          >> refSecond.optio ns[refSecond.optio ns.length] = new
                          >> Option(category[catName][i],"hai");
                          >> }
                          >>
                          >> }
                          >>
                          >> </script>
                          >>
                          >> </body>
                          >> </html>
                          >> --------------------------------------
                          >>
                          >>
                          >> Hope that helps.
                          >>
                          >> Good luck,
                          >> Erwin Moller[/color]
                          >
                          > Thanks Erwin for the code example, I will try it out and figure how to
                          > make it work.[/color]

                          You are welcome.
                          I hope you can figure it out.

                          Just copy it to a file, name it test.php, and run.
                          Then you can judge if that is what you wanted in the first place. :P
                          The trick is the Javascript, not the PHP.

                          If you need help or something, just come back here or mail me at
                          erwizznmollerzz @xs4all.nl (remove all 4 z)

                          [color=blue]
                          >
                          > Ohh, and just to add some oil in the fire. Some people do ask questions
                          > not because they want to solve something that there teacher gave them,
                          > but because he knows that other people are specialists in the field and
                          > hence he will find more easily his answer.[/color]

                          Yeah, I know.
                          It wasn't me who claimed you were asking your teacher's questions by the
                          way.
                          I developed an extra sense for that: People who are asking others to do
                          their homework.

                          I was in a terrible mood earlier today and easily irritated.
                          Sorry if I snarled at you.
                          [color=blue]
                          >
                          > You can ask me anything on quantum communications if you like :) That is
                          > one of the reasons why usenet even exists. To be a big community helping
                          > each other.[/color]

                          Really!??
                          WOW: You will be sorry for that. :P
                          I think that rules. :-) Quantum communications. I didn't even know one could
                          study that.

                          I like to study a little cosmology as a hobby and that means nowadays you
                          have to study quantumphysics too as you undoubtely know. But my primary
                          source of knowlegde comes from Special issues of Scientific American, so
                          you can imagine I still have a few questions. ;-)

                          Well, I always wondered: If some particle (A) falls appart in two other
                          particles (A->B+C), and we have a detector a few million miles away from
                          the place where that happened, and that detector measures some property,
                          like spin, of the particle (B): I understood the accompannying particle (C)
                          will instantly get the appropriate property too. (I hope you know what I am
                          referring to: I think it is called entanglement or something like that.)
                          Question: Isn't that faster-than-light communication?
                          And if you study quantum communications, can't you use that principle
                          somehow for very fast (instantanious) communication?

                          Ok, good luck trying to explain that to an idiot like me. :-)
                          [color=blue]
                          >
                          > Any way thank you a lot Erwin[/color]

                          You are welcome.

                          Regards,
                          Erwin Moller

                          Comment

                          • Erwin Moller

                            #14
                            Re: Dynamic fill form fields depending on dropdown box

                            Krustov wrote:
                            [color=blue]
                            > <comp.lang.ph p , Erwin Moller ,
                            > since_humans_re ad_this_I_am_sp am...yourself.co m>
                            > <43fca149$0$243 73$e4fe514c@new s.xs4all.nl>
                            > <Wed, 22 Feb 2006 18:37:13 +0100>
                            >[color=green]
                            >> I am trying to help people in here, and when I see bad questions (which I
                            >> see a lot lately) I get irritated.
                            >> It seems less and less people know how to post in usenet these days.
                            >>[/color]
                            >
                            > Newbies dont always know the best way to word a help question - or what
                            > info they should supply that would be useful to the advice giver .
                            >
                            > Its part of usenet on any technical newsgroup you care to mention and
                            > always will be .
                            >
                            > If your willing to accept its a futile battle and nothing you ever say
                            > will make any difference as new subscribers almost never read the back
                            > posts and will just ask their question straight away - then isnt it
                            > yourself who is making yourself get irritated .
                            >
                            > Like old leather some things become quite comfortable .[/color]

                            Well, all true. But if you never tell a kid it is considered rude to take a
                            leak against somebody else...

                            Anyway, let's drop this and have a beer instead.
                            *offers a virtual but otherwise great tasting beer*

                            Regards,
                            Erwin Moller

                            Comment

                            • Jerry Stuckle

                              #15
                              Re: Dynamic fill form fields depending on dropdown box

                              Krustov wrote:[color=blue]
                              > <comp.lang.ph p , Erwin Moller ,
                              > since_humans_re ad_this_I_am_sp am...yourself.co m>
                              > <43fc876b$0$110 65$e4fe514c@new s.xs4all.nl>
                              > <Wed, 22 Feb 2006 16:46:51 +0100>
                              >[color=green][color=darkred]
                              >>>I search all over the web but I could not find it.[/color]
                              >>
                              >>Really?
                              >>This is quite basic stuff (both in Javascript and PHP) so I wonder why you
                              >>didn't find any code that does similar things.
                              >>(No punch intended)
                              >>[/color]
                              >
                              >
                              > Do you ever actually help anybody on here who asks a question .
                              >
                              > You only ever seem to comment and give your opinion on 'what' they ask
                              > rather than helping them .[/color]

                              Actually, I agree with Erwin. My comment would have been exactly the same.

                              This is a PHP newsgroup. If he wants a javascript solution, he needs to
                              visit a javascript newsgroup.

                              --
                              =============== ===
                              Remove the "x" from my email address
                              Jerry Stuckle
                              JDS Computer Training Corp.
                              jstucklex@attgl obal.net
                              =============== ===

                              Comment

                              Working...