A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: My SECOND PHP Page! This will be EASY!!!

  1. #1
    Senior Member
    Join Date
    Jan 2001
    Location
    Location: Location
    Posts
    147

    My SECOND PHP Page! This will be EASY!!!

    Hi There--

    I'm crawling through a tutorial on PHP that is supposed to make a page editable by a user. I need this to make it easy for my client to add/remove text.

    I was able to get a php page to display text. (My first tutorial!) In this one, I get the following error message:

    Parse error: parse error, expecting `','' or `';'' in [...] mypage.php on line 26.

    When the server is reading PHP, does it start line numbers from where you announce that PHP is happening after the html header or at the top of the page?

    Everywhere where it might be line 26 seems to have one of those punctuations.

    If you have eagle magic coding eyes, would you mind having a look at this so I can see what I'm missing?

    Thanks,

    Lee

    [PHP]
    <html>
    <head><title>My editable page</title></head>
    <body bgcolor="#cccccc" text="#000000" link="#000000" alink="#000000" vlink="#000000">
    <p><font face="verdana, arial, helvetica" size=2>
    <?PHP

    if ($_POST['pw']!="") {$pw=$_POST['pw'];}else{$pw=$_GET['pw'];}
    $newcontent=$_POST['newcontent'];
    $filelocation = "mytext.txt";
    if (!file_exists($filelocation)) {
    echo "Couldn't find datafile, please contact administrator!";
    }
    else {
    $newfile = fopen($filelocation,"r");
    $content = fread($newfile, filesize($filelocation));
    fclose($newfile);
    }
    $content = stripslashes($content);
    $content = htmlentities($content);
    $pass="password";
    if (!$pw || $pw != $pass){
    $content = nl2br($content);
    echo $content;
    }
    else {
    if ($newcontent){
    $newcontent = stripslashes($newcontent);
    $newfile = fopen($filelocation,"w");
    fwrite($newfile, $newcontent);
    fclose($newfile);
    echo 'Text was edited.<form><input type="submit" value="see changes" /></form>';
    }
    else{
    echo '<form method="post"> <textarea name="newcontent" cols="50" rows="15" wrap="virtual">';
    echo $content;
    echo "</textarea><input type="hidden" name="pw" value="'.$pass.'" /> <br /><input type="submit" value="edit" /></form>';
    }
    }

    ?>
    </font></p>
    <p>&nbsp;</p>
    <p>&nbsp; </p>
    </body>
    </html>
    [PHP]

    (The text page, mytext.txt is sitting on my server with permissions 666, like it said to in the tutorial. )
    "It's kind of fun to do the impossible."
    - Walt Disney

  2. #2
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi,

    A double quote instead of a single quote.
    PHP Code:
    echo '</textarea><input type="hidden" name="pw" value="'.$pass.'" /> <br /><input type="submit" value="edit" /></form>'

    (you need to close the php tag with a slash / so that the forum parses your code)
    Last edited by nunomira; 09-05-2004 at 05:36 PM.

  3. #3
    Senior Member
    Join Date
    Jan 2001
    Location
    Location: Location
    Posts
    147
    Thanks!

    I appreciate your help and your keen eyesight.

    So, does this mean that when the server error comes up with a line number, it refers to the line number of where the code begins?

    Line 1 would be the first 'if' statement?

    Due to your sharp acumen, I no longer get the error message. However, I don't get the editable pages yet either.

    Is there a way, when you're troubleshooting code, that you look at things? By this I mean, do you do a pass where you check for semicolons, another pass for open and closing quotes, that kind of thing? I don't know the first things about the disciplines of coding. I muddle through, but I'm really curious about the process and being smart about it. With this, and actionscripting, I'm wondering how to be more efficient.

    The text in the textfile displays, but I can't edit it. It's not like the one in the example. I've been through it line by line, and I haven't a clue what I could be doing wrong or what to look for. I am grateful for any advice here.

    The link to the tutorial is below. It's short, only one page.

    http://www.onlinetools.org/articles/...epages_all.php

    The link to my sandbox is here:

    http://www.leevodra.com/fullTextEdit.php/mypage3.php

    Thanks again,

    Lee

    Here's my code again.

    PHP Code:
    <html>
    <head><title>My editable page</title></head>
    <body bgcolor="#cccccc" text="#000000" link="#000000" alink="#000000" vlink="#000000">
    <p><font face="verdana, arial, helvetica" size=2> 
    <?PHP 

    if ($_POST['pw']!="") {$pw=$_POST['pw'];}else{$pw=$_GET['pw'];}
    $newcontent=$_POST['newcontent'];
    $filelocation "mytext.txt";
    if (!
    file_exists($filelocation)) {
    echo 
    "Couldn't find datafile, please contact administrator!";
    }
    else {
    $newfile fopen($filelocation,"r");
    $content fread($newfilefilesize($filelocation));
    fclose($newfile);
    }
    $content stripslashes($content);
    $content htmlentities($content);
    $pass="password";
    if (!
    $pw || $pw != $pass){
    $content nl2br($content);
    echo 
    $content;
    }
    else {
    if (
    $newcontent){
    $newcontent stripslashes($newcontent);
    $newfile fopen($filelocation,"w");
    fwrite($newfile$newcontent);
    fclose($newfile);
    echo 
    'Text was edited.<form><input type="submit" value="see changes" /></form>';
    }
    else{
    echo 
    '<form method="post"> <textarea name="newcontent" cols="50" rows="15" wrap="virtual">';
    echo 
    $content;
    echo 
    '</textarea><input type="hidden" name="pw" value="'.$pass.'" /> <br /><input type="submit" value="edit" /></form>';
    }
    }

    ?>
    </font></p>
    <p>_</p>
    <p>_ </p>
    </body>
    </html>
    "It's kind of fun to do the impossible."
    - Walt Disney

  4. #4
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    Well, I have Studio MX 2004 which besdes Flash MX 2004, includes Dreamweaver MX 2004.
    This is a great tool for developing HTML but other lnguages/scripts as weill: PHP, ColdFusion, CSS, XML,...
    In this case it was DW's colored code that helped me to find the error (and its line numbers as well).
    There are other tools out there, and free ones as well... search.

    The exact line depends on the code and the kind of error, but the line numbers in DW are always turned on!

    The code works fine!
    Go check your text file!


    Notice that the the code requests a variable $pw, which you have to passw via POST or GET (once again DW alows you to test this easily with live data view). To test it using GET is also easy outside DW, just append the variable/value pair to the url:
    Code:
    somefile.php?pw=password

  5. #5
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    Also, if you're intested in a similar example using flash and php, I suggest you read write to text file

    Also, for more help regarding PHP, phpbuilder is great!

  6. #6
    Senior Member
    Join Date
    Jan 2001
    Location
    Location: Location
    Posts
    147
    Thanks Nuno!

    It shows on the page that you edited it! That's very cool.

    It works if I append to the URL what you showed.

    You are soooooo cool! Thank you.

    I'm going to work through the tutorial you sent me. It looks excellent! Thanks. I'll also check out that link.

    I have Dreamweaver. I found a PHP tutorial for it on Macromedia.com - to build a Record Store. Unfortunately, I've spent the last five days trying to get MySQL to work on my OS X machine, in order to do the tutorial, but have been painfully unsuccessful with the install. So I'm sorta stuck when it comes to using Dreamweaver for PHP for now.

    But I've ordered a book on PHP and Dreamweaver and hope that I'll be able to at least start with that until I can figure out the MySQL install.

    You are beyond cool!

    Obrigado,

    Lee
    "It's kind of fun to do the impossible."
    - Walt Disney

  7. #7
    Senior Member
    Join Date
    Jan 2001
    Location
    Location: Location
    Posts
    147
    Nuno!

    May I beg another favor?

    I'm working on Flash MX, and I think the files from the wonderful looking tutorial you pointed me to are MX 2004 and they are crashing my Flash. Would you resave them in MX if they're saveable??

    You've been very generous. I'm reluctant to prevail again, but I'd be enormously grateful.

    I wish you the very best week ever. Thanks again for your help above.

    Lee
    Attached Files Attached Files
    "It's kind of fun to do the impossible."
    - Walt Disney

  8. #8
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    I guess that the files were saved for Flash MX.
    Wouldn't it be a Mac/Win thing?
    Isn't there something you have to do when opening a file from another platform? Maybe file->open?
    Attached Files Attached Files

  9. #9
    Senior Member
    Join Date
    Jan 2001
    Location
    Location: Location
    Posts
    147
    Thank you for trying.

    They open fine, so I don't think it's the Mac/Win thing. (I'm on a Mac and use the Flash typer that puts a tiny bit of code in the front of a file so it can be read if I can't get something to open. Didn't have to this time. Haven't had to in a while, come to think of it.)

    When either file opens, I can read the actionscript, but when I try to get the library to open or try to publish, or try to open the file with the Library open, Flash unexpectedly quits.

    I'm guessing now that it's not an MX/MX 2004 thing either, because the same thing happened when I tried to open the files that you saved as MX from 2004 for me. I really appreciate your trying that for me.

    It says to "move the button to the stage" in the commented code. When I try to open the library, it crashes. I tried copying the actionscript to another movie, then opening that one's library, and it didn't crash, so there's something going on with the file, and not Flash. (Yay!)

    The big mystery for me is whether there's any special code on the button. I'm going to check that out this morning by creating a button in another movie, naming its instance appropriately, and copying the actionscript and seeing if, by sheer luck, it works.

    I'd entertain any other suggestions. I really want to nail this tutorial.

    I wish you every good fortune. Thanks so much for your help,

    Lee
    Last edited by Capoeirista; 09-06-2004 at 09:54 AM.
    "It's kind of fun to do the impossible."
    - Walt Disney

  10. #10
    Senior Member
    Join Date
    Jan 2001
    Location
    Location: Location
    Posts
    147
    Nuno!

    Here's what I came up with.

    I used a standard button, movieclipped it, named it 'submit'. Went into the Linkage and chose 'export for actionscript'.

    When I published and threw everything in my server, a textbox appeared that I could write in, but no submit button.

    Is there anything special about the button?

    BTW, love your website.

    I hope you have a terrific day.

    Lee
    Attached Files Attached Files
    "It's kind of fun to do the impossible."
    - Walt Disney

  11. #11
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    I can't open your file


    I found out that when I converted my files from MX 2004 to MX, I lost the LoadVars Objects because I was using strict data typing:
    code:

    var lv_read:LoadVars = new LoadVars();
    var lv_write:LoadVars = new LoadVars();


    which isn't supported in MX, and the code got commented.
    This should be the reason why it doesn't work.

    You should have this uncommented code instead:
    code:

    var lv_read = new LoadVars();
    var lv_write = new LoadVars();


    Here are the latest versions.
    Attached Files Attached Files
    Last edited by nunomira; 09-06-2004 at 11:59 AM.

  12. #12
    Senior Member
    Join Date
    Jan 2001
    Location
    Location: Location
    Posts
    147
    Hi Nuno!

    If I put these files on my localhost, I get a textbox that says "an error occurred." Is this because I didn't change GET to POST on the last line of actionscript code?

    Also, in my text editor above, I'd like to be able to read the text as html into my page.

    If I put " This is plain text. <b>This is bold text.</b>

    It doesn't convert to HTML. It shows the tags. Why is that? Is there a way to fix it?

    You get another week of good karma points. I hope you have a great day,

    Lee
    "It's kind of fun to do the impossible."
    - Walt Disney

  13. #13
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    Although you can use GET in the final application, you should use POST.

    The test player for Flash MX there is a bug, as it always uses GET.
    In MX 2004, it works as expected.

    That's not a real problem because the php script contains:
    PHP Code:
    $myvar $_REQUEST['myvar']; 
    which works for both GET and POST

    (you could use $_GET or $_POST instead of $_REQUEST, in case you were using GET or POST respectively in Flash)

    Make sure you have the correct url to yor local server, correct file names, ...



    The html doesn't show up for security reasons.
    But there are ways to show some tags, or even every... which isn't advisible.
    This happens because of htmlentities .
    If you comment that line, the html will display.
    A classic example of what a user could do is writing (in this case, not harmful but boring):
    Code:
    <script language="JavaScript" type="text/JavaScript">alert('hello');</script>
    To allow some tags, and get rid of all the others: strip_tags.

  14. #14
    Senior Member
    Join Date
    Jan 2001
    Location
    Location: Location
    Posts
    147
    Nuno!

    You are a PHP Deity!

    Commenting the code did the trick! I've now stuck everything together in one folder so that the flash app and the php page read the same textfile and they can be edited from the php page. WooHoo!!!!!!! Break out the champagne!

    With your example 1, I am able to get it to read the textfile, and display the submit button, but not write to the file. When I release the submit button, I get the error message: "Cannot open file", which is on your writetofile.php page. Everything's in the same folder. The permissions are read and write (666) across the board for the text file, test.txt.

    If I need to use this script in the future, and the files aren't in the same folder, where it says $filename = 'test.txt'; should I put in the whole path, or would that be somewhere different?

    Is $handle a variable already defined in PHP, but you're redefining it here? I'm a little confused by defining a variable with a negative statement. Is this common? I admit to being completely clueless.

    Code:
    if (!$handle = fopen($filename, 'w')) {
    
    	 echo "&message=Cannot open file&";
    
    	 exit;
    
    }
    I'm reading this as if it means: if the variable can't open the file, echo 'can't open the file'. However the structure for me is confusing. If not the variable $handle equals fopen of test.txt for writing...

    Or should I read this as if it were thus: If the variable $handle is unsuccessful at opening the textfile for writing, then echo. . . 'Cannot open file'.

    I'm not really understanding what $handle actually _is_. Is it the true or false return of the success of opening the file? So that !$handle = true if opening the file is unsuccessful? And you're defining $handle in the same statement?

    This is so cool! It's like a giant logic puzzle. I'm really enjoying myself.

    Thanks again for your help!

    Lee

    Here's the whole PHP:
    PHP Code:
    <?php

    // the name of the text file

    $filename 'test.txt';



    // get the variables from flash

    $somecontent $_REQUEST['somecontent'];



    // If the file doesn't exist, attempt to create it

    // and open it for writing

    // anything already writen will be replaced

    if (!$handle fopen($filename'w')) {

         echo 
    "&message=Cannot open file&";

         exit;

    }



    // Write $somecontent to the opened file.

    if (fwrite($handle$somecontent) === FALSE) {

        echo 
    "&message=Cannot write to file&";

        exit;

    }

    // close the file

    fclose($handle);



    // send the variable to flash

    echo "&message=$somecontent&";



    ?>


    Here's the actionscript.
    Code:
    // 
    // place the button on the stage
    // hide and position it
    // 
    // 
    this.attachMovie("submit", "submit_btn", 0);
    submit_btn._visible = false;
    submit_btn._y = 100;
    submit_btn._x = 380;
    // 
    // create the input text field
    // 
    this.createTextField("content_txt", 1, 0, 0, 400, 80);
    content_txt.border = true;
    content_txt.type = "input";
    content_txt.wordWrap = true;
    content_txt.multiline = true;
    // 
    // create the LoadVars Objects
    // 
    var read_lv = new LoadVars();
    var write_lv = new LoadVars();
    // 
    // read the content of the text file
    // 
    read_lv.onLoad = function(success)
    {
    	if (success) {
    		content_txt.text = this.message;
    		submit_btn._visible = true;
    	} else {
    		content_txt.text = "an error occurred";
    	}
    };
    read_lv.load("http://localhost/leeSites/writetofile_examples/1/readfile.php");
    // send the variable to the php script
    submit_btn.onRelease = function()
    {
    	// text to write in the text file:
    	write_lv.somecontent = content_txt.text;
    	write_lv.onLoad = function(success)
    	{
    		if (success) {
    			trace("text writen to the text file:\n" + this.message);
    			content_txt.text = this.message;
    		} else {
    			trace("an error occurred");
    		}
    	};
    	// GET is for testing purposes only (in the test player) - use POST instead
    	write_lv.sendAndLoad("http://localhost/leeSites/writetofile_examples/1/writetofile.php", write_lv, "POST");
    };
    "It's kind of fun to do the impossible."
    - Walt Disney

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center