我有一个函数可以向 php 网站发送 post 请求。通过简单地改变变量的大小写,我得到了两种不同的行为。有问题的变量是“action”变量,并且被设置为“deleteIndexMain”或“deleteIndexmain”如果action变量设置为“deleteIndexmain”,我会弹出显示php返回的html的窗口。如果我将变量设置为“deleteIndexMain”,则不会弹出窗口。 (这意味着它似乎是一个 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
不返回 bool 值,它返回一个数字,指示第一个字符串是否小于、等于或大于第二个字符串。所以你必须使用 == 0
来测试字符串是否相等。
或者您可以在正常比较之前使用 strtolower()
将所有内容转换为单个大小写。
if(isset($_POST) && strtolower($_POST['action']) == "deleteindexmain")
关于javascript - AJAX Post 的奇怪 Javascript/PHP 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37232301/