我有一个函数,可以向php网站发送发布请求。通过简单地改变一个变量的大小写,我得到了两种不同的行为。有问题的变量是"action"变量,并且被设置为"deleteIndexMain"或"deleteIndixmain"。如果操作变量被设置为"deleteIndexMain",我会得到弹出窗口,显示php返回的html。如果我将变量设置为"deleteIndex Main",我将不会弹出窗口。(意味着这似乎是一个javascript问题?
以下是java脚本代码:
function deleteMe(v,r)
{
if(confirm("Are you sure"))
{
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
if(xhttp.readyState == 4 && xhttp.status == 200)
{
alert(xhttp.responseText);
document.getElementById("indexmaintable").deleteRow(r);
}
};
xhttp.open("POST", "includes/control.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("action=deleteIndexMain&file="+v);
}
}
这是php代码:
<?php
//Todo make sure to authenticate!
session_start();
require_once("config.php");
function deleteIndexMain($file)
{
unlink($file);
$query = 'DELETE FROM indexmain WHERE linklocation="'.$file.'"';
$db->query($query);
}
print_r($_POST);
if(isset($_POST) && $_POST['action'] == "deleteIndexMain")
{
echo 'Deleting '.$_POST['file'];
deleteIndexMain($_POST['file']);
}
?>
字符串与==
的比较区分大小写。如果要执行不区分大小写的比较,可以使用strcasecmp()
:
if(isset($_POST) && strcasecmp($_POST['action'], "deleteIndexMain") == 0)
请注意,strcasecmp
不返回布尔值,它返回一个数字,指示第一个字符串是否小于、等于或大于第二个字符串。因此,您必须使用== 0
来测试字符串是否相等。
或者,在正常比较之前,您可以使用strtolower()
将所有内容转换为单个案例。
if(isset($_POST) && strtolower($_POST['action']) == "deleteindexmain")