|
Posted by Erwin Moller on 02/22/06 19:29
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
[Back to original message]
|