使用PHP将html输入名称转换为xml文档
这可能是一个简单的问题,但我对PHP很陌生。我需要使用具有相同名称的html输入标签,并使用php将条目写入xml文档。我发现的大多数使用与单选按钮相同的名称,但我需要这些文本输入。大多数人也同意数组是最简单的方法,但我不能让数组转到xml文件。我想要多个同名输入到xml,因为我不知道用户将要在xml中放置多少个作者。我使用clonenode方法创建更多可用条目。我尝试更改输入元素的名称,但似乎并不奏效,我认为将所有输入发布到xml文件可能会更容易。这是html的表单。使用PHP将html输入名称转换为xml文档
<form action="generate.php" method="post" name="book">
<div id="wrap">
<p id="author0">Author's First Name: <input class="firstInitial" type="text" name="firstInitial[]" placeholder="F" autocomplete="off" maxLength="1" size="1" ><input class="firstName" 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 class="firstInitial" 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>
这是目前运行的php。
<?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);
$firstAuthor=$xmldoc->createElement("authorEditor");
$firstAuthor=$book->appendChild($firstAuthor);
$firstName=$_POST["firstName"];
$firstInitial=$_POST["firstInitial"];
$middleInitial=$_POST["middleInitial"];
$lastName=$_POST["lastName"];
for($x = 0; $x < count($firstName); $x++)
{
$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 rest of the first name
/*$firstName=$xmldoc->*/
/*$firstName->nodeValue=$_POST['firstName'];*/
//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");
?>
这很简单,但我留下了大部分笔记。我希望firstAuthor现在可以适用于所有作者,所以这将会发生变化,但我需要它的工作。任何帮助将不胜感激。谢谢。 经过多一点研究之后,我想我得到的阵列几乎可以工作,但它仍然只发布第一作者而不是所有人。更新了代码。
我想通了。我需要把它作为一个数组放在后面.HTML代码<p id="author0">Author's First Name: <input class="firstInitial" type="text" name="firstInitial[]" placeholder="F" autocomplete="off" maxLength="1" size="1" ><input class="firstName" 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 class="firstInitial" type="text" name="lastName[]" placeholder="Last Name" autocomplete="off" /></p> </div> <input type="button" placeholder="Add Author" value="Add Author" onclick="duplicate()" ></input> <!--Use autocomplete="off" so the input isn't there on refresh or cite--> <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>
PHP代码 `preserveWhiteSpace = false; $ xmldoc-> load(“bookList.xml”); $ xmldoc-> formatOutput = true;
$root = $xmldoc->firstChild;
$book = $xmldoc->createElement("book");
$root->appendChild($book);
//create first author tag
for($x = 1; $x < count($_POST["firstName"]); $x++)
{
$firstAuthor=$xmldoc->createElement("authorEditor");
$firstAuthor=$book->appendChild($firstAuthor);
$firstName=$xmldoc->createElement("firstName", $_POST["firstName"][$x]);
$firstName=$firstAuthor->appendChild($firstName);
//add the first initial to the first Name
$firstInitial=$xmldoc->createElement("firstInitial", $_POST["firstInitial"][$x]);
$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"][$x]);
$middleInitial=$middleName->appendChild($middleInitial);
//add the last name
$lastName=$xmldoc->createElement("lastName", $_POST["lastName"][$x]);
$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");
?>`