[英]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. 请注意,我对字符串使用了单引号样式,因此,如果您使用我的答案,则必须根据需要进行更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.