简体   繁体   English

通过select选项标签在php和javascript之间传递if检查

[英]Passing the if check between php and javascript from a select option tag

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. 我对if语句有疑问,如果未在if语句中继续执行以下功能,则该选项框是否确定值是否为“ Choose Here”。 The $EnrollmentMonth is working fine capturing the value from elsewhere. $EnrollmentMonth运行良好,可以从其他地方获取价值。

<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>";
?>

You are calling document.getElementById() but your select doesn't have an id. 您正在调用document.getElementById(),但是您的选择没有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. 这就是为什么除非您提供选择id =“ add_product”否则不能使用getElementById的原因,否则您可以使用document.getElementsByName('add_product')[0]来获得名称为add_product的第一项。 but i would prefer the ID. 但我更喜欢ID。

your final PHP/HTML should look sth like this 您的最终PHP / HTML应该看起来像这样

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. 请注意,我对字符串使用了单引号样式,因此,如果您使用我的答案,则必须根据需要进行更改。

JavaScript - 一种检查选项的方法 <select>选择了标签 我有一个 select 标签,我想检查是否选择了飞蛾。 &lt;select name="Birth_Month"&gt; &lt;option value="" SELECTED&gt;- Month -&lt;/option&gt; &lt;option value="January"&gt;一月&lt;/option&gt; &lt;option value="Fabruary"&gt;Fabruary&lt;/option&gt; &lt; option value="March"&gt;三月&lt;/option&gt; &lt;option value="April"&gt;四月&lt;/option&gt; &lt;option value="May"&gt;五月&lt;/option&gt; &lt;option value="June"&gt;六月&lt;/option &gt; &lt;option value="July"&gt;七月&lt;/option&gt; &lt;option value="August"&gt;八月&lt;/option&gt; &lt;option value="September"&gt;九月&lt;/option&gt; &lt;option value="October"&gt;十月&lt; /option&gt; &lt;option value="November"&gt;November&lt;/option&gt; &lt;option value="December"&gt;December&lt;/option&gt; &lt;/select&gt; 这样做: if (document.registration_form.Birth_Month.value === ' ') { alert('请填写 select 月份;') } 但是这个 JavaScript 对于某些 select-s 有效,而对于其中一些无效。 Obviously, when the "- Month -" is selected the it returnes "- Month -" insted of "" (empty string).我做错了什么?检查标签选择的最佳方法是什么?</select> - JavaScript - a way check if an option of the <select> tag is selected

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript - 一种检查选项的方法 <select>选择了标签 我有一个 select 标签,我想检查是否选择了飞蛾。 &lt;select name="Birth_Month"&gt; &lt;option value="" SELECTED&gt;- Month -&lt;/option&gt; &lt;option value="January"&gt;一月&lt;/option&gt; &lt;option value="Fabruary"&gt;Fabruary&lt;/option&gt; &lt; option value="March"&gt;三月&lt;/option&gt; &lt;option value="April"&gt;四月&lt;/option&gt; &lt;option value="May"&gt;五月&lt;/option&gt; &lt;option value="June"&gt;六月&lt;/option &gt; &lt;option value="July"&gt;七月&lt;/option&gt; &lt;option value="August"&gt;八月&lt;/option&gt; &lt;option value="September"&gt;九月&lt;/option&gt; &lt;option value="October"&gt;十月&lt; /option&gt; &lt;option value="November"&gt;November&lt;/option&gt; &lt;option value="December"&gt;December&lt;/option&gt; &lt;/select&gt; 这样做: if (document.registration_form.Birth_Month.value === ' ') { alert('请填写 select 月份;') } 但是这个 JavaScript 对于某些 select-s 有效,而对于其中一些无效。 Obviously, when the "- Month -" is selected the it returnes "- Month -" insted of "" (empty string).我做错了什么?检查标签选择的最佳方法是什么?</select> - JavaScript - a way check if an option of the <select> tag is selected 从PHP中的select / option标记中提取值? - Extract value from select/option tag in php? 将标记ID从php传递到javascript - passing tag id from php to javascript 检查选择选项标签中的条件 - check condition in select option tag 从中选择多个选项后触发javascript函数<select>标签 - Trigger a javascript function after select multiple option from <select> tag 选项标签未出现在选择的Javascript中 - Option tag not appearing in select Javascript 选择 <select><option>从使用jQuery,Javascript或PHP的替代选择 - Select <select> <option> from alternate select using jQuery, Javascript, or PHP JavaScript已从选择标签列表中删除一个选项(如果已选择) - JavaScript remove an option from a select tag list if already selected 将 select 选项传递到“a”标签中的 href - Passing select option onto href in an “a” tag 将选择的选项从选择框传递到另一个php文件 - Passing selected option from select box to a different php file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM