gpt4 book ai didi

php - Javascript/AJAX 在除 IE 之外的所有环境中都可以正常工作

转载 作者:行者123 更新时间:2023-12-02 19:10:29 24 4
gpt4 key购买 nike

我有一个简单的程序,它在 Firefox 和 Chrome 中 100% 运行,但在 Internet Explorer(版本 9)中不起作用。

从列出当天所有预定约会的页面调用弹出窗口。这是该弹出窗口的代码:

<?php

$name = $_GET['name'];
#$type = $_GET['type'];
$type = "Regular";
$id = $_GET['id'];

?>

<!doctype html>
<html>
<head>
<title><?php echo $name; ?></title>
<style type="text/css">
body {
font-family: Georgia;
}
.col1 {
float: left;
padding-right: .75em;
}
.col1 div { height: 1.5em; }
.col2 div { height: 1.5em; }
.col2 {
float: left;
padding-right: .5em;
}
.notes {
clear: both;
}
</style>
<script type="text/javascript">
function saveClose() {
var xmlhttp;
var getstring = "";
var d = document;

var start_hour = d.getElementsByName('start_hour')[0].value;
var start_min = d.getElementsByName('start_min')[0].value;
var start_am = d.getElementsByName('start_am')[0].value;

var end_hour = d.getElementsByName('end_hour')[0].value;
var end_min = d.getElementsByName('end_min')[0].value;
var end_am = d.getElementsByName('end_am')[0].value;

var no_show = d.getElementsByName('no_show')[0].checked;

var notes = d.getElementsByName('notes')[0].value;

getstring += "?type=<?php echo $type ?>";
getstring += "&id=<?php echo $id ?>";
getstring += "&s_hour="+start_hour;
getstring += "&s_min=" +start_min;
getstring += "&s_am=" +start_am;
getstring += "&e_hour="+end_hour;
getstring += "&e_min=" +end_min;
getstring += "&e_am=" +end_am;
getstring += "&ns=" +no_show;
getstring += "&notes=" +notes;

if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari;
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5;
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
var reply = xmlhttp.responseText;
if (reply == "Successful!") {
setTimeout('self.close()', 1000);
} else {
document.getElementById('resultsContent').innerHTML = xmlhttp.responseText;
}
} else {
document.getElementById('resultsContent').innerHTML = "Saving...";
}
}

xmlhttp.open("GET", "saveClose.php"+getstring, true);
xmlhttp.send();
}
</script>
</head>
<body>
<div><h2><?php echo $name; ?>'s Consult</h2></div>
<div class="col1">
<div>Consult Start Time:</div>
<div>Consult End Time:</div>
<div>No Show? :</div>
</div>
<div class="col2">
<div>
<select name="start_hour">
<option></option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select>
:
<select name="start_min">
<option></option>
<option>00</option>
<option>05</option>
<option>10</option>
<option>15</option>
<option>20</option>
<option>25</option>
<option>30</option>
<option>35</option>
<option>40</option>
<option>45</option>
<option>50</option>
<option>55</option>
</select>
-
<select name="start_am">
<option></option>
<option>AM</option>
<option>PM</option>
</select>
</div>
<div>
<select name="end_hour">
<option></option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select>
:
<select name="end_min">
<option></option>
<option>00</option>
<option>05</option>
<option>10</option>
<option>15</option>
<option>20</option>
<option>25</option>
<option>30</option>
<option>35</option>
<option>40</option>
<option>45</option>
<option>50</option>
<option>55</option>
</select>
-
<select name="end_am">
<option></option>
<option>AM</option>
<option>PM</option>
</select>
</div>
<div><input type="checkbox" name="no_show" /></div>
</div>
<div class="notes">Notes:</div>
<div class="notes"><textarea name="notes" rows="5" cols="53"></textarea></div>
<div class="notes"><button type="button" onClick="saveClose();">Save & Close</button><span id="resultsContent" style="padding-left:1em"></span></div>
</body>
</html>

AJAX 调用运行此 PHP 脚本:

<?php

$type = $_GET['type'];
$id = $_GET['id'];
$s_hour = $_GET['s_hour'];
$s_min = $_GET['s_min'];
$s_am = $_GET['s_am'];
$e_hour = $_GET['e_hour'];
$e_min = $_GET['e_min'];
$e_am = $_GET['e_am'];
$ns = $_GET['ns'];
$ns = (($ns == "true") ? "1" : "0");
$notes = $_GET['notes'];


switch (true) {
case empty($type):
echo "\$type is broken!";
break;
case empty($s_hour):
echo "\$s_hour is broken!";
break;
case empty($s_min):
echo "\$s_min is broken!";
break;
case empty($s_am):
echo "\$s_am is broken!";
break;
case empty($e_hour):
echo "\$e_hour is broken!";
break;
case empty($e_min):
echo "\$e_min is broken!";
break;
case empty($e_am):
echo "\$e_am is broken!";
break;
case (!isset($ns)):
echo "\$ns is broken!";
break;
case (!isset($notes)):
echo "\$notes is broken!";
break;

default:
// SALES APPTS.
# ...
# ...
# ...

// OTHER APPTS.
# ...
# ...
# ...

echo "Successful!";
break;
}


?>

正如预期的那样,Firefox 和 Chrome 在每个 case 语句上都会停止,直到选择开始时间和结束时间。工作完美。

但是,无论如何,IE 每次都会在 case empty($s_hour): 时停止。我什至无法通过第一个案例陈述来查看其余的案例是否也被破坏。我不明白。

任何帮助将不胜感激,因为此应用程序必须兼容 IE9。

最佳答案

您正在尝试读取选择框的值,但“value”属性可能不会按您的预期填充,因为它试图从所选选项中获取 value 属性,而不是该选项的文本选项。

您应该向 <option> 添加显式值属性像这样的元素:

<option value='12'>12</option>

或显式获取所选选项的文本,如下所示:

var objStHr = document.getElementsByName('start_hour')[0];
var start_hour = objStHr.options[objStHr.value];

(为简洁起见,我使用临时变量来保存对象,因为我们必须引用它两次)

希望有帮助。

关于php - Javascript/AJAX 在除 IE 之外的所有环境中都可以正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13802623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com