Using Value from Dynamically Created drop-down list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrtr33
    New Member
    • Jul 2007
    • 3

    #1

    Using Value from Dynamically Created drop-down list

    I have a class table where I want to access the names of courses and the description of each course. The names of the courses go in a drop down list. Based on the user selection, the description shows up as text beneath the drop down menu.

    I have the following code:

    [code=php]$queryReq="SELE CT name, description FROM class";
    $resultReq = @mysql_query ($queryReq);
    echo "<br><form action=index.ph p name='trouble' method='POST'>< select name='class' >";
    while($nt=mysql _fetch_array($r esultReq)){echo "<option value=$nt[description]>$nt[name]</option>";}
    echo "</select><submit type='button' name='go'/>";
    $class = $_POST['class'];
    echo "$class</form>";
    echo "<br>";[/code]

    [Please use CODE tags when posting source code. Thanks! --pbmods]

    How do I use the value of my option tag to display the user's choice in some fashion beneath it? I've mostly seen only static array options handle this problem.

    I've tried to retrieve the selection using POST, also tried using an onchange function in javascript within the <select> tag.

    It seems like it should be pretty easy to do. Any help would be appreciated.

    mrtr33
  • Purple
    Recognized Expert Contributor
    • May 2007
    • 404

    #2
    Hi mrtr33 and welcome to TSDN

    try this:

    [PHP]<?php
    $queryReq="SELE CT name, description FROM class";
    $resultReq = mysql_query($qu eryReq);
    echo "<br>
    <form action=index.ph p name='trouble' method='POST'>
    <select name='class' >";
    while($nt = mysql_fetch_ass oc($resultReq)) {
    echo "<option value=".$nt['description'].">".$nt['name']."</option>";
    }
    echo "</select>
    <submit type='button' name='go'/>";
    $class = $_POST['class'];
    echo $class."</form><br>";
    ?>[/PHP]

    also take a look at this if you haven't done so already..

    Regards Purple

    Comment

    • mrtr33
      New Member
      • Jul 2007
      • 3

      #3
      Sweet code, I appreciate it.

      But it only returns the first word of the value. It looks like once it hits whitespace, it stops returning the value. The value of each option is a string.

      Is there any way I can display the entire string?

      description is of type longtext in mySQL. Each class description is no more than 1200 characters.

      Mark

      Comment

      • mwasif
        Recognized Expert Contributor
        • Jul 2006
        • 802

        #4
        Your problem will be solved by using quotes around the value
        [PHP]echo "<option value='".$nt['description']."'>".$nt['name']."</option>";[/PHP]

        Comment

        • mrtr33
          New Member
          • Jul 2007
          • 3

          #5
          Thanks, that worked.

          There was also a few things I needed to clean up in the code, for completeness purposes....

          Here is the final product:


          [PHP]$queryReq="SELE CT name, description FROM class";
          $resultReq = mysql_query($qu eryReq);
          echo "<br><form action=index.ph p name='trouble' method='POST'>< select name='class' >";
          while($nt = mysql_fetch_ass oc($resultReq)) {echo "<option value='".$nt['description']."'>".$nt['name']."</option>";}
          echo "</select><input type='submit' value='Go'/><br>";
          $class = $_POST['class'];
          echo $class."</form><br>";[/PHP]

          thanks to everyone for the help...

          Comment

          • satya61229
            New Member
            • Feb 2007
            • 24

            #6
            Originally posted by mrtr33
            I have a class table where I want to access the names of courses and the description of each course. The names of the courses go in a drop down list. Based on the user selection, the description shows up as text beneath the drop down menu.

            I have the following code:

            [code=php]$queryReq="SELE CT name, description FROM class";
            $resultReq = @mysql_query ($queryReq);
            echo "<br><form action=index.ph p name='trouble' method='POST'>< select name='class' >";
            while($nt=mysql _fetch_array($r esultReq)){echo "<option value=$nt[description]>$nt[name]</option>";}
            echo "</select><submit type='button' name='go'/>";
            $class = $_POST['class'];
            echo "$class</form>";
            echo "<br>";[/code]

            [Please use CODE tags when posting source code. Thanks! --pbmods]

            How do I use the value of my option tag to display the user's choice in some fashion beneath it? I've mostly seen only static array options handle this problem.

            I've tried to retrieve the selection using POST, also tried using an onchange function in javascript within the <select> tag.

            It seems like it should be pretty easy to do. Any help would be appreciated.

            mrtr33
            Just check here: http://satya61229.blogspot.com/2007/...t-list-by.html

            and this may also be useful for you: http://satya61229.blogspot.com/2007/...from-list.html

            Comment

            Working...