通过php表单修改我的xml文件 [英] modify my xml file via php form
问题描述
这是我的 xml 文件和我的 php 代码.我已经输入了一个输入类型,它将按名字搜索学生.然后会出现关于特定学生的信息,并且会出现另一个按钮更新.
问题是我想在那之后修改信息.如何通过标签名称获取元素,以便我可以修改特定学生的信息?
<学生><firstname>John</firstname><lasttname>Snow</lasttname><student_id>160600</student_id><性别>男性</性别><dob>23-06-95</dob><email>JohnSnow@gmail.com</email><手机号码>57675060</手机号码><address>albatros, portlouis</address><群组>BSE15PT</群组><程序>软件工程</程序><mode>PT</mode></学生><学生><firstname>Jey</firstname><lastname>Lacroix</lastname><student_id>150501</student_id><gender>M</gender><dob>1990-02-22</dob><email>Jey@hotmail.com</email><手机号码>57553536</手机号码><address>Curepipe</address><群组>BSE15AFT</群组><程序>软件工程</程序><mode>FT</mode></学生></学生>
xpath("/students/student[firstname = '$name']");$array=$查询;//echo "";//rint_r($array);//echo "</pre>";$count=0;$size=count($array);//echo $count;echo "";while($count!=count($array)){foreach ($array[$count]->children() as $child) {//将值存储在 child$getElementTag=$child->getName();//获取标签所以nomecho ''." ";echo '<input type="text" value=" '.$child.'" size="30"></input>';echo "
";echo "
";}$count++;}echo ''.'
';echo "*******************************";echo "</center>";}?><!DOCTYPE html><头><title>搜索</title>头部><身体><中心><form method="POST" action="searchtest.php"><label>输入学生姓名</label><input type="text" name="studentname" pattern="[A-Z][a-z]+" title="必须以大写字母开头!"需要><br><br><input type="submit" name="search" value="search"></表单></中心></html>
考虑动态的
XML 输出
(新文件不会覆盖现有文件,看看 Jess StackOverflow 如何替换旧的 John Snow)
This is my xml file and my php code below. I have put an input type which will search student by firstname. Then information about specific student will appear and another button update will appear.
The issue is that I want to modify the information after that. How can I get the element by tag name so that I can modify the information about the specific student?
<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>
<?php
if(isset($_POST['search']))
{
$xml=simplexml_load_file("studentInstance.xml") or die("Error: Cannot Create Object");
//query the document
$name = $_POST['studentname'];
//$xml = simplexml_load_string($xml);
$query = $xml->xpath("/students/student[firstname = '$name']");
$array=$query;
//echo "<pre>";
//rint_r($array);
//echo "</pre>";
$count=0;
$size=count($array);
//echo $count;
echo "<center>";
while($count!=count($array)){
foreach ($array[$count]->children() as $child) {//stores values in child
$getElementTag=$child->getName();//get tag so nom
echo '<label>'.$getElementTag.'</label>'." ";
echo '<input type="text" value= " '.$child.' " size="30"></intput>';
echo "<br>";
echo "<br>";
}
$count++;
}
echo '<input type="submit" name="modify" value="Update Record">'.'<br>';
echo "***************************";
echo "</center>";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Searching</title>
</head>
<body>
<center>
<form method="POST" action="searchtest.php">
<label>Enter Student Name</label>
<input type="text" name="studentname" pattern="[A-Z][a-z]+" title="Must start with capital letters!" required><br>
<br>
<input type="submit" name="search" value="search">
</form>
</center>
</body>
</html>
Consider a dynamic XSLT (the transformation language used to modify XML documents) where form values are passed to the XSLT script. However, several items must change in current script:
- You need
<form>
tags to initiate the$_POST
array and send values to server-side. Add needed action below. - You need to give each
<input>
a distinct name which you already have with $getElementTag. Consider sanitizing the server side$_POST
values. - You need a hidden input field to retain the old firstname in case the user changes this value. This field is important as it is used in XSLT to select the appropriate
<student>
node to update.
PHP Script
Below script contains only the two if (isset(...)
conditionals. Integrate into your full script and be sure echoes do not appear above <html>
and <head>
. Also, the embedded XSLT string is included. Be sure to have the XSLTProcessor extension (php_xsl.dll or php_xsl.so) enabled in .ini file.
<?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 Input
XML Output
(new file does not overwrite existing, see how the Jess StackOverflow replaces the old 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>
这篇关于通过php表单修改我的xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!