Reply to Re: Dynamic fill form fields depending on dropdown box

Your name:

Reply:


Posted by Erwin Moller on 02/22/06 22:17

Alex wrote:

> Erwin Moller wrote:
>> Alex wrote:
>>
>>> 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.javascript.
>>>>
>>>> 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_fetch_object($dataresults} 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
>>
>> 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="mainselectbox" onChange="populateSecondairy();">
>>
>> 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","pineapple"),
>> "books" => array("HitchHikersGuideToTheGalaxy","Programming
>> Perl","Postgresql"),
>> "animals" => array("dog","cat")
>> );
>>
>> ?>
>> <html>
>> <head>
>> <title>test</title>
>> </head>
>> <body>
>>
>> <form action="bla.php" Method="POST" name="testform">
>> category: <SELECT name="category" onChange="populateSecondSelect();">
>> <?
>> 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 = "";
>> $itemsWithQuotes = array();
>> foreach ($items as $oneItem){
>> $itemsWithQuotes[] = "\"$oneItem\"";
>> }
>>
>> ?>
>> category['<?= $cat ?>'] = new Array(<?=
>> implode(",",$itemsWithQuotes) ?>);
>> <?
>> }
>> ?>
>>
>> function populateSecondSelect(){
>> // get selected category
>> var selCat = document.forms["testform"].category.selectedIndex;
>> var catName = document.forms["testform"].category[selCat].value;
>>
>> // now repopulate the second selectbox
>> var refSecond = document.forms["testform"].item;
>>
>> // remove old options
>> refSecond.length = 0;
>>
>> // add new options
>> var theArr = category[catName];
>> for (var i=0; i< theArr.length ; i++){
>> refSecond.options[refSecond.options.length] = new
>> Option(category[catName][i],"hai");
>> }
>>
>> }
>>
>> </script>
>>
>> </body>
>> </html>
>> --------------------------------------
>>
>>
>> Hope that helps.
>>
>> Good luck,
>> Erwin Moller
>
> Thanks Erwin for the code example, I will try it out and figure how to
> make it work.

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)


>
> 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.

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.

>
> 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.

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. :-)

>
> Any way thank you a lot Erwin

You are welcome.

Regards,
Erwin Moller

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация