#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Issues with getting value of currently selected option in a dropdown


    Ok So I have 2 tables, one with Assignment names (Homework1, Homework2, etc) and a table with Student grades for each assignment (So mary might have entries for HW1, HW2 and Joe would also have entries for HW1 and HW2)

    Currently I'm using php to grab the list of homeworks from the first table and shove them into a dropdown, no problem there.

    But what I want to do is then grab that selected dropdown value, and use it in an SQL query to narrow grade results to JUST the selected Assignment

    PHP Code:
    <?
                $HWname 
    "SELECT hwname FROM Homeworks ORDER BY hwname ASC";
                
    $HWnameResult mysql_query($HWname) or die(mysql_error());
                
    $table "SELECT Student,NumCorrect,NumTotal,Score FROM StudentGrades WHERE Class='{$_POST[hwname]}' ORDER BY Student ASC";
                
    $tableResult mysql_query($table) or die(mysql_error());
                
                
    //Dropdown for Assignment select 
                
    $dropdown "<select name='Homeworks'";
                while(
    $row mysql_fetch_assoc($HWnameResult)) 
                {
                  
    $dropdown .= "\r\n<option value='{$row['hwname']}'>{$row['hwname']}</option>";
                }
                
    $dropdown .= "\r\n</select>";
                echo 
    $dropdown;
                
    ?>
                
                <table id="ViewGradesTable" class="table table-bordered table-condensed table-hover">
                
                <thead class="table-head">
                    <?
                    
    echo "
                    <tr>
                    <th>Student Name</th>
                    <th>Correct</th>
                    <th>Total</th>
                    <th>Score</th>
                    </tr>"
    ?>

                </thead>

                <?
                
    //Table of Student HW grades for selected Assignment
                
    while($row mysql_fetch_array($tableResult))
                  {
                  echo 
    "<tr>";
                  echo 
    "<td> </td>";
                  echo 
    "<td> </td>";
                  echo 
    "<td> </td>";
                  echo 
    "<td> </td>";
                  echo 
    "</tr>";
                  }
                echo 
    "</table>";
            
                
    mysql_close($con);
                
    ?>
    Left the connection to the database out as it's not part of the problem
  2. #2
  3. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,889
    Rep Power
    6444
    Code:
     $dropdown = "<select name='Homeworks'>";

    Comments on this post

    • ManiacDan agrees : Good eye
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    I saw that shortly after I posted this, fixed
    still having an issue
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,002
    Rep Power
    9398
    And where's the code where you try to grab the value?
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,866
    Rep Power
    6351
    Is there more to this application? For instance, is there a properly-formed <form> tag and a properly formed <input type="submit"> button? If you have those, does the form's action have code which retrieves the value of your drop-down and draws the appropriate page?

    Your "issue" seems to be that you're not done writing this application.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Here's my updated code, I added a button to submit, and I use the SQL query that grabs the HW name as the action.

    PHP Code:
    <?

                $HWname 
    "SELECT hwname FROM Homeworks ORDER BY hwname ASC";
                
    $HWnameResult mysql_query($HWname) or die(mysql_error());
                
    $table "SELECT Student,NumCorrect,NumTotal,Score FROM StudentGrades WHERE Class=???? ORDER BY Student ASC";
                
    ?>
                
                <form method="post" action="<? $tableResult mysql_query($table) or die(mysql_error()); ?>">
                <?
                
    echo $_POST['Homeworks'];

                
    //Dropdown for Assignment select 
                
    $dropdown "<select name='Homeworks' id='Homeworks'>";
                while(
    $row mysql_fetch_assoc($HWnameResult)) 
                {
                  
    $dropdown .= "\r\n<option value='{$row['hwname']}'>{$row['hwname']}</option>";
                }
                
    $dropdown .= "\r\n</select>";
                echo 
    $dropdown;
                
    ?>
                <input type="submit" id="submitbutton" name="Go" value="Check" />
                </form>
                
                <!--Table of Student HW grades for selected Assignment-->
                <table id="ViewGradesTable" class="table table-bordered table-condensed table-hover">
                <thead class="table-head">
                    <?
                    
    echo "
                    <tr>
                    <th>Student Name</th>
                    <th>Correct</th>
                    <th>Total</th>
                    <th>Score</th>
                    </tr>"
    ?>
                </thead>

                <?
                
    while($row mysql_fetch_array($tableResult))
                  {
                  echo 
    "<tr>";
                  echo 
    "<td>{$row['Student']} </td>";
                  echo 
    "<td>{$row['NumCorrect']} </td>";
                  echo 
    "<td>{$row['NumTotal']}</td>";
                  echo 
    "<td>{$row['Score']}</td>";
                  echo 
    "</tr>";
                  }
                echo 
    "</table>";
            
                
    mysql_close($con);
                
    ?>
    My issue right now is in the $table variable, I don't know what to put after Class= so that it grabs the submitted value of the dropdown box and uses it in the query
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    I did the "echo $_POST['Homeworks']; " to make sure that it's grabbing info correctly and it is. If I select Homework1 and hit Check, "Homework1" pops up on the page and vice versa for anything else I select. I just can't figure out how to take that text and throw it into the second query
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,866
    Rep Power
    6351
    PHP Code:
                $table "SELECT Student,NumCorrect,NumTotal,Score FROM StudentGrades WHERE Class='" mysql_real_escape_string($_POST['Homeworks']) . "' ORDER BY Student ASC"
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo