Friday, 5 December 2014

html input names into an xml document using PHP



This might be a simple question, but I'm new to php. I need to use html input tags with the same name and write the entries to an xml document using php. Most of what I found uses the same name in conjunction with radio buttons, but I need these to be text input. Most also agree that an array is the easiest way to do that, but I can't make the array go to the xml file. I want to have multiple inputs of the same name go to the xml because I don't know how many authors the users will want to put in the xml. I used the clonenode method to create more available entries. I tried to change the name of the input elements, but that doesn't seem to work, and I think it would probably be easier to just get all of the input to post to the xml file. This is the form for the html.



<form action="generate.php" method="post" name="book">
<div id="wrap">
<p id="author0">Author's First Name: <input type="text" name="firstInitial" placeholder="F" autocomplete="off" maxLength="1" size="1" ><input type="text" name="firstName" placeholder="irst Name" autocomplete="off" /></input>
Author's Middle Initial: <input type="text" name="middleInitial" maxLength="1" placeholder="Middle Initial" autocomplete="off" />
Author's Last Name: <input type="text" name="lastName" placeholder="Last Name" autocomplete="off" /></p>
</div>
<input type="button" value="Add Author" onclick="duplicate()"/>
<p>Title: <input type="text" name="title" placeholder="Title" /> </p>
<p>Publisher: <input type="text" name="publisher" placeholder="Publisher" /></p>
<p>City: <input type="text" name="city" placeholder="City" /></p>
<p>State: <input type="text" name="state" placeholder= "State" /></p>
<p>Year: <input type="text" name="year" placeholder="Year" /></p>
<!--Change the condition to a raio button-->
<p>Condition: <input type="text" name="condition" /> (Enter Electronic if not in Print)</p>
<p>First Line:
<input type="text" name="firstLine" rows="4" cols="50" autocomplete="off" placeholder="First Line" ></input>
</p>
<input type="submit" value="Cite"></input>
</form>


This is the php that it currently runs off of.



<?php
header("Location:start.html");
$xmldoc = new DOMDocument("1.0","UTF-8");
$xmldoc->preserveWhiteSpace = false;
$xmldoc->load("bookList.xml");
$xmldoc->formatOutput=true;

$root = $xmldoc->firstChild;
$book = $xmldoc->createElement("book");
$root->appendChild($book);
//create first author tag
$firstAuthor=$xmldoc->createElement("firstAuthorEditor");
$firstAuthor=$book->appendChild($firstAuthor);

//add the first name (just the tag)
$firstName=$xmldoc->createElement("firstName", $_POST["firstName"]);
$firstName=$firstAuthor->appendChild($firstName);

//add the first initial to the first Name
$firstInitial=$xmldoc->createElement("firstInitial", $_POST["firstInitial"]);
$firstInitial=$firstName->appendChild($firstInitial);
$firstName->insertBefore( $firstInitial, $firstName->firstChild);

//add the middle name
$middleName=$xmldoc->createElement("middleName");
$middleName=$firstAuthor->appendChild($middleName);
//create a place for the middle initial within the authors name
$middleInitial=$xmldoc->createElement("middleInitial", $_POST["middleInitial"]);
$middleInitial=$middleName->appendChild($middleInitial);

//add the last name
$lastName=$xmldoc->createElement("lastName", $_POST["lastName"]);
$lastName=$firstAuthor->appendChild($lastName);

//creates the title tag and inputs the tag from the html that says 'name="title"'
$title=$xmldoc->createElement("title", $_POST["title"]);
$title=$book->appendChild($title);

$publisher=$xmldoc->createElement("publisher", $_POST["publisher"]);
$publisher=$book->appendChild($publisher);

$city=$xmldoc->createElement("city", $_POST["city"]);
$city=$book->appendChild($city);

$state=$xmldoc->createElement("state", $_POST["state"]);
$state=$book->appendChild($state);

$year=$xmldoc->createElement("year", $_POST["year"]);
$year=$book->appendChild($year);

$condition=$xmldoc->createElement("condition", $_POST["condition"]);
$condition=$book->appendChild($condition);

$firstLine=$xmldoc->createElement("firstLine", $_POST["firstLine"]);
$firstLine=$book->appendChild($firstLine);

$xmldoc->save("bookList.xml");


?> It's pretty simple, but I left most of my notes. I want the firstAuthor to be for all of the authors now so that's going to change, but I need it to work. Any help would be greatly appreciated. Thanks.


No comments:

Post a Comment