这是我的xml文件和我的php代码。我已经输入了一个输入类型,它将通过名字搜索学生。然后将显示有关特定学生的信息,并将显示另一个按钮更新。
问题是我想在此之后修改信息。如何通过标签名称获取元素,以便我可以修改有关特定学生的信息?
xattr -rc .
<students>
<student>
<firstname>John</firstname>
<lasttname>Snow</lasttname>
<student_id>160600</student_id>
<gender>male</gender>
<dob>23-06-95</dob>
<email>JohnSnow@gmail.com</email>
<mobilenumber>57675060</mobilenumber>
<address>albatros, portlouis</address>
<cohort>BSE15PT</cohort>
<programme>Software Engineering</programme>
<mode>PT</mode>
</student>
<student>
<firstname>Jey</firstname>
<lastname>Lacroix</lastname>
<student_id>150501</student_id>
<gender>M</gender>
<dob>1990-02-22</dob>
<email>Jey@hotmail.com</email>
<mobilenumber>57553536</mobilenumber>
<address>Curepipe</address>
<cohort>BSE15AFT</cohort>
<programme>software engineering</programme>
<mode>FT</mode>
</student>
</students>
答案 0 :(得分:2)
考虑使用动态XSLT(用于修改XML文档的转换语言),其中将表单值传递给XSLT脚本。但是,当前脚本中必须更改几个项目:
<form>
个标记来启动$_POST
数组并将值发送到服务器端。在下面添加所需的操作。<input>
提供一个与 $ getElementTag 已有的不同名称。请考虑清理服务器端$_POST
值。<student>
节点。PHP 脚本
下面的脚本只包含两个if (isset(...)
条件。集成到您的完整脚本中,并确保回显不会出现在<html>
和<head>
之上。此外,还包括嵌入式XSLT字符串。一定要在.ini文件中启用XSLTProcessor扩展名(php_xsl.dll或php_xsl.so)。
<?php
$xml=simplexml_load_file($cd."/FormInput.xml") or die("Error: Cannot Create Object");
if (isset($_POST['modify'])) {
$oldfirstname=($_POST['oldfirstname']);
$firstname=($_POST['firstname']);
$lastname=($_POST['lastname']);
$student_id=($_POST['student_id']);
$gender=($_POST['gender']);
$dob=($_POST['dob']);
$email=($_POST['email']);
$mobilenumber=($_POST['mobilenumber']);
$address=($_POST['address']);
$cohort=($_POST['cohort']);
$programme=($_POST['programme']);
$mode=($_POST['mode']);
$xslstr = '<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="student[firstname=\''.$oldfirstname.'\']">
<xsl:copy>
<firstname>'.$firstname.'</firstname>
<lasttname>'.$lastname.'</lasttname>
<student_id>'.$student_id.'</student_id>
<gender>'.$gender.'</gender>
<dob>'.$dob.'</dob>
<email>'.$email.'</email>
<mobilenumber>'.$mobilenumber.'</mobilenumber>
<address>'.$address.'</address>
<cohort>'.$cohort.'</cohort>
<programme>'.$programme.'</programme>
<mode>'.$mode.'</mode>
</xsl:copy>
</xsl:template>
</xsl:transform>';
$xsl = new DOMDocument;
$xsl->loadXML($xslstr);
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
// Transform XML source
$newXml = $proc->transformToXML($xml);
// Save into new file
file_put_contents($cd."/FormInput_php.xml", $newXml);
}
if(isset($_POST['search'])) {
//query the document
$name = $_POST['studentname'];
$query = $xml->xpath("/students/student[firstname = '$name']");
$array=$query;
$count=0;
$size=count($array);
echo "<center>";
echo '<form id="contactform" name="contactform" method="post">';
while($count!=count($array)){
foreach ($array[$count]->children() as $child) {
$getElementTag=$child->getName();
echo '<label>'.$getElementTag.'</label>'." ";
echo '<input type="text" name="'. $getElementTag .'" value= "'.$child.'" size="30"></intput>';
echo "<br>";
echo "<br>";
}
$count++;
}
echo '<input type="hidden" name="oldfirstname" value="'.$name.'"></input>';
echo '<input type="submit" name="modify" value="Update Record">'.'<br>';
echo "</form>";
echo "***************************";
echo "</center>";
}
?>
HTML 输入
XML 输出
(新文件不会覆盖现有文件,请参阅Jess StackOverflow如何替换旧John Snow)
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student>
<firstname>Jess</firstname>
<lasttname>Stackoverflow</lasttname>
<student_id>999999</student_id>
<gender>female</gender>
<dob>10-05-16</dob>
<email>JStackoverflow@example.com</email>
<mobilenumber>7777777</mobilenumber>
<address>Example, Place</address>
<cohort>HGJD13D</cohort>
<programme>Web development</programme>
<mode>FT</mode>
</student>
<student>
<firstname>Jey</firstname>
<lastname>Lacroix</lastname>
<student_id>150501</student_id>
<gender>M</gender>
<dob>1990-02-22</dob>
<email>Jey@hotmail.com</email>
<mobilenumber>57553536</mobilenumber>
<address>Curepipe</address>
<cohort>BSE15AFT</cohort>
<programme>software engineering</programme>
<mode>FT</mode>
</student>
</students>