0

I have a problem with a if statement to determine whether the option box has the value "Choose Here", if it's not proceed to the following function inside the if statement. The $EnrollmentMonth is working fine capturing the value from elsewhere.

<script>
    function promptEnrollmentMonth () {
        if (document.getElementById("add_product").value !== "Choose here") {
            var month = prompt("Is this the correct month for this course/product?", "<?php echo $EnrollmentMonth; ?>");
            if (month != null) {
                window.location.href = "studentupdate.php?id=<?php echo $StudentID?>" + "&enrollmentmonth=" + month;
                <?php
                    $EnrollmentMonth = $_GET['enrollmentmonth'];
                    $sql3 = "INSERT into enrollment (StudentID, ProductID, EnrollmentMonth) VALUES ($StudentID, $select1, $EnrollmentMonth)";
                    mysql_query($sql3);
                ?>
                // Insert result if the prompt box does not return null.
            }
        } else {
            document.write("Stuck here!");
        }
    }
</script>

<button type="submit" onclick="promptEnrollmentMonth();">Update</button>

<?php
    $sql2          = 'SELECT * FROM product ORDER BY ProductID ASC';
    $sql2          = "SELECT * FROM product WHERE ProductID NOT IN (SELECT ProductID from enrollment WHERE StudentID = '$StudentID') ORDER BY ProductID ASC";
    $result_select = mysql_query($sql2);
    $rows          = array();
    while ($row = mysql_fetch_array($result_select)) {
        $rows[] = $row;
    }
    echo "<div class=\"spanstyle\">Add course/product:<select name='add_product'>";
    echo "<option value='Choose here' selected>Choose here</option>";
    foreach ($rows as $row) {
        echo "<option value='" . $row['ProductID'] . "'>" . $row['ProductName'] . "</option>";
    }
    echo "</select></div>";
?>
Joshua
  • 5,032
  • 2
  • 29
  • 45
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) that has been [removed](http://php.net/manual/en/mysql.php) from PHP. You should select a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jun 21 '17 at 08:02

1 Answers1

0

You are calling document.getElementById() but your select doesn't have an id. Thats why using getElementById will not work unless you give your select the id="add_product" alternatively you could use document.getElementsByName('add_product')[0] to get your very first item with the name add_product. but i would prefer the ID.

your final PHP/HTML should look sth like this

echo '<div class="spanstyle">Add course/product:<select name="add_product" id="add_product">';
echo '<option value="Choose here" selected>Choose here</option>';

please note, that i used single-quote style for the strings, so if you use my answer you have to change it to your needs.

Jaysn
  • 36
  • 4
  • Adding in `id="add_product"` to the select tag gave me the same result. it does not pass the if statement. I changed the quote/double quote as per your suggestion. No change. – Anonymous Vagrant Jun 21 '17 at 08:14
  • did you try to loose your comparison? ie. `document.getElementById('add_product').value != "Choose here"`? Also i would try to move away from comparing values to check if the first options is selected by using the select-node's property "selectedIndex" and check if its equal 0 – Jaysn Jun 21 '17 at 08:50