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!

Fill text field with selected option from dropdown (PHP-Mysql) 2

Status
Not open for further replies.

lexer

Programmer
Jun 13, 2006
432
VE
Hi

I have a mysql table (numberlist) with 2 fields: number and name. In php I've made the connection and create dropdown list that displays the selected number from calls table.
I want to display the name for the selected number in a text field. I was told that I can do it with javascript, but I dont know javascript, This my code:

Code:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form action="DeleteNumber.php" method="post" name="form1">
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("calls", $con);

//Hago query1 selecciono el Departamento a buscar
$query1 = "SELECT * FROM numberlist ".
"ORDER BY number ";
$result1 = mysql_query($query1) or die(mysql_error());

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
//$result = mysql_query ($query);
echo "<select name='number'>";

// printing the list box select command

while($row1=mysql_fetch_array($result1)){//Array or records stored in $nt
//echo "<option value=>$row1[departamento]</option>";
echo"<option value='$row1[number]'>$row1[number]</option>";

/* Option values are added by looping through the array */
}
// printing the list box select command

echo "</select>";// Closing of list box 
?>
      <label>
      <input type="text" name="textfield" value="" />
      </label>
      <input name="submit" type="submit" id="submit" value="Submit" />
</form>
</body>
</html>
[/color]
I will appreciate your help, Thanks!!!
 
Hi

Let us suppose your table numberlist has the following content :
[tt]number | name[/tt]
[tt]-------+------[/tt]
[tt] 1 | One[/tt]
[tt] 2 | Two[/tt]
[tt] 3 | Three[/tt]
And you will the following [tt]select[/tt] ( in fact not so readable because you not break the lines ) :
HTML:
[b]<select[/b] [maroon]name[/maroon][teal]=[/teal][green][i]'number'[/i][/green][b]>[/b]
[b]<option[/b] [maroon]value[/maroon][teal]=[/teal][green][i]'1'[/i][/green][b]>[/b]1[b]</option>[/b]
[b]<option[/b] [maroon]value[/maroon][teal]=[/teal][green][i]'2'[/i][/green][b]>[/b]2[b]</option>[/b]
[b]<option[/b] [maroon]value[/maroon][teal]=[/teal][green][i]'3'[/i][/green][b]>[/b]3[b]</option>[/b]
[b]</select>[/b]
And when the visitor selects let us say "2" in the [tt]input[/tt] called "textfield" should appear the value "Two" ?

If so, I do not like your approach. Why you have the number in both the [tt]option[/tt]s' [tt]value[/tt] and text ? Anyway, I would add the name to the [tt]option[/tt]s as [tt]title[/tt] :
PHP:
[b]echo[/b] [green][i]"<select name=\"number\" onchange=\"this.form.textfield.value=this.options[this.selectedIndex].title\">"[/i][/green][teal];[/teal]

[b]while[/b] [teal]([/teal][navy]$row1[/navy][teal]=[/teal][COLOR=darkgoldenrod]mysql_fetch_array[/color][teal]([/teal][navy]$result1[/navy][teal]))[/teal] [teal]{[/teal]
  [b]echo[/b] [green][i]"<option value=\"$row1[number]\" title=\"$row1[name]\">$row1[number]</option>"[/i][/green][teal];[/teal]
[teal]}[/teal]

[b]echo[/b] [green][i]"</select>"[/i][/green][teal];[/teal]
The JavaScript code is in the [tt]select[/tt]'s [tt]onchange[/tt] attribute.

Or you can build a JavaScript [tt]Array[/tt] with the names and take them from there instead of the [tt]title[/tt] properties.

Or you can use AJAX to get the matching name when the visitor changes something.


Feherke.
 
Thanks feherke, It works!!!
Is It posible to load the number and its name when the programs starts, It shows (no name for the number):
-------+------
1 |

I have to click (for example) on number 2 to see the number's name:

-------+------
2 | Two

This my code:
Code:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form action="DeleteNumber.php" method="post" name="form1">
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("calls", $con);

//Query
$query1 = "SELECT * FROM numberlist ".
"ORDER BY number ";
$result1 = mysql_query($query1) or die(mysql_error());

//$result = mysql_query ($query);
echo "<select name=\"number\" onchange=\"this.form.textfield.value=this.options[this.selectedIndex].title\">";

// printing the list box select command
while($row1=mysql_fetch_array($result1)){
	echo "<option value=\"$row1[number]\" title=\"$row1[name]\">$row1[number]</option>";
}

echo "</select>";// Closing of list box 
?>
      <label>
      <input type="text" name="textfield" value="" />
      </label>
      <input name="submit" type="submit" id="submit" value="Aceptar" />
</form>
</body>
</html>

 
Hi

I would move the JavaScript code into a [tt]function[/tt] and I would call it [tt]onload[/tt] too :
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]showname[/color][teal]([/teal]what[teal])[/teal]
[teal]{[/teal]
  what[teal].[/teal]form[teal].[/teal]textfield[teal].[/teal]value[teal]=[/teal]what[teal].[/teal]options[teal][[/teal]what[teal].[/teal]selectedIndex[teal]].[/teal]title
[teal]}[/teal]

window[teal].[/teal]onload[teal]=[/teal][b]function[/b][teal]()[/teal] [teal]{[/teal]
  [COLOR=darkgoldenrod]showname[/color][teal]([/teal]document[teal].[/teal]form1[teal].[/teal]number[teal])[/teal]
[teal]}[/teal]
Code:
[b]echo[/b] [green][i]"<select name=\"number\" onchange=\"showname(this)\">"[/i][/green][teal];[/teal]

Feherke.
 
Thanks feherke, It works fine!!!

My code:
Code:
<script language="JavaScript">
function showname(what)
{
  what.form.textfield.value=what.options[what.selectedIndex].title
}

window.onload=function() {
  showname(document.form1.number)
}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form action="DeleteNumber.php" method="post" name="form1">
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("calls", $con);

//Query
$query1 = "SELECT * FROM numberlist ".
"ORDER BY number ";
$result1 = mysql_query($query1) or die(mysql_error());

//$result = mysql_query ($query);
echo "<select name=\"number\" onchange=\"showname(this)\">";

// printing the list box select command
while($row1=mysql_fetch_array($result1)){
	echo "<option value=\"$row1[number]\" title=\"$row1[name]\">$row1[number]</option>";
}

echo "</select>";// Closing of list box 
?>
      <label>
      <input type="text" name="textfield" value="" />
      </label>
      <input name="submit" type="submit" id="submit" value="Aceptar" />
</form>
</body>
</html>
 
Hi

Just some HTML notes :
[ul]
[li]First I thought you missed the [tt]html[/tt] tag accidentally while copy & pasted the code, but I see it never appears.[/li]
[li]There is no life outside the [tt]head[/tt] and [tt]body[/tt].[/li]
[li]The [tt]type[/tt] attribute is mandatory, the [tt]language[/tt] attribute is deprecated for the [tt]script[/tt] tag.[/li]
[/ul]
Code:
[red]<!DOCTYPE[/red] [maroon]html[/maroon] [maroon]PUBLIC[/maroon] [green][i]"-//W3C//DTD XHTML 1.0 Transitional//EN"[/i][/green] [green][i]"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"[/URL][/i][/green][red]>[/red]
[b]<html>[/b]

[b]<head>[/b]
[b]<meta[/b] [maroon]http-equiv[/maroon][teal]=[/teal][green][i]"Content-Type"[/i][/green] [maroon]content[/maroon][teal]=[/teal][green][i]"text/html; charset=iso-8859-1"[/i][/green] [b]/>[/b]
[b]<title>[/b]Untitled Document[b]</title>[/b]

[b]<script[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"text/javascript"[/i][/green][b]>[/b]
//<![CDATA[
function showname(what)
{
  what.form.textfield.value=what.options[what.selectedIndex].title
}

window.onload=function() {
  showname(document.form1.number)
}
//]]>
[b]</script>[/b]
[b]</head>[/b]

[b]<body>[/b]

[gray][gray]<!-- the rest of the code is OK -->[/gray][/gray]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top