我的代码中有一部分带有一些似乎没有正常工作的elseif条件。我已经建立了一个条件下拉表格,到目前为止大部分都在工作,除非它到达其他几个地方。
我正在使用JS通过GET将下拉选项名称发送到我的PHP脚本,这似乎很奇怪。对于大多数表单,它从MySQL查询中提取下一个下拉列表的选项,但是对于第一个drop,我刚刚使用了一些elseifs来使它不那么复杂。您可以查看实时剧本 here. 以下是给我带来麻烦的部分(这是最后两个):
if (isset($_GET['setpoint'])) {
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
}
} elseif (isset($_GET['power'])) {
echo "<option>Please Choose Setpoint Options</option>";
while ($row = mysql_fetch_array($result)) {
$row{'stp'} = ucfirst($row{'stp'}); //capitalizes the first letter; necessary?
echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
}
} elseif (isset($_GET['source'])) {
echo "<option>Please Choose Input Range</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row{'sir'} . "'>" . $row{'sir'} . "</option>";
}
} elseif (isset($_GET['type']) && $_GET['type'] = "Digital") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='RS232C'>RS232C</option><option value='RS422'>RS422</option><option value='RS485'>RS485</option><option value='current loop'>current loop</option>";
} elseif (isset($_GET['type']) && $_GET['type'] = "Analog") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='DC current'>DC Current</option><option value='DC voltage'>DC Voltage</option><option value='AC current'>AC Current</option><option value='AC voltage'>AC Voltage</option><option value='process'>Process</option><option value='thermocouple'>Thermocouple</option><option value='RDT'>rdt</option>";
}
第一个下拉列表会将$_GET['type']
设置为'模拟'或'数字',但无论选择哪个选项$_GET['type']
,都会运行第一个检查$_GET['type']
的elseif已设定。如果$_GET['type'] = 'Analog'
那么它应该返回最后一个elseif而不是倒数第二个。
这是大多数参考脚本:
//prevents injections
//any order
isset($_GET['type'])?$type = urldecode($_GET['type']):"";
//$type = mysql_real_escape_string(urldecode($_GET['type']));
isset($_GET['source'])?$source = mysql_real_escape_string(urldecode($_GET['source'])):"";
isset($_GET['range'])?$power = mysql_real_escape_string(urldecode($_GET['range'])):"";
isset($_GET['setpoint'])?$setpoint = mysql_real_escape_string(urldecode($_GET['setpoint'])):"";
//forms the query depending on what data is recieved through GET
//first option on the bottom; last option on the top to avoid conflicts
if (isset($_GET['setpoint'])) {
$query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' AND stp='$setpoint' ORDER BY model";
} elseif (isset($_GET['power'])) {
$query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' ORDER BY model";
} elseif (isset($_GET['range'])) {
$query = "SELECT DISTINCT pso FROM meters WHERE sio='$range' ORDER BY model";
} elseif (isset($_GET['source'])) {
$query = "SELECT DISTINCT sir FROM meters WHERE sio LIKE '%$source%' ORDER BY sir";
}
//creates a result array from query results
isset($query)?$result = mysql_query($query):"";
//outputs dropdown options dependent on what GET variables are set
//first option on the bottom; last option on the top to avoid conflicts
if (isset($_GET['setpoint'])) {
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
}
} elseif (isset($_GET['power'])) {
echo "<option>Please Choose Setpoint Options</option>";
while ($row = mysql_fetch_array($result)) {
$row{'stp'} = ucfirst($row{'stp'}); //capitalizes the first letter; necessary?
echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
}
} elseif (isset($_GET['source'])) {
echo "<option>Please Choose Input Range</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row{'sir'} . "'>" . $row{'sir'} . "</option>";
}
} elseif (isset($_GET['type']) && $_GET['type'] = "Digital") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='RS232C'>RS232C</option><option value='RS422'>RS422</option><option value='RS485'>RS485</option><option value='current loop'>current loop</option>";
} elseif (isset($_GET['type']) && $_GET['type'] = "Analog") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='DC current'>DC Current</option><option value='DC voltage'>DC Voltage</option><option value='AC current'>AC Current</option><option value='AC voltage'>AC Voltage</option><option value='process'>Process</option><option value='thermocouple'>Thermocouple</option><option value='RDT'>rdt</option>";
同样,您可以查看实时脚本here.
答案 0 :(得分:3)
$_GET['type'] = "Digital"
是作业,而不是支票。
您需要的是
$_GET['type'] == "Digital"
与$_GET['type'] = "Analog"
应该是
$_GET['type'] == "Analog"
此外,与Darkbee一样,请使用PDO
或MySQLi
答案 1 :(得分:0)
当我想要比较运算符equals
时,你正在分配$ _GET ['type']:
$_GET['type'] = "Digital"
变为
$_GET['type'] == "Digital"
答案 2 :(得分:0)
尝试
elseif (isset($_GET['type']) && $_GET['type'] == "Digital")
和
elseif (isset($_GET['type']) && $_GET['type'] == "Analog")
两个等号是一个等号是一个赋值(总是会返回真值)