简体   繁体   English

如何更改XML节点JavaScript或PHP

[英]How to change XML node JavaScript or PHP

I am trying to create a rating system. 我正在尝试创建一个评分系统。

I have an XML file, which has the follow information: 我有一个XML文件,其中包含以下信息:

<?xml version="1.0" encoding="ISO-8859-1"?>
<PROGRAMMEDATA> 
    <PROGRAMME id="1">
        <TITLE>Arrow</TITLE>
        <IMAGE>img/arrow.jpg</IMAGE>
        <POINTS>0</POINTS>
    </PROGRAMME>
    ...etc
<PROGRAMMEDATA>

I then have a form, with programme options and 1 to 5 score. 然后,我得到一个表格,其中包含程序选项和1到5分。

<form>
    <select id="name">
        <option selected value=""></option>
        <option value="1">Arrow</option>
        <option value="2">You, Me and Dupree</option>
        <option value="3">Fargo</option>
        <option value="4">Flash</option>
    </select>
    <select id="rating">
        <option selected value="">stars</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
    </select>
    <input type="submit" value="Submit rating" onClick="saveRating()" />
</form>

The idea is you can choose a programme and then choose a rating. 这个想法是您可以选择一个程序,然后选择一个评分。 You can then submit that and the rating will then be added to the corresponding programme in the XML file. 然后,您可以提交该评分,然后将该评分添加到XML文件中的相应程序中。

I have tried to capture the form values with javascript, but I then don't know how to store the value in the XML file. 我试图用javascript捕获表单值,但是然后我不知道如何将值存储在XML文件中。

var name = document.getElementById("name").value;
var rating = document.getElementById("rating").value; 

Another way I've thought about is using PHP, and using DOMDocument, however I can't get it to append the POINTS, it only adds to the bottom of the XML file. 我考虑过的另一种方法是使用PHP和DOMDocument,但是我无法获取它来附加POINTS,它只会添加到XML文件的底部。

Any help greatly appreciated. 任何帮助,不胜感激。

Something like this should work. 这样的事情应该起作用。

$file ="ratings_file.xml";

$title="test";
$image="img/arrow.jpg";
$points=0;

//load xml object
$xml= simplexml_load_file($ratings_file);

//assign title
$xml->PROGRAMME->title = $title;

//assign image
$xml->PROGRAMME->image = $image;

//assign points
$xml->PROGRAMME->points = $points;

//store the value into the file
file_put_contents($ratings_file, $xml->asXML());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM