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!

onclick event to lookup value in mysql table

Status
Not open for further replies.

Pampers

Technical User
Apr 7, 2004
1,300
AN
Hi Everyone,
This is a repost of thread216-1287339 in the javascript-forum (no replys, so I assume i posted it in the wrong forum)

I made a little database (mysql), then wrote some php and html to create and fill a listbox. Works.

Now I want the user to select a value from the listbox (a country), maybe click a button, and then display in a textbox what the corresponding countrycode is.

Example:
User Selects 'The Netherlands' from the listbox. Then presses 'button', and then the countrycode of the Netherlands is displayed (0031). How would I go about that?

Pampers [afro]
Keeping it simple can be complicated
 
i suspect that this is mainly a javascript problem.

the crude and php way of achieving this is to tie the onchange event of the list/select box to the form.submit() action of a form which wraps the select box. php can then regenerate the page with the right dialing code prefix. this requires a page refresh

the client-side-only way of doing this is entirely javascript and better placed in the js forum. A crude solution might be something like this:

Code:
<div>
 <span>
  <select onchange="document.getElementById('countrycode').innerHTML='Country code is: ' + this.value;" id="selectbox">
   <option value="00 31">The Netherlands</option>
   <option value="00 44">The UK</option>
   <option value="00 33">France</option>
   <option value="00 34">Spain</option>
  </select>
 </span>
 <span id="countrycode">
  &nbsp;
 </span>
</div>
 
Hi jpadie,
tnx for the response. I think for now I would like to try a PHP-solution. This is what I have so far.

Code:
<?
//dim
$database="PhoneCode";

//connect to my-sqlserver
mysql_connect(localhost);

//connect to database
@mysql_select_db($database) or die( "Unable to select database");

//select records
$query="SELECT Country FROM tblIntPhoneCode";

//put records in resultset
$result=mysql_query($query);

//build listbox
print "<SELECT name=item>"; 
    //loop and build array from resultset
    while ($line = mysql_fetch_array($result)) 
      { 
      foreach ($line as $value) 
       { 
         print "<OPTION value='$value'"; 
      }  
         print ">$value</OPTION>"; 
      } 
    //close connection to my-sqlserver
	mysql_close();
  // <select onchange="alert(this.value);">
print "</SELECT>"; 
?>

Pampers [afro]
Keeping it simple can be complicated
 
i'm confused.

you have posted code to produce a select box. you have not shown any code with which you have been trying to show the VALUE of the select box.

the best way to do this is not to force the user to interact with the server as the data involved is pretty small. use javascript like I suggested or as you seemingly have previously tried with the alert(this.value) code.

i'd recommend being explicit in your db connect statement
Code:
mysql_connect("localhost", $dbusername, $dbpassword);
 
Hi jpadie,
I didn't put any code to show the value, because I didn;t have a clou how to do that! But I have found the alert-function working okee (the textbox output didn't do it).
Now, would this alert-function considered to be(X)HTML DOM?


<select onchange="alert(this.value);">
<!--select onchange="this.form.elements['output'].value = this.value;"-->
<?php
//dim
$database="PhoneCode";

//connect to my-sqlserver
mysql_connect("localhost", $dbusername, $dbpassword);

//connect to database
@mysql_select_db($database) or die( "Unable to select database");

//select records
$query="SELECT PhoneCode, Country FROM tblIntPhoneCode";

//put records in resultset
$result=mysql_query($query);

//build listbox
//print "<SELECT name=item>";
//loop and build array from resultset
while ($line = mysql_fetch_array($result))
{
foreach ($line as $value)
{
print "<OPTION value='$value'";
}
print ">$value</OPTION>";
}

//close connection to my-sqlserver
mysql_close();
//print "</SELECT>";
?>
</select>

Pampers [afro]
Keeping it simple can be complicated
 
is it xhtml compliant? not really. run it through a validator to be sure.

will the js work? no (at least not as you have posted above) as you have not included any form element called "output".

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top