简体   繁体   English

在xml文件中存储html表单值

[英]storing html form values in xml file

i have entered 15 characters in 'item_detail'(data.html) field but i want to store just 10 characters in 'itemdetail' element in xml file(item.xml) how can i do that? 我在“ item_detail”(data.html)字段中输入了15个字符,但是我只想在xml文件(item.xml)的“ itemdetail”元素中存储10个字符,我该怎么做?

data.html---- data.html ----

 <form>
     <p>Id:<input type="text" name= "id" size="10" /> </p>
     <p>Item Detail:<textarea name="item_detail" rows="3" cols="50" ></textarea></p>
     <input name="submit" type = "button" onClick = "getData('data.php','info', id.value, ,item_detail.value)" value = "Add Item" />

</form>    

 <div id="info"> </div>

data.js------- data.js -------

var xhr = createRequest();
function getData(dataSource, divID, id,itemd) {
if(xhr) {
var obj = document.getElementById(divID);
var requestbody ="idd="+encodeURIComponent(id)+"&itd="+encodeURIComponent(itemd);
xhr.open("POST", dataSource, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
obj.innerHTML = xhr.responseText;
} // end if
} // end anonymous call-back function
xhr.send(requestbody);
} // end if
} // end function getData()

data.php file----------- data.php文件-----------

$id= $_POST["idd"];
$item_detail= $_POST["itd"];


            $xml = new DomDocument();
            $xml->load("item.xml");
            $xml->formatOutput = true;   
            $items = $xml->firstChild;     
            $item = $xml->createElement('item');   
            $items->appendChild($item); 
            $id = $xml->createElement('id');
            $itemdetail = $xml->createElement('itemdetail'); 
            $item->appendChild( $id );
            $id->nodeValue = $id;
            $item->appendChild( $itemdetail );
            $ItemDetail->nodeValue = $item_detail;


            $xml->save("item.xml");

           }

You could use php substr 您可以使用php substr

$item_detail= substr($_POST["itd"], 0,10);

As a side note you should look into sanitising the $_POST data before. 附带说明,您应该先清理$ _POST数据。

EDIT 编辑

I have sorted out your variable names as you where overwriting the post vars with the xml vars 我已经为您整理了变量名,并在其中用xml vars覆盖了post vars

data.php data.php

 <?php
$post_id= $_POST["idd"];
$post_item_detail = substr($_POST["itd"], 0,10);

$xml = new DomDocument();
$xml->load("item.xml");

$xml->formatOutput = true;   

$items = $xml->documentElement;
$itemsLength = $items->getElementsByTagName('item');
for ($i = 0; $i < $itemsLength->length; $i++) {
$itm = $items->getElementsByTagName('item')->item($i);
$oldItem = $items->removeChild($itm);
}


$items = $xml->firstChild;     
$item = $xml->createElement('item');   
$items->appendChild($item); 
$id = $xml->createElement('id');
$itemdetail = $xml->createElement('itemdetail'); 
$item->appendChild( $id );
$id->nodeValue = $post_id;
$item->appendChild( $itemdetail );
$itemdetail->nodeValue = $post_item_detail;

$xml->save("item.xml");

?>

item.xml item.xml

<?xml version="1.0" encoding="UTF-8"?>
<doc>

</doc>

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

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