php - 如何检查提交的元素值是否已经存在?
问题描述
因此,至于作业,我正在编写一个 PHP 熟悉的页面,该页面允许您将订单 ID 提交到 XML 配置,因此它会过滤掉所需的订单。
一切都很好,但我想在配置中已经存在提交的 ID 时提醒用户。
XML 元素
<filter><!-- Copy filter-item and put the order-id in as the value to skip it-->
<filter_item>1142575860</filter_item><filter_item>1142495027</filter_item>
</filter>
if (isset($_POST['btnAddId']))
{
$addID = $_POST['idFilter'];
$xml = simplexml_load_file('Config.xml');
if(empty($addID))
{
echo '<script> alert("Input value is empty");</script>';
}
else if(!is_numeric($addID))
{
echo '<script> alert("input is not numeric");</script>';
}
else if(??)
{
echo '<script> alert("ID already exists in the filter");</script>';
}
else{
$orderFilter = $_POST['idFilter'];
$sxe = new SimpleXMLElement($xml->asXML());
$itemsNode = $sxe->filter;
$itemsNode->addChild('filter_item', $orderFilter);
$sxe->asXML('Config.xml');
echo $LocScript;
}
}
解决方案
您可以使用 XPath 搜索<filter_item>
与他们输入的 ID 具有相同内容的元素...
if ( count($xml->xpath('//filter_item[.="'.$addID.'"]')) > 0 )
{
echo '<script> alert("ID already exists in the filter");</script>';
}
它给出了一个 XPath 表达式(例如)
//filter_item[.="1142495027"]
由于这匹配其中一项,它将返回一个包含 1 项的列表。然后条件count()
将触发一个项目已经存在。
推荐阅读
- php - Laravel 圣所登录
- asp.net-core - 如何修复 ASP.NET Core 中的 WrongInputException?
- java - 如何找到并修复我的 android 应用程序的安全漏洞?
- node.js - 运行 pkg npm 模块创建的 .exe 文件时找不到 .wsf 文件
- svg - SVG 不使用 xlink 显示,仅使用 img src -
- .net-core - 如何在 dotnet core 中生成 OData 过滤器字符串
- javascript - 使用 vue 和 vue-router 确保视图顺序的最佳实践
- python - 将 Python 生成器转换为 C 函数
- python - Python - 如果服务器在 PUT 完成之前回答,则 HTTP 模块无法解析响应
- c# - 在结帐时转换为完整的类名,使用 Var 提交