Page 1 of 1

The evil select tag...

Posted: Fri Jul 14, 2006 11:24 pm
by whitfield
So I am having problems getting my stuff to appear in a select tag. I want users to be able to manage news, which means they can also delete news items. To do this, I access a MySQL database with all of the news content and *attempt* to list all of the news items that have been posted. The problem is that I do so inside a select tag. I know my code is all pretty prim and proper because it works in other instances where I have used similar code structures. I did some research and found this page:

http://us2.php.net/manual/en/faq.html.php

Scroll down to #4 which seems to note a specific set of instructions for using select tags within PHP. I really don't even know what the code they show does and have tried it to an extent without any luck.

Here is my code...


HTML:

Code: Select all

<td>
					<select multiple name="news_items" size="5" id="news_item_list">
					</select>
				</td>
JavaScript:

Code: Select all

function complete_loadNewsItemList() 
{
	if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" )
	{
		if (xmlHttp.responseText == '' )
			return;
			
		document.getElementById( "news_item_list" ).innerHTML = xmlHttp.responseText;
		alert( xmlHttp.responseText );
	}
}
The alert you'll see in the above code shows exactly what I want it to... <option value="2006-07-14 00:18:30">MySQL Is Working! - 2006-07-14 00:18:30</option>

Because of this fact, I would only find it complicating to show you the PHP. I have pretty much narrowed it down to a couple of facts.


1) when I do

Code: Select all

alert( document.getElementById( "news_item_list" ).firstChild.nodeType );
I get #text and not whatever would indicate that its an element.

2) The option tag works when I place the same text that appears in my alert directly into the HTML

3) When I put the id="news_item_list" in the <td> tag parenting the <select> tag and take out the <select> tag and throw the <select> tag in the PHP instead, I simply get the text of what it is I'm trying to display.




What exactly does the PHP site mean when they talk about using an array of vars? It doesn't make logical sense to me.


Thank you for any of your time and help in advance,
Mike Whitfield

Posted: Sat Jul 15, 2006 12:49 am
by RobertGonzalez
A select multiple tag has the potential for many values. Hence when you post the form you could potential be sending an array of values in that field's POST var. To code these correctly you need to make each select multiple an array...

Code: Select all

<select multiple name="news_items[]" size="5" id="news_item_list">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
<option value="4">Item 4</option>
<option value="5">Item 5</option>
</select>