$_POST does not work

Discussion in 'PHP' started by gullyman, Feb 22, 2007.

  1. #1
    Hello-
    I am trying to use php and flash to write a xml file to my local file system.
    My php code looks like this:
    <?php
    $newline = "\n";
    echo "starting... \n";
    $file = $_POST['xfile'];
    $input = $_POST['input'];
    $handle;
    echo  "input is $input\n";
    echo  "xfile is $file\n";
    $path="..\\XML\\";
    $fullpath = $path.$file;
    // Let's make sure the file exists and is writable first.
    if (file_exists($fullpath)) {
    
        if (!$handle = fopen($fullpath, 'w')) {
             echo "Cannot open file ($fullpath)";
    		 print "&receivedData=Cannot open file..";
             exit;
        }
    
        // Write $input to our opened file.
        if (fwrite($handle, $input) === FALSE) {
            echo "Cannot write to file ($fullpath)";
            exit;
        }
    
        echo "Success, wrote ($input) to file ($fullpath)";
    	print("&receivedData=New File written");
        fclose($handle);
    
    } else {
        echo "The file $fullpath is not writable";
    	print "&receivedData=File does not exist";
    }
    ?>
    PHP:
    My flash script looks like this:
    function WriteXML(inputstring:String){
    //getURL( phploc+"writeRoomLayout.php", "_blank" );
    var SubmitLoadVars:LoadVars= new LoadVars();
    SubmitLoadVars.xfile = "roomLayout.xml";
    SubmitLoadVars.input= inputstring;
    trace("SubmitLoadVars.xfile = "+SubmitLoadVars.xfile);
    trace("phploc= "+phploc+"writeRoomLayout.php");
    trueorfalse=SubmitLoadVars.sendAndLoad(phploc+"writeRoomLayout.php",SubmitLoadVars,"POST");
    trace("trueorfalse= "+trueorfalse);
    SubmitLoadVars.onLoad = function(success:Boolean){
    if(success){
    trace("result= "+this.receivedData);
    }
    };
    }
    Code (markup):
    When I view the output in my browser I get this:
    starting ... input is xfile is Cannot open file ../XML/.
    What could be wrong?
    Why does the php not register the posted data?
    Please help if you can.
    Could it be my PHP.INI file?
    Thanks for reading.
    gullyman.
     
    gullyman, Feb 22, 2007 IP
  2. SilkySmooth

    SilkySmooth Well-Known Member

    Messages:
    1,583
    Likes Received:
    269
    Best Answers:
    0
    Trophy Points:
    180
    #2
    What does the form code look like?
     
    SilkySmooth, Feb 22, 2007 IP
  3. gullyman

    gullyman Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm not sure I know what you mean by the "form code"
     
    gullyman, Feb 23, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    It's a flash form. He posted the code.
     
    nico_swd, Feb 23, 2007 IP
  5. DeViAnThans3

    DeViAnThans3 Peon

    Messages:
    785
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'm not yet very familiar with Flash (actionscript), but it is clear, like you state, that $_POST isn't working .. or at least $_POST['xfile'].

    I'ld first like to debug the $_POST handler. Do this like this, and test again .. then let me know.

    Find:
    <?php
    PHP:
    After, add: (at new line preferred)
    
    // temporary debug code
    foreach($HTTP_POST_VARS as $n => $v)
    {
    echo "\$_POST['$n'] value: $v";
    }
    die();
    // end temporary debug code
    
    PHP:
    Please tell us what the script put out :)
     
    DeViAnThans3, Feb 23, 2007 IP
  6. gullyman

    gullyman Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi- My power went out so I was not able to reply until now...
    My php script now looks like this:
    <?php
    // temporary debug code
    foreach($HTTP_POST_VARS as $n => $v)
    {
    echo "\$_POST['$n'] value: $v";
    }
    die();
    // end temporary debug code 
    $newline = "\n";
    echo "starting... \n";
    echo  $_POST['xfile'];
    $file = $_POST['xfile'];
    $input = $_POST['input'];
    $handle;
    echo  "\ninput is $input\n";
    echo  "\nxfile is $file\n";
    $path="..\\XML\\";
    $fullpath = $path.$file;
    // Let's make sure the file exists and is writable first.
    if (file_exists($fullpath)) {
    
        if (!$handle = fopen($fullpath, 'w')) {
             echo "Cannot open file ($fullpath)";
    		 print "&receivedData=Cannot open file..";
             exit;
        }
    
        // Write $input to our opened file.
        if (fwrite($handle, $input) === FALSE) {
            echo "Cannot write to file ($fullpath)";
            exit;
        }
    
        echo "Success, wrote ($input) to file ($fullpath)";
    	print("&receivedData=New File written");
        fclose($handle);
    
    } else {
        echo "The file $fullpath is not writable";
    	print "&receivedData=File does not exist";
    }
    ?>
    PHP:
    I added the script you suggested and this is what I got:

    Notice: Undefined index: xfile in C:\Apache\Apache2\htdocs\APA\PHPFiles\writeRoomLayout.php on line 4

    Notice: Undefined index: xfile in C:\Apache\Apache2\htdocs\APA\PHPFiles\writeRoomLayout.php on line 5

    Notice: Undefined index: input in C:\Apache\Apache2\htdocs\APA\PHPFiles\writeRoomLayout.php on line 6
    input is xfile is
    Warning: fopen(..\XML\) [function.fopen]: failed to open stream: Permission denied in C:\Apache\Apache2\htdocs\APA\PHPFiles\writeRoomLayout.php on line 15
    Cannot open file (..\XML\)&receivedData=Cannot open file.


    This is valuable debugging code Thanks a lot!
    But where do I go from here?
     
    gullyman, Feb 23, 2007 IP
  7. gullyman

    gullyman Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Latest development
    Now my output reads:
    Notice: Undefined variable: HTTP_POST_VARS in C:\Apache\Apache2\htdocs\APA\PHPFiles\writeRoomLayout.php on line 3

    Warning: Invalid argument supplied for foreach() in C:\Apache\Apache2\htdocs\APA\PHPFiles\writeRoomLayout.php on line 3

    There is something seriously wrong here.
    Could it be my php.ini file?
    Or maybe my htpd.conf file in Apache?
    I'm drowning over here:eek:
     
    gullyman, Feb 23, 2007 IP
  8. DeViAnThans3

    DeViAnThans3 Peon

    Messages:
    785
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    0
    #8
    It clearly seems $HTTP_POST_VARS is not at all passed indeed :)
    Well, could you check php.ini and try to find
    register_globals = 
    Code (markup):
    If there is "off" after, replace it with On, so that you get
    register_globals = On
    Code (markup):
    Restart apache. Let me know if this helped :)
     
    DeViAnThans3, Feb 23, 2007 IP
  9. gullyman

    gullyman Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Well I tried that and there is still no change.
    Maybe I need to try to re-install Apache and php?
    Although I've done that several times...
    Is there a version conflict with Apache 2.0.50 and PHP 5.2.1 that could be the problem?
     
    gullyman, Feb 23, 2007 IP
  10. SilkySmooth

    SilkySmooth Well-Known Member

    Messages:
    1,583
    Likes Received:
    269
    Best Answers:
    0
    Trophy Points:
    180
    #10
    Switching register globals to on, is not the solution. I am going to re-read this thread and then respond with what you should do next.
     
    SilkySmooth, Feb 23, 2007 IP
  11. SilkySmooth

    SilkySmooth Well-Known Member

    Messages:
    1,583
    Likes Received:
    269
    Best Answers:
    0
    Trophy Points:
    180
    #11
    gullyman,

    I very much doubt that there is a conflict or problem with your installations, if there were you would be having more trouble than just passing variables through a post.

    To debunk this, lets take this back to basics. Please put the following into a blank PHP file, call it whatever you want. Up it to your server and request it in a browser, submit the form and tell me what the output is.

    
    <?php
    if (isset($_POST['click']))
       {
       echo "You sent: <b>".$_POST['data']."</b><br />";
       }
    ?>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <input type="text" name="data" value="" /><br />
    <input type="submit" name="click" value="click me" />
    </form>
    
    Code (markup):
     
    SilkySmooth, Feb 23, 2007 IP
  12. gullyman

    gullyman Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Well I did what you asked ,Silky, and it works.. I get a textbox and a button
    and when I enter text into the textfield and click the button my text appears
    after the words "You sent".
    This is hopeful.
    -G
     
    gullyman, Feb 24, 2007 IP
  13. DeViAnThans3

    DeViAnThans3 Peon

    Messages:
    785
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Hmmm, very strange. Which server are you using? Apache? IIS? ..
    It might also be just due to PHP, but I doubt that more and more. Maybe SilkySmooth still has a solution? :)
     
    DeViAnThans3, Feb 24, 2007 IP
  14. SilkySmooth

    SilkySmooth Well-Known Member

    Messages:
    1,583
    Likes Received:
    269
    Best Answers:
    0
    Trophy Points:
    180
    #14
    Ok, based on the test you have just done for me, we now know that there is nothing wrong with the PHP installation or the PHP settings in the php.ini, otherwise the test would have given you an error or produced no output.

    Now, I have read over the original PHP script and can find no obvious problems, the script should at least echo the posted data as per the test you have just run.

    Therefore, logically, the problem must be in the flash script somewhere. Unfortunately whilst I can tell you pretty much anything about PHP, my flash scripting is no where near as good.

    I will do some Googling and come back in a little while.
     
    SilkySmooth, Feb 24, 2007 IP
  15. SilkySmooth

    SilkySmooth Well-Known Member

    Messages:
    1,583
    Likes Received:
    269
    Best Answers:
    0
    Trophy Points:
    180
    #15
    SilkySmooth, Feb 24, 2007 IP
  16. SilkySmooth

    SilkySmooth Well-Known Member

    Messages:
    1,583
    Likes Received:
    269
    Best Answers:
    0
    Trophy Points:
    180
    #16
    Also note that in these examples it looks like they are declaring a datatype, ala:

    
    dataOut.password = password.text;
    
    Code (markup):
    Another suggestion would be to append the .text to see if that pulls in the data.
     
    SilkySmooth, Feb 24, 2007 IP
  17. gullyman

    gullyman Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    I think you are right the problem is probably in the flash form.
    Thank you all for your help It's much appreciated.
    -gullyman
     
    gullyman, Feb 24, 2007 IP