如何在xml文件中添加多个选择选项?

时间:2018-03-09 08:45:16

标签: php html xml

代码与单选选项一起使用。但是当我选择多个选项时会出错。我知道这是一个数组,需要更改代码。任何人都可以帮助编辑以下代码。

<html>
    <body>
    <form method ="post" action ="test.php">
     <h1> School List </h1>
     <table>
     <tr> <td> Name : </td> <td> <input type = "text" name = "Name"> </td> </tr>
     <tr> <td> Sex : </td> <td> <input type = "text" name = "Sex"> </td> </tr>
     <tr> <td width="200px"> Location : </td> 
     <td> <select name = "Location[]" multiple="multiple"> 
     <option value = "New York" selected ="selected"> New York </option> 
     <option value = "Boston" > Boston </option> 
     <option value = "New Jersey"> New Jersey </option> 
     <option value = "Virginia"> Virginia </option> </select>
     </td> </tr> 
     <tr> <td> </td> 
     <td> <input type ="submit" name="insert" value ="Add"> </td></tr>
     </form>
     </body>
     </html>

     <?php
     if(isset($_REQUEST['insert'])){
     $xml =     new DomDocument("1.0","UTF-8");
     $xml->load("schoollist.XML");

     $schoollistTag = $xml->getElementsByTagName("schoollist")->item(0);
     $xml->formatOutput=true;
     $schoolTag = $xml->createElement("school"); 

     $NameTag = $xml->createElement("Name",$_REQUEST['Name']);
     $SexTag = $xml->createElement("Sex",$_REQUEST['Sex']);
     $LocationTag = $xml->createElement("Location",$_REQUEST['Location']);

     $schoolTag->appendChild($NameTag);
     $schoolTag->appendChild($SexTag);
     $schoolTag->appendChild($LocationTag);

     $schoollistTag->appendChild($schoolTag);
     $xml->save("schoollist.XML");              
     }
     ?>

获取xml:

 <schoollist>
 <school>
 <Name>Jacob</Name>
 <Sex>Male</Sex>
 <Location>Boston</Location>
 </school>
 </schoollist>

需要xml,如:

<schoollist>
 <school>
 <Name>Jacob</Name>
 <Sex>Male</Sex>
 <Location>Boston</Location>
 <Location>Virginia</Location>
 <Location>New York</Location>
 </school>
 </schoollist>

提前致谢!

0 个答案:

没有答案