473,385 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how do display drop down list selected value

3
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
8 15272
ronverdonk
4,258 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
Josso
8
@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
4,258 Expert 4TB
Hope you can continue now without problems. See you next time.

Ronald
Mar 2 '08 #4
hsriat
1,654 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
stekk
3
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
4,258 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
stekk
3
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
4,258 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

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

Similar topics

1
by: maflu | last post by:
Hello, I am rather a beginner in Javascript and have the following problem. There is a drop down list on my Web page with various values to be selected by the user. Once the user has selected a...
4
by: charliewest | last post by:
I need to set the selected drop down list value at run time. I am aware of the method "SelectIndex" however this works only if you know the precise location of the value within the ListItem...
1
by: J. Babe | last post by:
I am populating a drop down list with a privilege levels from a table called "Privilege" with the fields "PrivilegeID" and "PrivilegeName". In a table named "Users", their is a corresponding column...
4
by: nz mark in aus | last post by:
Hi there I am populating an empty <asp:dropdownlist on the client side using Javascript using the following code Heres my dropdown list <asp:dropdownlist id="ddlDealers"...
2
by: clickon | last post by:
I am using ASP.net 2.0 and trying to take advantage of the updated data editing facilities provided through the SQLDataSource control and the DetailsView control. The data is a record from a...
1
by: tom c | last post by:
In ASP.net is there any way to trigger an event when you change the selected value of a drop down list box?
2
by: usgog | last post by:
I have a <select style="" name="" size="2"> <option value="1">Name 1</option> <option value="1">Name2</option> </select>. It will popup and display as a drop down list with "gray'ed" vertical...
2
by: dimitri | last post by:
There is a drop down list on my Web page with values 1,2,3,4,5 to be selected by the user. Once the user has selected a value, he/she clicks on the submit button. When he/she does that I would like...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.