I am trying to generate an XML document from html form input using PHP all on a LAMP stack. I can generate the XML document, but I seem to be having problems brining in the data from the html field.
I am trying to call $_POST['itemName']
as a text item in my PHP code, but I am getting an error that says that itemName
is undefined. Please see the partial index.html
document and full generate.php
code below:
Here is the full `generate.php` code:
<?php
$dom = new DomDocument('1.0');
$orders = $dom->appendChild($dom->createElement('orders'));
$order = $orders->appendChild($dom->createElement('order'));
$item = $order->appendChild($dom->createElement('item'));
$item->appendChild($dom->createTextNode($_POST['itemName']));
$dom->formatOutput = true;
$orders = $dom->saveXML();
$dom->save('orders.xml');
?>
Here is the form section from index.html
:
<form name="form1" action="generate.php" method="post">
<input type="text" class="form-control" id="itemName"
placeholder="Item Name">
<button type='submit' action='submit' name='generate'>Generate XML</button>
</form>
I expect to get an XML document like this:
<?xml version="1.0"?>
<orders>
<order>
<item>text from form field</item>
</order>
</orders>
What I get is the following error displayed in the browser:
Notice: Undefined index: itemName in generate.php on line 17
I have test the Apache Server and PHP on my local machine, and it works correctly, so the problem definitely lies within my code.
I feel like this is really simple and I am making a pretty obvious mistake, but I don't really know PHP, so this is all kind of new to me.
Thanks!