ayman
ayman

Reputation: 51

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?

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-------

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-----------

$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");

           }

Upvotes: 0

Views: 2838

Answers (1)

Thomas Brasington
Thomas Brasington

Reputation: 588

You could use php substr

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

As a side note you should look into sanitising the $_POST data before.

EDIT

I have sorted out your variable names as you where overwriting the post vars with the xml vars

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

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

</doc>

Upvotes: 1

Related Questions