PHPNoob_BC Posted May 26, 2008 Share Posted May 26, 2008 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); ?> Quote Link to comment Share on other sites More sharing options...
technotool Posted May 26, 2008 Share Posted May 26, 2008 what is the error or output message that you are getting. You need to LOOP(for, while, etc...) through the data in the flash file. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted May 26, 2008 Share Posted May 26, 2008 $default_dir *might* need to be a full path more like: $default_dir = $_SERVER['DOCUMENT_ROOT'].'/guestbook/'; and it will need to be writable. Quote Link to comment Share on other sites More sharing options...
PHPNoob_BC Posted May 26, 2008 Author Share Posted May 26, 2008 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 Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted May 26, 2008 Share Posted May 26, 2008 Parse error: syntax error, unexpected $end in /Users/lesbrown/Sites/site1/test.php on line 32 you're missing the closing bracket for your if Quote Link to comment Share on other sites More sharing options...
PHPNoob_BC Posted May 26, 2008 Author Share Posted May 26, 2008 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? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted May 26, 2008 Share Posted May 26, 2008 are these set somewhere before the piece of code you posted? if not, that's your problem. // may need to be if(isset($_POST['name']) and isset($_POST['email']) and isset($_POST['message'])){ also, is flash POST'ing or GET'ing? Quote Link to comment Share on other sites More sharing options...
PHPNoob_BC Posted May 26, 2008 Author Share Posted May 26, 2008 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. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted May 26, 2008 Share Posted May 26, 2008 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."; Quote Link to comment Share on other sites More sharing options...
PHPNoob_BC Posted May 26, 2008 Author Share Posted May 26, 2008 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 Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted May 26, 2008 Share Posted May 26, 2008 in fopen, try using a (lower case "A") instead of w+ Quote Link to comment Share on other sites More sharing options...
PHPNoob_BC Posted May 26, 2008 Author Share Posted May 26, 2008 BlueSkyIS - everything works amazingly well now, thank you so much! I really appreciate it! -BC Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.