how to refresh page every time option is selected?

A

Anonymous

Guest
I have a HTML form with dropdown list (<select> and <option>) to show doctor's codes. For example, the codes and the related names are:

Code Name
-------------------
001 Dr. One
002 Dr. Two
003 Dr. Three

What I want is, everytime I select the code from the list, I want the name of the doctor is also showed on the same page.

I'm using PostgreSQL database. Table in PostgreSQL :

table name : doctors
field : docID, docName

Here is my code:
--------------------
<tr>
<td>Code</td>
<td>:</td>
<td>
<table>
<tr>
<td>
<select name="code">
<?
$db=pg_connect("host=localhost dbname=dbhis user=user");
$result=pg_query($db,"select * from doctors;");
while (list($docID) = pg_fetch_row($result)) {
echo "<option value=\"$docID\">$docID</option>";
}
pg_close($db);
?>
</select>
</td>
<td>Name</td>
<td>:</td>
<td>
<?
...........
...........
?>
</td>
</tr>

The code to show doctor's code works fine, but I can't find a way to show the name. What should I fill in the blanks?

Please help me. I'm really stuck. Thanks a lot.

rgrds,
jenny
 
Try this out!
Code:
<table>
<tr> 
<td>Code</td> 
<td>:</td> 
<td> 
<table> 
<tr> 
<td> 
<form name="mydocs">
<select name="code" onChange="javascript:DocName.innerHTML = document.mydocs.code.options[document.mydocs.code.selectedIndex].value;"> 
<? 
$db=pg_connect("host=localhost dbname=dbhis user=user"); 
$result=pg_query($db,"select * from doctors;"); 

// Listing the Doc. Name too for the <option> value!
while (list($docID, $docName) = pg_fetch_row($result)) { 
echo "<option value=\"$docName\">$docID</option>"; 
} 
pg_close($db); 
?> 
</select> 
</form>
</td> 
<td>Name</td> 
<td>:</td> 
<td id="DocName"></td> 
</tr>
</table>
If you need to get the ID of selected Doc, you can use the next javascript code:
Code:
document.mydocs.code.options[document.mydocs.code.selectedIndex].text;
 
I've guessed that this problem should be solved with innerHTML. I've tried the code but it hasn't worked for the doctor's name.

Can you explain what this line means?

DocName.innerHTML = document.mydocs.code.options[document.mydocs.code.selectedIndex].value;">

'document' refers to the page containing the script, right?
'mydocs' refers to the form.
'code' refers to what?


Thank you so much, gesf.

rgrds,
jenny
 
I'm not good with JavaScript, but I can guess this one. "code" refers to the name of your select box. "options" refers to its options. "selectedIndex" refers to the index of the currently selected option. And "value" refers to that option's value.
 
sorry, it took me so long to try the code. I've been sick and slept in bed for 3 days. Now I'm ready to make some scripts again.

Your code works!! thank you so much, gesf & swirlee!!

I change it a bit.

DocName.innerHTML = document.mydocs.code.options[document.mydocs.code.selectedIndex].value;">

this code make the page still contains errors.
but after I eliminate the 'document.mydocs', so it becomes:

DocName.innerHTML = code.options[code.selectedIndex].value;">

it runs well.
thank you so much, this is the first time I ever post a question in a forum and I feel so helped.

rgrds,
jenny
 
I´m glad you made it!

Well, sometimes (this case) we can ignore the javascript document property, as it refers to the actual HTML document in the window! And consequently the form name!
If we were working with frames... it would be necessary!

This is working for you:
DocName.innerHTML = code.options[code.selectedIndex].value;

Fine! But, if you add another form to your document, even if it´s in another file that is included in this one, you may need to use a form on your select option and refers to its name in the code above.

regards
 
I'm having the same problem. I've tried to change my codes similarly to the suggestion/solution as above. But it still gives me a syntax error...

Can anybody help me with this??

Code:
include('connect2db.php');
     $query = "SELECT employee.Emp_ID FROM employee,supervisor WHERE employee.Supervisor_ID = supervisor.Supervisor_ID AND supervisor.Supervisor_ID = '".$_SESSION['EmpID']."' ORDER BY employee.Emp_ID;";
     $result = mysql_query($query);
     echo '<select name="emID" onChange=="javascript:DocName.innerHTML = emID.options[emID.selectedIndex].value;" id="emID" style="width: 100px;">';
     while($row = mysql_fetch_array($result,MYSQL_NUM)){
         echo '<option value=$row[0]>'.$row[0].'</option>';

     }
     echo '</select>';

     if(isset($_POST['emID'])){
         $EID = $_POST['emID'];
         $query2 = "SELECT Emp_Name FROM employee WHERE Emp_ID = '$EID';";
         $result2 = mysql_query($query2);
         while($row2 = mysql_fetch_array($result2,MYSQL_NUM)){
             $EmpName =  $row2[0];

         }

     }
     echo '<input type="text" style="width: 250px; name="EName" id="EName" value="'.$EmpName.'"></input>';
 
Well, first of all i suggest you to use a form on your select option!

You need to start using the superglobals array.
That means, when getting something from a form you need to use $_POST or $_GET array. Which one to use depends on which form method you're using. Otherwise (not from form), use the $_GET and $_REQUEST array.

Example:
Code:
<?php
echo '<input type="text" style="width: 250px; name="EName" id="EName" value="' . $_POST['EmpName'] . '"></input>';
?>

What´s the error, Undefined index: EmpName !?
That´s what i get in the last line!

That´s just a notice and it´s not a malicious error. You can turn it off, by using error_reporting(E_ALL & ~E_NOTICE);!
Anyway, i reccomend that when developing code you have the error level set to E_ALL. That way you will get notifyed about all errors that occur. It's a good way to learn ;)

You can simply replace your last line with the next one and everything will be alright:
Code:
<?php
echo '<input type="text" style="width: 250px; name="EName" id="EName" value="$EmpName"></input>';
?>
Anyway, don´t forget the superglobals array!!

Another thing, you´re using the above post javascript code and i don´t see the element 'DocName' in your html!? For you to see what happens, add the next line in your code(at last):
Code:
echo '<a name="DocName"></a>';
Ok, it will only work if your select option is working too!
 
Back
Top