我正在从 XML 文件加载值,然后使用 php 页面上的表单更新 XML。我的问题是 - 我有一个查询在节点上运行计数,它工作得很好,但我也想用计数更新节点并获得准确的值。当我提交表单时,它会提交我加载文件时的值,但为了正确更新计数(因为我需要更新的节点计数),我需要让它提交两次。我试图让它在主体 onload 函数上提交表单,但是从我可以确定当您点击提交按钮时它不会进行真正的整页刷新。它重新加载页面,但我一直无法让它实际运行任何脚本或加载功能。我想到但不能的替代方案
<!DOCTYPE html>
<?php
$xml->load('test.xml');
$xpath = new DOMXpath($xml);
$liftsopen = $xpath->query("//node/Lifts-Open")->item(0);
$lift1status = $xpath->query("//areas/area[@name='Lift1']/lifts/lift[@name='Lift1']/@status")->item(0);
$lift2status = $xpath->query("//areas/area[@name='Lift2']/lifts/lift[@name='Lift2']/@status")->item(0);
$liftcount = 0;
foreach ( $xpath->query('//lifts/lift[@status="OPEN"]') as $count1 ) {
$liftcount++;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$lift1->nodeValue = $_POST['lift1statusform'];
$lift2->nodeValue = $_POST['lift2statusform'];
$liftsopen->nodeValue = $liftcount;
$xml->save('test.xml');
}
?>
<body>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<h4>Lift1</h4>
<select name="lift1statusform" >
<option selected value="<?= htmlspecialchars($lift1status->nodeValue) ?>"><?= htmlspecialchars($lift1status->nodeValue) ?></option>
<option value="OPEN">OPEN</option>
<option value="CLOSED">CLOSED</option>
</select>
<h4>Gondola</h4>
<select name="lift2statusform" >
<option selected value="<?= htmlspecialchars($lift2status->nodeValue) ?>"><?= htmlspecialchars($lift2status->nodeValue) ?></option>
<option value="OPEN">OPEN</option>
<option value="CLOSED">CLOSED</option>
</select>
<input name="submit" type="button" value="Save"/>
</form>
</body>
</html>
偶然的你