Jump to content

Trouble with Form Data and the XML DOM


kneifelspy

Recommended Posts

So I am new to PHP, so please go easy on me.

 

I am building a content management system for a website that runs on XML and PHP using both the XML DOM and SimpleXML.  The system is meant to populate a list of ALL current news-posts and then give options on whether to preview, modify, or delete the entry.  SO FAR, my delete function works just fine--however my preview function is a complete mess.

 

The main script is newsarchive.php, which looks like this:

 

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<title>Selection Form Test</title>

<script language="JavaScript">
<!--
function openWindow(theURL, winName, features)
{
window.open(theURL, winName, features);
return false;
}
//-->

<!--
var cnf;
var last;
function cnf(msg)
{
cnf = confirm(msg);
if (cnf) {
	last = 'true';
	}
}
function send(var)
{
if (last = "true") {
	var.submit();
	}
}
//-->
</script>

</head>

<body>

<?php

$xml = simplexml_load_file('note.xml');

$counter = 0;

$entrys = $xml->xpath('/blogs/blog');
foreach ($entrys as $entry) {
echo "{$entry->blogtitle}";

?>
<form method="post" action="newspreview.php" target="_blank" onsubmit="return openWindow('newspreview.php','Preview_Entry','width=400, height=500, toolbar=no, menubar=no, resizable=no, location=no, directories=no, status=no');" />
	<input type="hidden" name="entry_number" value="<?echo $counter;?>" />
	<input type="submit" value="preview entry" />
</form>
<form method="post" action="newsedit.php">
	<input type="hidden" name="entry_number" value="<?echo $counter;?>" />
	<input type="submit" value="modify entry" />
</form>
<form method="post" action="delete.php">
	<input type="hidden" name="entry_number" value="<?echo $counter;?>" />
	<input type="hidden" name="source_page" value="newsarchive.php" />
	<input type="submit" value="delete entry" onclick="return confirm('Are you sure you want to DELETE the selected entry?');" />
</form><br />
<?

$counter = $counter + 1;
}

?>

</body>
</html>

 

It uses simple XML to populate the list of entries by title, and offers three form options.  Each of which submits an entry number to the next form so that that PHP script can easily grab whichever entry is selected.

 

There are two issues.  The first is related to the preview button.  When I submit my form to preview any given entry, it sends the post data to newspreview.php -- which looks like this:

 

<?php

$n = $_POST['entry_number'];

$xml = simplexml_load_file('note.xml');

$n_blogtitle = $xml->blog[$n]->blogtitle;
$n_author = $xml->blog[$n]->author;
$n_content = $xml->blog[$n]->content;
$n_timestamp = $xml->blog[$n]->timestamp;

echo "<b>Title:</b><br /> $n_blogtitle <br /><br />";
echo "<b>Author:</b><br /> $n_author <br /><br />";
echo "<b>Content:</b><br /> $n_content <br /><br />";
echo "<b>Date/Time:</b><br /> $n_timestamp <br /><br />";

?>

 

Skipping over the fact that the preview is NOT opening in a new popup window (which is my second problem and involves the Javascript above)--when newspreview.php runs, it does not display ANY of the blogentry data.  Even when I strip the SimpleXML code down to the basics, and just tell it to echo the raw data, it doesn't show up.  I tried re-writing the code with the XML DOM, but it doesn't work at all either.  The WEIRD thing is that my delete.php works PERFECTLY under the same principles.  Here is what that looks like:

 

<?php

$n = $_POST['entry_number'];
$site = $_POST['source_page'];

$dom = new DomDocument();
$dom->load('note.xml');
$xpath = new DomXPath($dom);

$result = $xpath->query('/blogs/blog');

$result->item($n)->parentNode->removeChild($result->item($n));

echo "<br /><b>Entry successfully deleted!</b><br /><br />";
$dom->save('note.xml');

include("$site");

?>

 

What's wrong with my script.  The delete function works fine, but the preview function doesn't at ALL, even when I change the code to the XML DOM instead of SimpleXML.  I am using PHP v5.x on my server.  Also can anyone debug my Javascript to tell me why the newspreview.php isn't opening in a popup window?

 

Thanks,

--Kevin

Link to comment
Share on other sites

not to be annoying but you probably should upgrade your site to running off of a database, it is much more secure than the xml method (if thats what you're doing)

 

and for your php problems try

 

error_reportng(E_ALL);

 

at the top of every php file that way you can see all the errors which occur than post them back here

Link to comment
Share on other sites

Preview button not working.

function send(var)
{
   if (last = "true") {
      var.submit();
      }
}

I removed this function and it worked.  The error is that the argument for send() "var" is a reserved word, change it to arg or something, and change it inside the function also.

 

The problem with your popup is that it doesn't receive any POST data.  I don't know of any "right" or proper way to do it, but one method is to append an argument of the xmlfile name or id or w/e unique identifier you have and to have newpreview.php read that and load the preview.[read: use GET instead]

 

newsarchive.php

<form onsubmit="return openWindow('newspreview.php?entry_number=<?php echo $counter; ?>','Preview_Entry','width=400, height=500, toolbar=no, menubar=no, resizable=no, location=no, directories=no, status=no');" />

obviously you'd have your other form attributes as well, but i left them out so you could see what i changed

 

newspreview.php

$n = intval($_GET['entry_number']);

change $_POST->$_GET (you should have some further validation as well [intval] and perhaps an if(entry exists) thing.

 

 

So basically the error is because you're not actually posting the data to the new window.  I don't know how to get it to do that with javascript, but perhaps someone else knows.

-edit: after looking it up, i've read a few people saying it's not possible to POST, but you can use GET above.

Link to comment
Share on other sites

not to be annoying but you probably should upgrade your site to running off of a database, it is much more secure than the xml method (if thats what you're doing)

 

I agree with you about running things off a database instead of running off of XML files--which I will definitely consider for the next site I make.  As for now, I am going to stick to the XML method until I've worked it through.

 

As for xtopolis, I will try your changes in a couple hours and see how that fixes the results.

 

Thanks guys.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.