Jump to content

[SOLVED] Php writing to xml - not working


PHPNoob_BC

Recommended Posts

Hi All,

I am very new to php and am trying to write a simple php script which takes the variables from my flash guestbook form and write it into an xml file. The problem that I am having is that I can't get the php script to write the data into an xml file. Any help is greatly appreciated. Thanks,

-BC

 

<?PHP
// read the variables from flash
if(isset($name) and isset($email) and isset($message)){
echo "Guestbook Data Posted";
$name = $_POST['yourname'];
$email = $_POST['youremail'];
$message = $_POST['message'];
$ipaddress = "Sender's IP Address: " . $_SERVER['REMOTE_ADDR'];
$xml_dec = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";	
// hold all data
    $rootELementStart = "<guestbook>";
    $rootELementEnd = "</guestbook>";
    $xml_doc=  $xml_dec;
    $xml_doc .=  $rootELementStart;
    $xml_doc .=  "<name>";
    $xml_doc .=  $name;
    $xml_doc .=  "</name>";
    $xml_doc .=  "<ipaddress>";
    $xml_doc .= $ipaddress;
    $xml_doc .=  "</ipaddress>";
    $xml_doc .=  "<emailaddress>";
    $xml_doc .=  $email;
    $xml_doc .=  "</emailaddress>";
    $xml_doc .=  "<message>";
    $xml_doc .=  $message;
    $xml_doc .=  "</message>";
    $xml_doc .=  $rootElementEnd;
    $default_dir = "/guestbook/";
    $default_dir .=  "guestbook.xml";	
// write user data
$fp = fopen($default_dir,'w+');
		$write = fwrite($fp,$xml_doc);
?>

Link to comment
https://forums.phpfreaks.com/topic/107339-solved-php-writing-to-xml-not-working/
Share on other sites

Technotool - the error I am getting is "Parse error: parse error, unexpected $ in /home/content/b/r/i/brianjamescook/html/bjcmedia/guestbook/guestbooktry2.php on line 33" however the only code on line 33 is the closing of the php script "?>"

 

BlueSkyIS - I have tried to change the line you have suggested but it hasn't solved the problem

BlueSkyIS - that fixed the parse error but unfortunately it is still not writing. I have even made the whole directory read, write, executable too.

 

Am I correct in thinking that even if the data is coming in wrong from the flash form that the ip address should no matter what still be written to the guestbook.xml file, so there is still an issue in the php script?

BlueSkyIS - my total php script is this:

 

 <?PHP
// read the variables from flash
if(isset($_POST['yourname']) and isset($_POST['youremail']) and isset($_POST['yourmessage'])){
echo "Guestbook Data Posted";
$name = $_POST['yourname'];
$email = $_POST['youremail'];
$message = $_POST['message'];
$ipaddress = "Sender's IP Address: " . $_SERVER['REMOTE_ADDR'];
$xml_dec = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
}	
// hold all data
    $rootELementStart = "<guestbook>";
    $rootELementEnd = "</guestbook>";
    $xml_doc=  $xml_dec;
    $xml_doc .=  $rootELementStart;
    $xml_doc .=  "<name>";
    $xml_doc .=  $name;
    $xml_doc .=  "</name>";
    $xml_doc .=  "<ipaddress>";
    $xml_doc .= $ipaddress;
    $xml_doc .=  "</ipaddress>";
    $xml_doc .=  "<emailaddress>";
    $xml_doc .=  $email;
    $xml_doc .=  "</emailaddress>";
    $xml_doc .=  "<message>";
    $xml_doc .=  $message;
    $xml_doc .=  "</message>";
    $xml_doc .=  $rootElementEnd;
    $default_dir = $_SERVER['DOCUMENT_ROOT'].'/guestbook/';
    $default_dir .=  "guestbook.xml";	
// write user data
$fp = fopen($default_dir,'w+');
		$write = fwrite($fp,$xml_doc);
?> 

 

and my flash swf file is POSTing the yourname, youremail, yourmessage data

 

 submit_btn.addEventListener(MouseEvent.CLICK, sendClicked);

function sendClicked (event:MouseEvent):void {
   var req:URLRequest = new URLRequest("/guestbook/guestbooktry2.php");
   var vars:URLVariables = new URLVariables();
   var loader:URLLoader = new URLLoader();
  
   vars.yourname = namebox_txt.text;
   vars.youremail = emailbox_txt.text;
   vars.yourmessage = messagebox_txt.text;
   
   req.method = URLRequestMethod.POST;
   req.data = vars; } 

 

thanks for not giving up on me.

okay, maybe we should back up and make sure you can write anything at all to a file. i would create a new script or alter yours to just do this, then open it in your browser instead of Flash and see if the file is written to:

 

    $default_dir = $_SERVER['DOCUMENT_ROOT'].'/guestbook/';
    $default_dir .=  "guestbook.xml";	
// write user data
    $fp = fopen($default_dir,'w+') or die('fopen failed');

    $str = 'hello world';

    $write = fwrite($fp,$str) or die('fwrite failed');

    echo "done.";

BlueSkyIS - I have figured out the problem. The problem was the way that I have the web hosting setup instead of /guestbook/ for the directory it is actually /bjcmedia/guestbook. I'm glad it now works but I feel like such an idiot! Thank you for all your help.

I have one last question now that the original guestbook script is working the only problem that I run into is that when I run the form twice the first stuff gets deleted and replaced with the newer stuff is there any way to go about fixing that so that it just appends to the top document instead of overwriting it? Thank you so much,

-BC

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.