Really eash logic php code

here is my code really easy what i am trying to do is have a php page that can edit the xml.

The page has a input box to add lines in the xml and a drop down that list all the lines in the xml.

but right now if you enter something in the input box and click the button it just enters i blank line in the xml.

here in the xml

<?xml version="1.0"?>
<display option="4">
      <title class="title"> Yoursite.com </title>
      <background class="background">Web site template </background>
      <line class="line"> I like Pie </line>
      <line class="line"> more stuff </line>
      <line class="line"> this is fun </line>
      <line class="line"> Thank you  </line>


</display>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?



$file = "doc.xml";

$xml = simplexml_load_file($file);

$addLine = $_POST["addthis"];


$xml->asXML($file);



if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
?>
<form method="post" action="xml.php" >

Add Line <input type="text"  name="addthis" length="30">

<? echo "<br><br>Option: "  .$xml['option']. "\n"; 
echo "<bR><br><select name=''>";

foreach($xml->line as $line){

echo	"<option value='$line'>$line</option>";
	

}


echo"</select>";

?>
<bR><bR>
<input type="submit" name="submit" value = "Search">

</form>

<?
$x = $xml->addChild("line",$addLine);
}
foreach($xml->line as $line){
echo "<br>Line: "  .$line. "\n";
}
?>


</body>
</html>

Open in new window

Christopher CaseySr Systems AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nanharbisonCommented:
This might be it - when you use a form tag that doesn't have a closing tag, as <table></table> does, you have to end the single tag with />, ie
<input type="submit" name="submit" value = "Search">

<input type="submit" name="submit" value = "Search" />

and same with the Br> - should be <br />

0
Avinash ZalaWeb ExpertCommented:
Try attached Code:

Hope this helps.
Addy
<?php
$file = "doc.xml";
$xml = simplexml_load_file($file);
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	$addLine = $_POST["addthis"];
	$xml->asXML($file);
	$xml->addChild("line",$addLine);
}
else
{
?>
<form method="post" action="xml.php" >
Add Line <input type="text"  name="addthis" length="30">
<?php
echo "<br><br>Option: "  .$xml['option']. "\n"; 
echo "<bR><br><select name=''>";
	foreach($xml->line as $line)
	{
		echo	"<option value='$line'>$line</option>";
	}
echo"</select>";
?>
<br>
<br>
<input type="submit" name="submit" value = "Search">
</form>
<?php
}
	$xml = simplexml_load_file($file);
	foreach($xml->line as $line)
	{
		echo "<br>Line: "  .$line. "\n";
	}
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hieloCommented:

Q_25889406
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?


$file = "doc.xml";

$xml = simplexml_load_file($file);




//$xml->asXML($file);



if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
?>
	<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>" >

		Add Line <input type="text"  name="addthis" length="30">

		<? 
		echo "<br><br>Option: "  .$xml['option']. "\n"; 
		echo "<bR><br><select name=''>";

		foreach($xml->line as $line){
			echo    "<option value='$line'>$line</option>";
		}
		echo"</select>";
?>
		<br><br>
		<input type="submit" name="submit" value = "Search">
	</form>
<?
}
else//you can only add a line if something has been posted
{
	$addLine = $_POST["addthis"];
	$x = $xml->addChild("line",$addLine);
}

foreach($xml->line as $line){
	echo "<br>Line: "  .$line. "\n";
}
?>


</body>
</html>

Open in new window

0
Christopher CaseySr Systems AdministratorAuthor Commented:
ok i did some changes the / didn't work but i did see the

$x = $xml->addChild("line",$addLine);  on line 49

i moved it to line 24 and it writes to the xml file but also every time i load the page it puts blank line. But at lest it also write what i enter in the input box
0
Christopher CaseySr Systems AdministratorAuthor Commented:
Thank you your code worked i had to change the

      $addLine = $_POST["addthis"];
      $xml->asXML($file);
      $xml->addChild("line",$addLine);

to

      $addLine = $_POST["addthis"];
      $xml->addChild("line",$addLine);
        $xml->asXML($file);

because you need to add the line be for writing it.


Thank you all
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.