Selected value in drop down

Hi All,
I am using this code to populate a drop down list from mysql database.
I need to pass id and name through this code.but the problem is the value is lost when the page gets refreshed.and it works fine if i pass only name through it.
my code is as follows:

<?PHP
$query=mysql_query("SELECT * FROM client_master WHERE client_status=1");?>
<select name='name' onChange='javascript:form_submit();' style='width:80px'>
<option value="">-SELECT-</option>
<?php

while($row=mysql_fetch_array($query))
{
$name=$row['client_name'];
?>
<option value="<?php echo $row['client_id'] ?>" <?php echo($row['client_name']==$_POST['name'])?'selected':''?>>
<?php echo $row[client_name];?>
</option>
<?php
}
?>
</select>
<script language="javascript">
function form_submit(){
	document.gr.submit();
}
</script>

:confused:

What is the resulting HTML code that your PHP script generates?
That should help to make the problem loud and clear.

ok.more precisely, i am using this form in such a way that,when client name is selected, it will submit the form and will show the quotation numbers related to that client.but when the value is submitted,it shows the quotations, but dosent show the client name.is there any solution for this?? :frowning:

Yes there is. To get to the solution, more needs to be known about the problem.
What is the bad HTML code. You can see the rendered HTML code by right-clicking on the page and selecting “View page source”, or something equivalent.

I suspect that the problem is that you are placing the name as an attribute of the html element instead of as the content part of the element itself, but the only way to find out for sure is to confirm that suspicion against the actual HTML code itself.

By knowing what’s going bad, we can adjust the PHP script so that it created good code. Anything else and we’re just making blind stabs in the dark.

Yes it is not showing any value in “selected” attribute in page source.probably because client_id is not posted.
please have a look at the example:http://believecreation.org/test/test.php.
when values are selected from both of the drop downs,the values are lost.:(:(:frowning:

The form posts the value that’s in the value attribute, which in this case is 1 or 2.
The comparison you make in your PHP file is currently checking the name, instead of the id.

You can choose one of two possible solutions:

  1. either use the name in the value field
  2. or check the id number against the posted number

I have stored the client_ids in the table.so i cant just pass the name.
“check the id number against the posted number” sounds cool.but seriously dont know how i can achieve this.if i use

<option value="<?php echo $row['client_name'] ?>" <?php echo($row['client_name']==$_POST['name'])?'selected':''?>>

it works as it has to work.but then i cant check the values in other database which is related to the quotation number.

The problem is that $_POST[‘name’] currently contains a value of 1, or a value of 2, and you are checking if a string equals the number.
For example:

'Anita' == '1' ? 'selected' : ''

What do you want to do about that?

i dont know how to do this.isnt thr any way out??or i have to change all my queries to insert the client_names instead??

If you don’t want to change anything about the form values, or the way the form values are interpreted, then you need an in-between step where you use the POSTed number to retrieve the name from the database.

You can use that POSTed name id to create a new result row that that has 1 where the name matches the id, or 0 where it doesn’t.

wow!!


<option value="<?php echo $row['client_name'] ?>" <?php echo($row['client_name']==$_POST['name'])?'selected':''?>>


if(isset($_POST['name']))
$client=$_POST['name'];
{
$client_id=mysql_query("SELECT * FROM client_master WHERE client_name='".$client."'");
while($client_query=mysql_fetch_array($client_id))
{
$id=$client_query['client_id'];
}
$query=mysql_query("SELECT * FROM quotation WHERE quo_status=0 AND client_id='".$id."'");


this worked like miracle.thx a lot!!:slight_smile:

That is a lot of database requests though.

It’s possible to reduce it down to just one request, but my brain isn’t up to it as late as it is here for me.

The good people in the SQL forum might be able to help best there though, when it comes to reworking SQL queries.

problem is, i am unable to insert the client_id into the database.what can i do for that??

I’ll leave for someone else, as it’s late and my brain is saying things like “no more!” and “need omega 3 fish oil!”

o yeh.its just 4.10 pm here.so i am in my full swing.i will keep trying some solution for this.n hope SQL gurus will help me out as always :slight_smile: good nyt n sweet dreams n yeh thx again :slight_smile: