how do display drop down list selected value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stekk
    New Member
    • Mar 2008
    • 3

    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><sele ct>\n";
    echo "<option value='0'>===== =======</option> \n" ;
    echo "</select></font>\n";
    echo "RECRUITER : <font id=vendorrecrui ter ><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=\"doch ange('vendorrec ruiter', this.value)\">\ n";
    echo "<option value='0'>==== Choose vendor ====</option>\n";
    $result=mysql_d b_query($dbname ,"select `clientid`, `companyname` from rtracker_vendor group by `companyname`") ;
    while(list($id, $name)=mysql_fe tch_array($resu lt)){
    echo "<option value=\"$name\" >$name</option> \n" ;

    }
    } else if ($data=='vendor recruiter') { // second dropdown
    echo "<select name='vendorrec ruiter' >\n";
    echo "<option value='0'>====C hoose recruiter ====</option>\n";
    $result=mysql_d b_query($dbname ,"SELECT `clientid`, `name` FROM rtracker_vendor where `companyname` like '%$val%' ORDER BY `name` ");
    while(list($id, $name)=mysql_fe tch_array($resu lt)){
    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
    Last edited by ronverdonk; Mar 2 '08, 10:55 AM. Reason: code within proper code tags
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    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='selec ted' ";
    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

    Comment

    • Josso
      New Member
      • Mar 2008
      • 8

      #3
      @ronverdonk:
      Ahh,, It was selected='selec ted' and not checked='checke d'. :p

      Thanks for that. ;)

      I can use that in my script. :)

      Greetings,
      - Johan aka Josso

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Hope you can continue now without problems. See you next time.

        Ronald

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Originally posted by Josso
          @ronverdonk:
          Ahh,, It was selected='selec ted' and not checked='checke d'. :p

          Thanks for that. ;)

          I can use that in my script. :)

          Greetings,
          - Johan aka Josso
          You can also use only selected instead of selected='selec ted', and similarly in case of checked='checke d'

          Comment

          • stekk
            New Member
            • Mar 2008
            • 3

            #6
            Originally posted by ronverdonk
            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='selec ted' ";
            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 $vendorrecruite r. 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_ord ers_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=vendorrecrui ter 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 & - $vendorrecruite r ";
            ?>

            </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="RECRUITE R" selected>RECRUI TER</OPTION>

            </SELECT>
            <font face="Arial" size="2">&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;</font>
            </td>
            </TR>
            </TBODY></TABLE></DIV>
            <TR>
            <TD width="100%" height=12 bgColor="#ff996 6"> <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=Javasc ript>
            function Inint_AJAX() {
            try { return new ActiveXObject(" Msxml2.XMLHTTP" ); } catch(e) {} //IE
            try { return new ActiveXObject(" Microsoft.XMLHT TP"); } catch(e) {} //IE
            try { return new XMLHttpRequest( ); } catch(e) {} //Native Javascript
            alert("XMLHttpR equest not supported");
            return null;
            };

            function dochange(src, val) {
            var req = Inint_AJAX();
            req.onreadystat echange = function () {
            if (req.readyState ==4) {
            if (req.status==20 0) {
            document.getEle mentById(src).i nnerHTML=req.re sponseText; //retuen value
            }
            }
            };
            req.open("GET", "vendor.php?dat a="+src+"&val=" +val); //make connection
            req.setRequestH eader("Content-Type", "applicatio n/x-www-form-urlencoded;char set=iso-8859-1"); // set Header
            req.send(null); //send value
            }

            window.onLoad=d ochange('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=\"doch ange('vendorrec ruiter', this.value)\">\ n";
            echo "<option value='0'>==== Choose vendor ====</option>\n";
            $result=mysql_d b_query($dbname ,"select `clientid`, `companyname` from rtracker_vendor group by `companyname`") ;
            while(list($id, $name)=mysql_fe tch_array($resu lt)){
            echo "<option value='$name'";
            if ($_POST['vendor'] ==$name) echo " selected ='selected'";
            echo" >$name</option> \n" ;

            }
            } else if ($data=='vendor recruiter') { // second dropdown
            echo "<select name='vendorrec ruiter' >\n";
            echo "<option value='0'>====C hoose recruiter ====</option>\n";
            $result=mysql_d b_query($dbname ,"SELECT `clientid`, `name` FROM rtracker_vendor where `companyname` like '%$val%' ORDER BY `name` ");
            while(list($id, $name)=mysql_fe tch_array($resu lt)){
            echo "<option value='$name'";
            if ($_POST['vendor'] ==$name) echo " selected ='selected'";
            echo" >$name</option> \n" ;
            }
            }
            echo "</select>\n";
            ?>[/php]
            Last edited by ronverdonk; Mar 7 '08, 12:49 AM. Reason: cote tags

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #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

              Comment

              • stekk
                New Member
                • Mar 2008
                • 3

                #8
                Originally posted by ronverdonk
                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?

                Comment

                • ronverdonk
                  Recognized Expert Specialist
                  • Jul 2006
                  • 4259

                  #9
                  warning: Please enclose any code within the proper code tags. See the Posting Guidelines on how to do that.

                  moderator

                  Comment

                  Working...