0

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

           }
1
  • Do you want to store the first ten characters, or the last ten? Of do you want to save five characters less than the input? Can you provide examples of what you want to achieve? BTW, using POST requests, you should replace %20 by + after encoding the string with encodeURIComponent. Commented Apr 24, 2011 at 12:25

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

8 Comments

i want to enter the first 10 characters of 'item_detail' into 'itemdetail' element of xml file
@thomas , i tried with ur code but it gives an error 'Parse error: syntax error, unexpected '=' in data.php on line 2
yes its working.thanks for ur help. can u also provide code for deleting all elements from 'item.xml' file
I have added a method that removes all nodes labeled item.
but i want to delete all elements including root element also from xml file
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.