Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

on change combo box

Status
Not open for further replies.

miroiti

Programmer
Jun 20, 2009
18
EG
hi all,
iam just a begineer in java script,and i tried my program that load data from a database in combo box,my table is managelinks that contain 2 fields title and url ,the title column is loaded in combo box and i tried when the user changes the value of this combo box i select from database the crossponding url and open it ,but it doesnot work well,

this is the code
<code>
<select onchange="address(this)" name="select" id="select">'

<script>
function address(obj)
{
var dropdownIndex = document.getElementById('select').selectedIndex;
var dropdownValue = document.getElementById('select')[dropdownIndex].text;
//alert(dropdownValue);
<?
$query = "SELECT url FROM " .
"{managelinks } where title='dropdownValue'";

$queryResult = db_query($query);

while ($arr = db_fetch_object($queryResult))
{
?>
alert("$arr[1]");
document.forms[0].cat_name.value= obj.options[obj.selectedIndex].text;
<?
}
?>
</script>
</code>

 
It seems that you are over-complicating the JS script. If the combobox already the url as the value or as the text, you should be able to use the passed object to reference the url.

Code:
function address(it) {
  var url = it.value;    // or  var url = it.text;
  window.open(url);
}

I am not sure if you are intending to run the PHP code as the page is originally rendered or as a trigger to the onchange() event.

One thing that would help is knowing more about your select box - what do <option ...> look like ... ???



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
I'm guessing you're a beginner in PHP as well, because one thing you should know is that server-side code like that is always executed BEFORE the page is rendered to the browser.

This means that the PHP in your function will have run server-side before the browser gets the page, so the variable 'dropdownValue' will never be known (not that it would have been passed if it was known anyway).

If you need to pass data from form elements back to PHP to perform some more queries, you should probably use one of these 3 techniques:

1: Use a bog-standard form submission, with the whole page being re-rendered.

2. Use AJAX (see forum1600).

3. Use a hidden iframe (now considered to be 'old hat' with AJAX becoming more mainstream, but still valid and still works very well).

I can also recommend that you ask in forum434 about how to write PHP that can pass parameters to DB queries, because it doesn't look like you know that, and here isn't really the right forum to go into it.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top