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?
 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.