/    Sign up×
Community /Pin to ProfileBookmark

Using DOM to populate SELECT

I am trying to get the following:

Add a new row to a table on demand (done)
In this row there should be two select boxes
– the first is populated by PHP (done)
– the second is populated by the option selected in the first (not done)
– as there could be (in theory) infinite rows, these would need to be as an array… but I am not sure how to relate to these…

any ideas?

code is as below:

[code]
<html>
<HEAD>

<?php
include(“db.php”);
$query=do_query(“SELECT company FROM `suppliers`”);
while(list($company)=mysql_fetch_row($query))
{
$query=do_query(“SELECT ref,productname,modelnumber,ordered_ph,instock,minstock FROM `”.$company.”`”);
while(list($ref,$product,$model,$ordered,$instock,$minstock)=mysql_fetch_row($query))
{
@$array .=”””.$company.””,””.$model.” – “. $product.””,”;
}
}
$array=substr($array,0,-1);
?>
<SCRIPT LANGUAGE=”JavaScript”>
var supplier_toCategoryMap = new Array(
<? echo $array;?>);
function supplier_popCategory(supplier, category) {
var manCode = supplier.options[supplier.selectedIndex].value;
var i, j;

j = category.options.length = 1;
category.options.selectedIndex = 0;

for(i=0; i<supplier_toCategoryMap.length/4; i++) {

if (supplier_toCategoryMap[i*4] == manCode) {
category.options.length = j+1;
category.options[j].value = supplier_toCategoryMap[i*4+2];
category.options[j].text = supplier_toCategoryMap[i*4+1];

j += 1;
}

}
return;
}
<!– begin
function addRow(id){
var tbody = document.getElementById
(id).getElementsByTagName(“TBODY”)[0];
var row = document.createElement(“TR”);
var td1 = document.createElement(“TD”);
var newForm = document.createElement(‘select’);
newForm.setAttribute(‘name’,’supplier[]’);
newForm.setAttribute(‘class’,’textbox’);
newForm.setAttribute(‘onChange’, ‘supplier_popCategory(this, document.form1.category)’);
newOpt = document.createElement(“option”);
newOpt.setAttribute(“value”, “”);
newOpt.appendChild( document.createTextNode(“– choose one –“) );
newForm.appendChild(newOpt);
<?
$query=do_query(“SELECT company FROM `suppliers`”);
while(list($supplier)=mysql_fetch_row($query))
{
?>
newOpt = document.createElement(“option”);
newOpt.setAttribute(“value”, “<?echo $supplier;?>”);
newOpt.appendChild( document.createTextNode(“<?echo $supplier;?>”) );
newForm.appendChild(newOpt);
<?
}
?>
td1.appendChild(newForm);
var td2 = document.createElement(“TD”)
var newForm = document.createElement(‘select’);
newForm.setAttribute(‘name’,’category[]’);
newForm.setAttribute(‘class’,’textbox’);
newOpt = document.createElement(“option”);
newOpt.setAttribute(“value”, “”);
newOpt.appendChild( document.createTextNode(“– choose one –“) );
newForm.appendChild(newOpt);
td2.appendChild(newForm);
row.appendChild(td1);
row.appendChild(td2);
tbody.appendChild(row);
}
// End –>

</script>

</HEAD>
<BODY>
<a href=”javascript:addRow(‘myTable’)”>Add row</a>
<form name=”form1″ method=”POST” action=”answers.php”>
<input type=”submit” name=”submit” value=”submit”>
<table id=”myTable” cellspacing=”0″ border=”1″>
<tbody>
<tr>
<th>Supplier</th><th>Product</th>
</tr>
</tbody>
</table>
</form>
</body>
</html>
[/code]

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@FangJan 05.2004 — If you are referring to the table rows, just give them an id on creation.
Copy linkTweet thisAlerts:
@The_ChancerauthorJan 05.2004 — I'm not referring to the table rows, but I need to refer to the particular dropdown (called categories)

The details for the categories in the second dropdown are populated, and should appear when the respective supplier is chosen in the first dropdown as shown in the supplier_popcategory function.

When seems to be the problem is that it doesn't know which category to use - as is states that is has no properties. (Mozilla 0.7) But as these objects (categories) are as an array so that I can sort through them on the second page - I am unsure of how to refer to them ID's might be a way - but I am at a loss as to how to code it...

Any ideas / links would be very grateful....
Copy linkTweet thisAlerts:
@FangJan 05.2004 — Still a little ? Is it the onChange selection?

onChange="supplier_popCategory(this.options[this.selectedIndex].value)";
Copy linkTweet thisAlerts:
@The_Chancer2Jan 06.2004 — Sorry - being a little slow on the explanation....

The add new line works, so I get a new line with two select boxes in it. The first one (called products as an array) is populatec from the database and comes out with my suppliers - all working fine.

In a static page I have built previously, the suppliers select box, when the property is changed and a new one is selected, the products for that particular supplier show up - and change according to the supplier shown in the original box.

When I now try to do it with the new row being added dynamically, it doesn't seem to want to work, and I am lost..

Does that make it any clearer ?

I have changed the code in the original post, so as to have the actual code, used, not including the PHP...


[edited and code removed to make thread smaller and easier to read - see below for attachment]
Copy linkTweet thisAlerts:
@FangJan 06.2004 — That's much clearer.

The error is [COLOR=green]newForm.setAttribute('name','category[COLOR=green][][/COLOR]');[/COLOR]

Drop the [COLOR=red][][/COLOR]

Also in supplier_popCategory(), not an error, but

supplier.options[supplier.selectedIndex].value is the same as supplier.value
Copy linkTweet thisAlerts:
@The_Chancer2Jan 06.2004 — Thanks for that... it does now work for the first row, however it doesn't for any subsequent ones...

I would have thought that this is because it is still referring to the "category" name in each occurrence - the only way in which to do this would be to append a number on the end would it not ?
Copy linkTweet thisAlerts:
@The_Chancer2Jan 06.2004 — File attached

File updated - working copy.

Fang - many thanks for noting the error...

[upl-file uuid=8713a1df-2816-48fc-bb71-10f24ade25f3 size=3kB]test.txt[/upl-file]
Copy linkTweet thisAlerts:
@FangJan 06.2004 — A few changes:

I changed the way array supplier_toCategoryMap is setup.

Simpler this way, but change it back if need be.

All select objects have indicies.

There is a browser object detection for the "onchange".

IE and Moz. handle this differently.

The program now works as expected.
Copy linkTweet thisAlerts:
@The_Chancer2Jan 07.2004 — Would there have to be a browser object detection for the onClick as well ?

I am using :

<i>
</i> if(document.all) { newForm.setAttribute('onClick', function() {addRow('myTable');}); }//IE
else { newForm.setAttribute('onClick', "addRow('myTable')"); } //Moz.


Moz works fine - but IE doesn't work....
Copy linkTweet thisAlerts:
@The_Chancer2Jan 07.2004 — I have had to go back to the original way or running the query to populate the two select boxes - as stripping the company name off the front in the result page was being annoying...

Everything works fine in Moz now, and everything will work in IE - apart from the supplier dropdown populating the second dropdown.

IE error is "options is null or not an object"....

Everything works, it is just the information that is passed to the supplier_popCategory that fails.

I have passed the objects over, yet it doesn't seem to get there...

Any ideas ?

I have attached the code...

[upl-file uuid=7c6e1772-7ae0-431a-b8cb-0d72c6e3945d size=13kB]test.txt[/upl-file]
Copy linkTweet thisAlerts:
@FangJan 07.2004 — You can't do this: [COLOR=green]"document.form1.category"+num[/COLOR] it won't work.

It may work using eval("document.form1.category"+num)

The approach I used of finding the Product menu corresponding to the Supplier menu works, just pass supplier:
function supplier_popCategory(supplier) {
var manCode = supplier.value;
var i, j;
// get index to Supplier menu
for (var i=0; i &lt; document.form1.length; i++) {
if(document.form1.elements[i].name==supplier.name) {i++; break;}
}
// corresponding Product menu
var category=document.form1.elements[i];
j = category.options.length = 1;
category.options.selectedIndex = 0;

for(i=0; i&lt;supplier_toCategoryMap.length/2; i++) {

<i> </i>if (supplier_toCategoryMap[i*2] == manCode) {
<i> </i> category.options.length = j+1;
<i> </i> category.options[j].value = supplier_toCategoryMap[i*2+1];
<i> </i> category.options[j].text = supplier_toCategoryMap[i*2+1];

<i> </i> j += 1;
<i> </i>}

}
return;
}

It does mean that [COLOR=green]<select name="supplier0"[/COLOR] and [COLOR=green]newForm.setAttribute('name','supplier'+x);[/COLOR]

have to have an index.

Without it all Supplier menus has the same name, very confusing for the browser.

You can also set the onclick handler for IE:

[COLOR=green]newForm.onclick=addRow;[/COLOR]
Copy linkTweet thisAlerts:
@The_Chancer2Jan 07.2004 — Genius....

Many Thanks,

In addition to this - as I needed to know the number of the rows, I just used a counter into a hidden input - and used that in the next page.

But apart from that - excellent - any thanks again for your help.

?
×

Success!

Help @The_Chancer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...