[英]Passing the if check between php and javascript from a select option tag
我對if語句有疑問,如果未在if語句中繼續執行以下功能,則該選項框是否確定值是否為“ Choose Here”。 $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>";
?>
您正在調用document.getElementById(),但是您的選擇沒有ID。 這就是為什么除非您提供選擇id =“ add_product”否則不能使用getElementById的原因,否則您可以使用document.getElementsByName('add_product')[0]來獲得名稱為add_product的第一項。 但我更喜歡ID。
您的最終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>';
請注意,我對字符串使用了單引號樣式,因此,如果您使用我的答案,則必須根據需要進行更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.