1

I'm new to both XML and PHP but I have this project and this is what needs to be done.

I have an html form like this:

<table>   
  <tr>
    <td align="right" width="120px">First name:</td>
    <td><input id="txtfname" style="width: 200px" type="text" /></td>
  </tr>
  <tr>
    <td align="right">Last name:</td>
    <td><input id="txtlname" style="width: 200px" type="text" /></td>
  </tr>
  <tr>
    <td align="right">Business name:</td>
    <td><input id="txtbisname" style="width: 200px" type="text" /></td>
  </tr>
</table>

and I have an xml file like this:

<data>
    <first_name></first_name>
    <last_name></last_name>
    <business_name></business_name>
</data>

is it possible to use a php file to write the data from the html forms to the xml file? If so, can anyone please tell me how. I really need to solve this problem.

After this i also need to transfer the xml file to a sql database.

I'm all ears.

Thanks..

2 Answers 2

1

Try this. It assumes, you have your HTML table in a form and post it to the server.

<?php
$str = '<?xml version="1.0" encoding="utf-8"?><data></data>';
$xml = simplexml_load_string( $str );

$fname = trim( $_POST[ 'txtfname' ] );
$lname = trim( $_POST[ 'txtlname' ] );
$bname = trim( $_POST[ 'txtbisname' ] );

$fname = htmlentities( $fname, ENT_COMPAT, 'UTF-8', false );
$lname = htmlentities( $lname, ENT_COMPAT, 'UTF-8', false );
$bname = htmlentities( $bname, ENT_COMPAT, 'UTF-8', false );

$xml->addChild( 'first_name', $fname );
$xml->addChild( 'last_name', $lname );
$xml->addChild( 'business_name', $bname );

$doc = new DOMDocument( '1.0' );
$doc->formatOutput = true;
$doc->preserveWhiteSpace = true;
$doc->loadXML($xml->asXML(), LIBXML_NOBLANKS);
$doc->save( 'data.xml' );
?>

Now, data.xml is the xml file. Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Quick question though. the data displays like this <data><first_name>James</first_name><last_name>Jr.</last_name><business_name>The CEO Club of Pittsburgh</business_name></data>. But I need it to display like the one on my sample code.
You are really a life saver. Thanks again @Pushpesh. ^_^
0

I think XSLT is useful for you. Check this out: http://www.w3schools.com/xsl/

1 Comment

I thought XSLT is just a stylesheet appropriate for xml documents

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.