467,885 Members | 1,256 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,885 developers. It's quick & easy.

how do display drop down list selected value

Question: How do I display/echo the values in page the selected values from drop down lists in page 2? I think this is the line that I have to add some code to make the values SELECTED. Any help is appreciated.
[php]echo "<option value=\"$name\">$name</option> \n" ;

page1.php
<?
echo "<form name=sel>\n";
echo "VENDOR : <font id=vendor><select>\n";
echo "<option value='0'>============</option> \n" ;
echo "</select></font>\n";
echo "RECRUITER : <font id=vendorrecruiter ><select>\n";
echo "<option value='0'>=== none ===</option> \n" ;
echo "</select></font>\n";
?>

page2.php

if ($data=='vendor') { // first dropdown
echo "<select name='vendor' onChange=\"dochange('vendorrecruiter', this.value)\">\n";
echo "<option value='0'>==== Choose vendor ====</option>\n";
$result=mysql_db_query($dbname,"select `clientid`, `companyname` from rtracker_vendor group by `companyname`");
while(list($id, $name)=mysql_fetch_array($result)){
echo "<option value=\"$name\">$name</option> \n" ;

}
} else if ($data=='vendorrecruiter') { // second dropdown
echo "<select name='vendorrecruiter' >\n";
echo "<option value='0'>====Choose recruiter ====</option>\n";
$result=mysql_db_query($dbname,"SELECT `clientid`, `name` FROM rtracker_vendor where `companyname` like '%$val%' ORDER BY `name` ");
while(list($id, $name)=mysql_fetch_array($result)){
echo "<option value=\"$id\" >$name</option> \n" ;
}
}
echo "</select>\n";[/php]

Please enclose any code within the proper code tags. See the Posting Guidelines on how to do that. - moderator
Mar 2 '08 #1
  • viewed: 14745
Share:
8 Replies
ronverdonk
Expert 4TB
Welcome to The Scripts!

Since you have not posted all the code, it is a bit difficult to see what drop down values you want to mark. So let's take the code at line 20, select name ='vendor' and the option value from the db to be displayed is '$name'. Assuming you have POSTed the drop-downs using a submit of the form, then the code in that part would be something like:[php]echo "<option value='$name'";
if ($_POST['vendor'] == $name)
echo " selected='selected' ";
echo ">$name</option>";[/php]If that is not what you are looking for, then show all code involved (and within the appropriate code tags this time) and we can see what is going on.

Ronald
Mar 2 '08 #2
@ronverdonk:
Ahh,, It was selected='selected' and not checked='checked'. :p

Thanks for that. ;)

I can use that in my script. :)

Greetings,
- Johan aka Josso
Mar 2 '08 #3
ronverdonk
Expert 4TB
Hope you can continue now without problems. See you next time.

Ronald
Mar 2 '08 #4
hsriat
Expert 1GB
@ronverdonk:
Ahh,, It was selected='selected' and not checked='checked'. :p

Thanks for that. ;)

I can use that in my script. :)

Greetings,
- Johan aka Josso
You can also use only selected instead of selected='selected', and similarly in case of checked='checked'
Mar 2 '08 #5
Welcome to The Scripts!

Since you have not posted all the code, it is a bit difficult to see what drop down values you want to mark. So let's take the code at line 20, select name ='vendor' and the option value from the db to be displayed is '$name'. Assuming you have POSTed the drop-downs using a submit of the form, then the code in that part would be something like:[php]echo "<option value='$name'";
if ($_POST['vendor'] == $name)
echo " selected='selected' ";
echo ">$name</option>";[/php]If that is not what you are looking for, then show all code involved (and within the appropriate code tags this time) and we can see what is going on.

Ronald
Thanks Ronald. Yes, what I am trying to do is this in more detail:

I am using PHP/MYSQL/AJAX for inserting to database using the form which has dependent drop down list. The problem here is I am not able to see the values of variables that I need for inserting to the DB in the MainPage.php I have used the POST part of the code also that you had suggested. I think I am making a mistake in the form but not sure how... Please let me know if this is clear?

The main Page has

MainPage.php: This is the form in the Main Page where I am trying to get the values to be assigned to two variables $vendor and $vendorrecruiter. As long as i am able to echo/print this values in this mainpage.php the code wil work.[php]
[php]?include 'inc/ajax.js'?>

<FORM action="job_orders_test.php" method=post>
<TR>
<td width="4%" ><b><font face="Arial Narrow" size="2">&nbsp;ID</font></b></td>
<td width="13%" > <INPUT name=jobid value="<?= $jobid ?>" size=9></td>
<td width="6%" ><b><font face="Arial Narrow" size="2">Title</font></b></td>
<td align="justify" width="38%" ><input name=jobtitle value="<?= $jobtitle ?>" size="32" ></td>
<td width="6%" ><b><font face="Arial Narrow" size="2">Rate</font></b></td>
<td width="12%" ><input size=9 name=rate value="<?= $rate ?>"></td>
<td width="12%" ><b><font face="Arial Narrow" size="2">Vendor Rate</font></b></td>
<td width="10%" ><input size=9 name=vendorrate value="<?= $vendorrate ?>"></td>
</TR>
<TR>
<td align="center" width="50%" colSpan="8" height="24"><b>
<font face="Arial Narrow" size="2">
<?
echo "<form name=sel>\n";
echo "VENDOR : <font id=vendor face=\'Arial Narrow'\ size=2><select>\n";
echo "<option value='0'>============</option> \n" ;
echo "</select></font>\n";
echo "RECRUITER : <font id=vendorrecruiter face=\'Arial Narrow'\ size=2><select>\n";
echo "<option value='0'>=== none ===</option> \n" ;
echo "</select></font>\n";
$vendor=$_GET['vendor'];
echo " We have - $vendor & - $vendorrecruiter ";
?>

</font>
</td>
</TR>
<TR>
<td align="middle" width="100%" colSpan="8" height="24"><b>
<font face="Arial Narrow" size="2"> POC </font></b>&nbsp;&nbsp;
<SELECT style="WIDTH: 238px" size="1" name=poc >
<OPTION value="RECRUITER" selected>RECRUITER</OPTION>

</SELECT>
<font face="Arial" size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;</font>
</td>
</TR>
</TBODY></TABLE></DIV>
<TR>
<TD width="100%" height=12 bgColor="#ff9966"> <b><font face="Arial Narrow" size="2">
<P align="center">
<INPUT TYPE="hidden" name=tried value="yes">
<INPUT name=B1 type=submit value=Submit> <INPUT name=B2 type=reset value=Clear> </P>
</FORM>
_______________________________
ajax.js

<script language=Javascript>
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
};

function dochange(src, val) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
document.getElementById(src).innerHTML=req.respons eText; //retuen value
}
}
};
req.open("GET", "vendor.php?data="+src+"&val="+val); //make connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
req.send(null); //send value
}

window.onLoad=dochange('vendor', -1); // value in first dropdown
</script>
__________________________________________________ _
vendor.php

<?
//set IE read from page only not read from cache
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");

header("content-type: application/x-javascript; charset=tis-620");

$data=$_GET['data'];
$val=$_GET['val'];

include 'inc/dbcon.php';

//set database

mysql_pconnect($dbhost,$dbuser,$dbpass) or die ("Unable to connect to MySQL server");


if ($data=='vendor') { // first dropdown
echo "<select name='vendor' onChange=\"dochange('vendorrecruiter', this.value)\">\n";
echo "<option value='0'>==== Choose vendor ====</option>\n";
$result=mysql_db_query($dbname,"select `clientid`, `companyname` from rtracker_vendor group by `companyname`");
while(list($id, $name)=mysql_fetch_array($result)){
echo "<option value='$name'";
if ($_POST['vendor'] ==$name) echo " selected ='selected'";
echo" >$name</option> \n" ;

}
} else if ($data=='vendorrecruiter') { // second dropdown
echo "<select name='vendorrecruiter' >\n";
echo "<option value='0'>====Choose recruiter ====</option>\n";
$result=mysql_db_query($dbname,"SELECT `clientid`, `name` FROM rtracker_vendor where `companyname` like '%$val%' ORDER BY `name` ");
while(list($id, $name)=mysql_fetch_array($result)){
echo "<option value='$name'";
if ($_POST['vendor'] ==$name) echo " selected ='selected'";
echo" >$name</option> \n" ;
}
}
echo "</select>\n";
?>[/php]
Mar 3 '08 #6
ronverdonk
Expert 4TB
Since you chose to ignore the request to follow the Posting Guidelines and show your code witin the appropriate code tags, which has been asked before, I will not look any further at your code.

moderator
Mar 3 '08 #7
Since you chose to ignore the request to follow the Posting Guidelines and show your code witin the appropriate code tags, which has been asked before, I will not look any further at your code.

moderator
Sorry I did not see the code tags message while I was posting. Can I repost it? Or how do I edit my post?
Mar 7 '08 #8
ronverdonk
Expert 4TB
warning: Please enclose any code within the proper code tags. See the Posting Guidelines on how to do that.

moderator
Mar 7 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by maflu | last post: by
1 post views Thread by J. Babe | last post: by
4 posts views Thread by nz mark in aus | last post: by
2 posts views Thread by clickon | last post: by
2 posts views Thread by usgog | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.