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 derfloh 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 1

Status
Not open for further replies.

Pampers

Technical User
Apr 7, 2004
1,300
AN
Hi Everyone,
I'm new to the scripting business... 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), 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 (telephone code that is) of the Netherlands is displayed (0031). How would I go about that?

Pampers [afro]
Keeping it simple can be complicated
 
If the value of the options is the country code, e.g:

Code:
<select>
   <option value="44">UK</option>
   <option value="1">US</option>
</select>

then you can simply add an onchange event handler to the select element to alert the value:

Code:
<select onchange="alert(this.value);">

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I realised you might not have meant an alert box when you said "text box", so have put this together for you:

Code:
<html>
<body>
	<form>
		<select onchange="this.form.elements['output'].value = this.value;">
			<option value="">Select a country</option>
			<option value="31">Netherlands</option>
			<option value="44">UK</option>
			<option value="1">US</option>
		</select>
		<input type="text" name="output" />
	</form>
</body>
</html>

It outputs the value in a standard form text box.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Tnx BillyRayPreachersSon,
Much appreciated. But I'm unsure where I put the code. For now it is ok if the output goes to an alert-box and only displays the value the user selected.

Tried a few things, but no go... This is my code (I commented out the onchange-event:

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>"; 
//<select size="1" onchange="alert(this.value)">

    //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 no PHP expert, but it looks like you're missing quotes in some places, have nested quotes in others, and have really messed up the nesting of your loop. Try this for size, but if it doesn't work, ask for more help in the PHP forum:

Code:
print "<select name='item' onchange='alert(this.value);'>";

while ($line = mysql_fetch_array($result)) {
	foreach ($line as $value) {
		print "<option value='$value'>$value</option>";
	}  
}
print "</select>";

Also, is "print" a PHP function? Again, I'm no expert, but I've used "echo" before. Maybe if this doesn't work, you can ask for more help on this in the PHP forum.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
But, this seems a classic mixing client-side with server-side script?! Search is on server-side. Have to post the form back to do the job, or again aj-thing if you like it hot.
 
Hi tsuji, yes, I can see it, since js is client-side scripting, and php server-side.


Hi BillyRayPreachersSon, yep, I forgot the quotes in print "</SELECT>"; FOr the rest, the php works fine.

So, I would be better of having this handled in PHP? - I reposted this thread in the PHP forum.


Pampers [afro]
Keeping it simple can be complicated
 
Back again.
BillyRayPreachersSon, I used your code, and that is working, to a degree. If I use alert, it works, and it puts the value on screen. If I use form.elements, nothing happens....(I commented it out). Any suggestions.
,
<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);

//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();
//print "</SELECT>";
?>
</select>
<input type="text" name="output"/>

Pampers [afro]
Keeping it simple can be complicated
 
No, let's call it a day. I'm pretty happy with the result and progress made. Tnx for the patience and the help.

Pampers [afro]
Keeping it simple can be complicated
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top