views:

715

answers:

3

Hello everyone

I am trying to parse a xml file that i have (using javascript DOM). and then i want to insert this data in a mysql table (using php).

I know how to parse xml data. But i am unable to figure out how to use php and javascript together and insert data in mysql table.

Kindly help me in this.

Best Zeeshan

+2  A: 

The JavaScript needs to send the data to the server. There are several ways this can be achieved.

  1. Generate a form and submit it
  2. Use XMLHttpRequest directly
  3. Use a library like YUI or jQuery

Once there, you get the data (from $_POST for example) and insert it into MySQL as normal.

David Dorward
this is stupid of me, but can you tell me how should i make a form that has all my xml parsed data, and then which i can submit to php on server so that it can insert each record of my xml in a table
Zeeshan Rang
var form = document.createElement('form'); form.action='foo.php'; form.method="post"; var data1 = document.createElement('input'); data1.value = myVar; data1.name="data1"; form.appendChild(data1); document.body.appendChild(form); form.submit(); /* Simplified of course */
David Dorward
+1  A: 

You can use jQuery to do this, for example:

<house>
 <roof>as</roof>


<door>asd</door><window number="12">iles</window></house>

$('path_to_xml',xml).each(function(){ roof = $('roof',this).text(); door = $('door',this).text(); num_wind = $('window',this).attr('number'); });

red777
+2  A: 

Why don't you just use PHP DOM to parse the XML ?

e.g:

$doc = new DOMDocument();
$doc->load('book.xml');
echo $doc->saveXML();

After loaded your document $doc you can use the PHP DOM fuctions.

Freddy
I would love to use php dom. it will make things so very easy to do right?can you please give me a few sample, where i can learn about how to parse xml in php dom.
Zeeshan Rang