Forum Moderators: coopster

Message Too Old, No Replies

Select value not inserting

         

zed420

4:21 pm on Feb 18, 2009 (gmt 0)

10+ Year Member



Hi All
I have a small problem with my code I hope someone can help me, the code below works fine apart from one thing that I can NOT seem to figure out. Two dropdown boxes populate from to another, onload it populates the Client Name and when you select one of them it populates the other(site_name) dropdown box with their site names as well as Client ID. When you select a Site Name it also populates the Site ID. The problem is in the database instead of inserting the Name of Client as a value it inserts the ID of it and same goes for the Site Name. I want the Client Name as value in cl_name box and Site Name as value in site_name box.

I'm sorry if this is little confusing. Thanks in advance
Zed

 
<td>Client ID : </td>
<td><input type="text" id="cl_id" name="cl_id" size="5" /></td></tr>
<td>Company Name : </td>
<td>
<SELECT NAME="cl_name" onChange="SelectSite();document.getElementById ('cl_id').value=this.value;" >
<Option value="">Company Name</option>
</select></td></tr>
<td>Site Name</td>
<td>
<SELECT NAME="site_name" id="site_name" onchange="document.getElementById('site_id').value=this.value;" >
<Option value="">Site(s) Name</option>
<?=$optionsid?>
</select>
</td></tr>
<tr><td>Client Site ID : </td>
<td><input type="text" name="site_id" id="site_id" size="5" /></td></tr>


echo "
function fillCategory(){
// this function is used to fill the category list on load

";
$q1=mysql_query("select * from client");
echo mysql_error();
while($nt1=mysql_fetch_array($q1)){
echo "addOption(document.emp_rec.cl_name, '$nt1[client_id]','$nt1[client_name]');";
}// end of while
?>
} // end of JS function

function SelectSite(){

removeAllOptions(document.emp_rec.site_name);
addOption(document.emp_rec.site_name, "", "Site Name", "");

<?
$q2=mysql_query("select distinct(site_id) from client_site");
while($nt2=mysql_fetch_array($q2)){
echo "if(document.emp_rec.cl_name.value == '$nt2[site_id]'){";
$q3=mysql_query("select * from client_site where site_id='$nt2[site_id]'");
while($nt3=mysql_fetch_array($q3)){
echo "addOption(document.emp_rec.site_name,'$nt3[site_id]','$nt3[site_name]');";
} // end of while loop
echo "}"; // end of JS if condition

}
?>
}
function removeAllOptions(selectbox)
{
var i;
for(i=selectbox.options.length-1;i>=0;i--)
{
//selectbox.options.remove(i);
selectbox.remove(i);
}
}
function addOption(selectbox, value, text )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;

selectbox.options.add(optn);
}

zed420

6:28 pm on Feb 19, 2009 (gmt 0)

10+ Year Member



Anyone ?

coopster

8:09 pm on Feb 19, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You are assigning them in that order right here, id is the second argument and name is the third ...

echo "addOption(document.emp_rec.cl_name, '$nt1[client_id]','$nt1[client_name]');";

... and then look how you are assigning the parameters in the JavaScript function, value is the second argument, which is the id:

function addOption(selectbox, value, text ) 
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}

zed420

12:05 am on Feb 20, 2009 (gmt 0)

10+ Year Member



Thanks for your reply, that doesn't make any difference.
Zed

coopster

12:12 am on Feb 20, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No?

The problem is in the database instead of inserting the Name of Client as a value it inserts the ID of it and same goes for the Site Name.

Whatever is in the value of an option element is what will be submitted in the $_POST index for that element name. In this case, it is going to be the ID.

It looks to me like you have your 2nd and 3rd arguments reversed in your JavaScript function.