Jump to content

Recommended Posts

Hi, i have this form that auto fills text boxes but i cant work out how to make the drop down lists do the same? The customer details are in a table called customers. Can some on point me towards how this is done please ? any help would be great

 

this is the code that seems to work for the text boxes atm

 

<center>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST" name="names">
<table border='0' >
<tr>
<td><p>Select A CustomerID:</p></td>
<td>
<SELECT NAME='results' onchange="this.form.submit();">
<?php	


	$query = "SELECT CustomerID FROM customers"; 
	$result = mysql_query($query);

	while ($line = mysql_fetch_array($result))
	{
	 echo "<OPTION value='$line[0]'>$line[0]</OPTION>";
	}?>
	</SELECT>
</td>
</tr>

 

 

<?php 

if($value=$_POST['results']){

$result = mysql_query("SELECT * FROM customers WHERE CustomerID = '$value'");

$customerID = mysql_result($result,0,'customerID');
$surname = mysql_result($result,0,'surname');
$forename = mysql_result($result,0,'forename');
$addressLine1 = mysql_result($result,0,'addressLine1');
$addressLine2 = mysql_result($result,0,'addressLine2');
$postcode = mysql_result($result,0,'postcode');

}
?>
</form>
<form action="insertEditCustomer.php" onsubmit ="return validate_form(this)" method="post">
<tr>
<td><br></td>
<td><br></td>
</tr>
<tr> 
<td>Customer ID: </td>
<td><input type="text" readonly="readonly"  size ="1" name="customerID" value="<?php echo $customerID ?>" /></td>
</tr>
<tr> 
<td><p>Surname:</td>
<td><input type="text" name="surname" value="<?php echo $surname ?>" /></td>
</tr>
<tr>
<td><p>Forename:</td>
<td><input type="text" name="forename" value="<?php echo $forename?>"/></td>
</tr>
<tr>
<td><p>Adress Line 1:</td>
<td><input type="text" name="addressLine1" value="<?php echo $addressLine1?>"/></td>
</tr>
<tr>
<td><p>Adress Line 2:</td>
<td><input type="text" name="addressLine2" value="<?php echo $addressLine2?>"/></td>
</tr>
<tr>
<td><p>Postcode:</td>
<td><input type="text" name="postcode" value="<?php echo $postcode?>"/></td>
</tr>

<!--------------------------------Daily newspapers--------------------------->
<td><p>Daily Newspapers: </p></td>
<td>
<SELECT NAME='daily1' width='300' style='width: 145px' >
<?php	

	$query = "SELECT paper FROM dailyPapers"; 
	$result = mysql_query($query);

	while ($line = mysql_fetch_array($result))
	{
	 echo "<OPTION value='$line[0]'>$line[0]</OPTION>";
	}?>
	</SELECT>
</td>
<td>
<SELECT NAME='daily2' width='300' style='width: 145px' >
<?php	

	$query = "SELECT paper FROM dailyPapers"; 
	$result = mysql_query($query);

	while ($line = mysql_fetch_array($result))
	{
	 echo "<OPTION value='$line[0]'>$line[0]</OPTION>";
	}?>
	</SELECT>
</td>
<td>
<SELECT NAME='daily3' width='300' style='width: 145px' >
<?php	

	$query = "SELECT paper FROM dailyPapers"; 
	$result = mysql_query($query);

	while ($line = mysql_fetch_array($result))
	{
	 echo "<OPTION value='$line[0]'>$line[0]</OPTION>";
	}?>
	</SELECT>
</td>
</tr>

Link to comment
https://forums.phpfreaks.com/topic/115514-solved-auto-fill-drop-down-listshelp/
Share on other sites

then this won't work

echo "<OPTION value='$line[0]'>$line[0]</OPTION>";

 

it should be

$line[''];

 

and one of these

customerID

surname

forename

addressLine1

addressLine2

postcode

dailyNewspaper1

dailyNewspaper2

dailyNewspaper3

no the code that i have just dose the text boxes ive been playing around with it but no such luck. The drop down list are populated by data from another table dailypapers. but i want to auto fill the drop down list with the newspaper that the customer has in their records so it could then be changed if needed. If that makes sense

 

This is the full code that auto fills the texts boxes:

 

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST" name="names">
<table border='0' >
<tr>
<td><p>Select A CustomerID:</p></td>
<td>
<SELECT NAME='results' onchange="this.form.submit();">
<?php	


	$query = "SELECT CustomerID FROM customers"; 
	$result = mysql_query($query);

	while ($line = mysql_fetch_array($result))
	{
	 echo "<OPTION value='$line[0]'>$line[0]</OPTION>";
	}?>
	</SELECT>
</td>
</tr>

<?php 

if($value=$_POST['results']){

$result = mysql_query("SELECT * FROM customers WHERE CustomerID = '$value'");

$customerID = mysql_result($result,0,'customerID');
$surname = mysql_result($result,0,'surname');
$forename = mysql_result($result,0,'forename');
$addressLine1 = mysql_result($result,0,'addressLine1');
$addressLine2 = mysql_result($result,0,'addressLine2');
$postcode = mysql_result($result,0,'postcode');

}
?>
</form>
<form action="insertEditCustomer.php" onsubmit ="return validate_form(this)" method="post">
<tr>
<td><br></td>
<td><br></td>
</tr>
<tr> 
<td>Customer ID: </td>
<td><input type="text" readonly="readonly"  size ="1" name="customerID" value="<?php echo $customerID ?>" /></td>
</tr>
<tr> 
<td><p>Surname:</td>
<td><input type="text" name="surname" value="<?php echo $surname ?>" /></td>
</tr>
<tr>
<td><p>Forename:</td>
<td><input type="text" name="forename" value="<?php echo $forename?>"/></td>
</tr>
<tr>
<td><p>Adress Line 1:</td>
<td><input type="text" name="addressLine1" value="<?php echo $addressLine1?>"/></td>
</tr>
<tr>
<td><p>Adress Line 2:</td>
<td><input type="text" name="addressLine2" value="<?php echo $addressLine2?>"/></td>
</tr>
<tr>
<td><p>Postcode:</td>
<td><input type="text" name="postcode" value="<?php echo $postcode?>"/></td>
</tr>

<!--------------------------------Daily newspapers--------------------------->
<td><p>Daily Newspapers: </p></td>
<td>
<SELECT NAME='daily1' width='300' style='width: 145px' >
<?php	

	$query = "SELECT paper FROM dailyPapers"; 
	$result = mysql_query($query);

	while ($line = mysql_fetch_array($result))
	{
	 echo "<OPTION value='$line[0]'>$line[0]</OPTION>";
	}?>
	</SELECT>
</td>
<td>
<SELECT NAME='daily2' width='300' style='width: 145px' >
<?php	

	$query = "SELECT paper FROM dailyPapers"; 
	$result = mysql_query($query);

	while ($line = mysql_fetch_array($result))
	{
	 echo "<OPTION value='$line[0]'>$line[0]</OPTION>";
	}?>
	</SELECT>
</td>
<td>
<SELECT NAME='daily3' width='300' style='width: 145px' >
<?php	

	$query = "SELECT paper FROM dailyPapers"; 
	$result = mysql_query($query);

	while ($line = mysql_fetch_array($result))
	{
	 echo "<OPTION value='$line[0]'>$line[0]</OPTION>";
	}?>
	</SELECT>
</td>
</tr>

liek this? "<OPTION value='$line['dailyNewspaper1']'>$line['dailyNewspaper1']</OPTION>";

 

 

 

i get this

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp1\www\R&V Project\editCustomer.php on line 172

not this

"<OPTION value='$line['dailyNewspaper1']'>$line['dailyNewspaper1']</OPTION>";

 

this

<OPTION value="$line['dailyNewspaper1']">$line['dailyNewspaper1']</OPTION>

 

 

 

i tried that and got this Parse error: syntax error, unexpected '<' in C:\wamp1\www\R&V Project\editCustomer.php on line 172

 

Do i need the echo and the ""; ?

  • 3 weeks later...

ive just found out how to do this. This is what i needed to include

 

<SELECT NAME='daily1' width='300' style='width: 145px' >
<?php	

	$query = "SELECT paper FROM dailyPapers"; 
	$result = mysql_query($query);

	while ($line = mysql_fetch_array($result))
	{
	echo "<option";
	if($line[0]==$daily1)
	echo " selected";
	 echo " value='$line[0]'>$line[0]</OPTION>";
	}?>
	</SELECT>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.