display data in text box using combo box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
maideen
Forum Newbie
Posts: 2
Joined: Tue Mar 08, 2011 1:20 am

display data in text box using combo box

Post by maideen »

Hi.

I am new to php. I am just in learning stage.

I created a php page to pull the data through combo box and display into text box on screen. I have done to pull the data that means customer code into combo. But if i select code in combo, the information of code,name , address do not appear in the text box.

Can any body help me to learn?

below is the code.

<?php include("header.php"); ?>

<?php

$result = mysql_query("SELECT * FROM Persons order by code;");

if($row = mysql_num_rows($result)> 0 ) {

// Start combo-box
echo "<select name='code'>\n";
echo "<option>-- Select Item --</option>\n";

// For each item in the results...
while ($row = mysql_fetch_array($result))
// Add a new option to the combo-box

echo "<option value='$row

Code: Select all

'>$row[code]</option>\n";
				 
        // End the combo-box
        echo "</select>\n";  
		$strcode=$row['code'];
		      
}        
$sqlSelect="SELECT * FROM Persons WHERE code = '$strcode'";
$resultSelect = mysql_query($sqlSelect);
($rowSelect=mysql_fetch_array($resultSelect))  ;
 ?>
 
<html>
<head>
<body>

<form method="post">
code: <input type="text" name="code" value="<?php echo $rowSelect['code'];?>" ><br>
name: <input type="text" name="name" value="<?php echo $rowSelect["Name"]; ?>" size='40'><br>
address: <input type="text" name="address" value="<?php echo $rowSelect["Address"]; ?>"><br>
</p>
</form> 
</head>
</body>
</html>

Rgd

Maideen
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Re: display data in text box using combo box

Post by Kadanis »

To do an automatic update like that you will need to look at Javascript as the data has already been sent to the browser by the time the combo box and text box are rendered.

If you want to only use PHP you will need to post back to the php script after the combo box is changed and then fill the text box with the required information. This will mean a page refresh
maideen
Forum Newbie
Posts: 2
Joined: Tue Mar 08, 2011 1:20 am

Re: display data in text box using combo box

Post by maideen »

Thanks Kadanis. I don't know the post back function. If you can, Please explain
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Re: display data in text box using combo box

Post by Kadanis »

Post back isn't a function. What I meant was that you make the form post to they php script after the dropdown is changed and then handle its value to populate the text box

Here is an example script that basically does the population of the text box, you should be able to integrate it into your code.

Code: Select all

<?php
//filename: populate.php

if (!empty($_POST)) {
	
	switch ($_POST['code']) {
		case 1: 
			$value = 'You selected 1';
			break;
		case 2: 
			$value = 'You selected 2';
			break;
		default:
			$value = '';		
	}
	
}


?>

<html>
<body>

<form method="post" action="populate.php">

<select name="code" onchange="this.form.submit();">
	<option>Select a Code</option>
	<option value="1">1</option>
	<option value="2">2</option>
</select>

<input type="text" name="test" value="<?php echo $value;?>" />
	
</body>
</html>

Post Reply