通过php表单修改我的xml文件 - php

这是我的xml文件和下面的php代码。我输入了一个输入类型,它将按名字搜索学生。然后将显示有关特定学生的信息,并且将显示另一个按钮更新。

问题是我想在那之后修改信息。如何通过标签名称获取元素,以便可以修改有关特定学生的信息?

<students>
  <student>
    <firstname>John</firstname>
    <lasttname>Snow</lasttname>
    <student_id>160600</student_id>
    <gender>male</gender>
    <dob>23-06-95</dob>
    <email>[email protected]</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>[email protected]</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>

参考方案

考虑动态的XSLT(用于修改XML文档的转换语言),其中将表单值传递给XSLT脚本。但是,必须在当前脚本中更改以下几项:

您需要<form>标记来启动$_POST数组并将值发送到服务器端。在下面添加所需的操作。
您需要为每个<input>赋予一个独特的名称,而该名称已经通过$ getElementTag获得。考虑对服务器端的$_POST值进行清理。
您需要一个隐藏的输入字段来保留旧的名字,以防用户更改此值。此字段很重要,因为它在XSLT中用于选择适当的<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输入

通过php表单修改我的xml文件 - php

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>[email protected]</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>[email protected]</email>
    <mobilenumber>57553536</mobilenumber>
    <address>Curepipe</address>
    <cohort>BSE15AFT</cohort>
    <programme>software engineering</programme>
    <mode>FT</mode>
  </student>
</students>

故障排除“警告:session_start():无法发送会话高速缓存限制器-标头已发送” - php

我收到警告:session_start()[function.session-start]:无法发送会话缓存限制器-标头已发送(错误输出开始如果我将表单数据提交到其他文件进行处理,则可以正常工作。但是,如果我将表单数据提交到同一页面,则会出现此错误。请建议<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0…

使用PHP包含时的淡入淡出过渡 - php

我正在尝试使用jQuery或CSS(或其他方式!)在DIV中包含的php页面上创建淡入淡出或滑动过渡。我四处搜寻,发现了很多淡入淡出过渡的示例,这些实例彼此淡入淡出div或隐藏内容淡入淡出,但是这种情况略有不同。我有一个DIV,其内容由导航栏控制。选中后,每个页面都可以使用PHP成功地包含在div中,但我想使内容淡入和淡出。关于如何在页面更改之间进行漂亮过渡…

Div单击与单选按钮相同吗? - php

有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date&#…

PHP-复选框组 - php

我有一个需要发布的表单复选框组。<input type="checkbox" value="true" checked name="chk0[]"> <input type="checkbox" value="false" name=…

AJAX调用只能与$(document).on('click')一起使用 - php

我有一个显示数据库条目的表。用户能够为每一行打开一个弹出菜单。选项之一是删除数据库条目,并且该表应通过AJAX调用相应地刷新。只要有人单击#delete-toggle中的table-popup,我就会在HTML页面上进行AJAX调用(table-popup是div,当有人单击每行中存在的表中的table-edit-button时出现的表): <div …