hai

have one combo box and text box and one submit button in(page1.php). i want to search from DB for some records based one combo box item and text typed in textbox by clicking search button.

i select one item from combo box, and type text in text box and click submit(search) button, the result will appear on the same page(page1.php) below the submit button.

but my problem is if i click submit button,the text box content disappears and combo box displays first item in the list(not selected item for me). how can i retain the values when i click submit button.

Well, what I tell you is entirely dependent on how you go about submitting the form (POST or GET) and the way you made your drop down list. Please copy and paste your source code so that I can look at it. After that I will be able to give you a more direct answer.

hai this is my code,

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Order Report</title>
<script type="text/javascript" language="javascript">
function change(code)
{
	location.href="sbhorder.php?id=" + code;
}

</script>
</head>

<body>
<form name="sbhorder" id="sbhorder" method="post" action="sbhorder.php">
<table border="0" cellpadding="1" cellspacing="1" align="center">
<tr>
<td align="right" width="300px">Select Type:</td>
<td align="left" width="300px"><select name="searchtype" id="searchtype" onchange="change(this.value)">
<option value="nothing">---Select Type---</option>
<option value="productcode">Product Code</option>
<option value="orderid">Order Id</option>
</select>
</td>
</tr>
<tr>
<td align="right" width="300px">
<?php
if(isset($_GET['id']))
{
	$id=$_GET['id'];
}

if ($id==nothing)
{
?>
Enter Value:
<?php
}
else if($id==productcode)
{
?>
Enter Product Code:
<?php
}
else if($id==orderid)
{
?>
Enter Order ID:
<?php
}
?>
</td>
<td align="left" width="300px"><input type="text" name="code" id="code" /></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="button" name="btnsubmit" id="btnsubmit" value="Submit" /></td>
</tr>
<tr>
<td align="center" colspan="2">
<?php
if (isset($_POST['btnsubmit']))
{
	$searchtype=$_POST['searchtype'];
	$productcode=$_POST['code'];
	$orderid=$_POST['code'];
	$owner_id=11;
	
	if ($searchtype=="productcode")
	{
		$orderid=0;
	}
	if ($searchtype=="orderid")
	{
		$productcode="";
	}
	$_SESSION['searchtype']=$searchtype;
	$_SESSION['productcode']=$productcode;
	$_SESSION['orderid']=$orderid;
	$_SESSION['owner_id']=$owner_id;
	$exec=mssql_init("usp_sbh_get_order_report");
	mssql_bind($exec,"@sbSearchType",$searchtype,SQLVARCHAR);
	mssql_bind($exec,"@sbProductCode",$productcode,SQLVARCHAR);
	mssql_bind($exec,"@sbOrderId",$orderid,SQLINT4);
	mssql_bind($exec,"@sbOwnerId",$owner_id,SQLINT4);
	
	$result=mssql_execute($exec);
$rows=mssql_num_rows($result);
if ($rows > 0)
{	
	echo '<table border="1" cellpadding="1" cellspacing="1"><tr><td colspan="9" align="center"><b>Order Report</b></tr><tr><td nowrap><b>Order ID</b></td><td nowrap><b>Product Code</b></td><td nowrap><b>Product Title</b></td><td><b>Category</b></td><td><b>Status</b></td><td><b>Quantity</b></td><td nowrap><b>Vendor Price</b><td nowrap><b>SkyBuy Price</b></td><td nowrap><b>Owner Type</b></td>';
	while($row=mssql_fetch_array($result))
	{
		echo '<tr><td>'.$row["order_id"].'</td>';
		echo '<td>'.$row["prod_code"].'</td>';
		echo '<td>'.$row["prod_title"].'</td>';
		echo '<td>'.$row["cate_name"].'</td>';
		echo '<td>'.$row["status"].'</td>';
		echo '<td>'.$row["qty"].'</td>';
		echo '<td align="right">'.$row["vend_price"].'</td>';
		echo '<td align="right">'.$row["sbh_price"].'</td>';
		echo '<td>'.$row["owner_type"].'</td></tr>';
		$total_vend_price+=$row["vend_price"];
		$total_sbh_price+=$row["sbh_price"];
		
	}
echo '<tr><td colspan="6" align="right"><b>Total</b></td><td align="right">'.$total_vend_price.'</td><td align="right">'.$total_sbh_price.'</td></tr>';
	echo '<tr><td colspan="9" align="center"><a href="sbhorderprint.php"><img src="images/print.png" border="0" height="35px" width="35px" alt="Print" title="Print"></a><a href="sbhorderpdf.php"><img src="images/pdf.jpg" border="0" height="35px" width="35px" alt="Export PDF" title="Export PDF"></a></table>';
	
}

else
{
	echo '<table align="center"><tr><td align="center"><b>No Results Found</b></td></tr></table>';
}
}
?>
</td>
</tr>
</table>
</form>
</body>
</html>

Alright, no problem. The first thing we'll do is make sure that your drop down menu retains its value. In order to do this, we check the POST value of the select field and then add selected="selected" to the correct option, like so.

<option value="nothing">---Select Type---</option>
<option value="productcode"<?php echo ($_POST['searchtype'] == 'productcode') ? ' selected="selected"' : ''; ?>>Product Code</option>
<option value="orderid"<?php echo ($_POST['searchtype'] == 'orderid') ? ' selected="selected"' : ''; ?>>Order Id</option>
</select>

Here I've used a short-hand version of an IF statement. It simply checks the value of 'searchtype' for the value of both of the drop down options. Very useful.

For input boxes, it's even simpler. You just echo out the field's value from the POST variable.

<input type="text" name="code" id="code" value="<?php echo $_POST['code']; ?>" />

I hope that helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.