0

Newbie in Servers
The PHP script in the action attribute get downloaded each time i submit the form.

<form action="action.php" method="POST">

The form is in register.html . Files action.php and register.html are both in the same directory /var/www/html which is my current root directory after I installed lamp server. I have another php file containing phpinfo() function which gets executed properly but action.php doesn't work.

My action.php script:

<?php

    echo $_POST['name'];

?>

My register.html file:

<!DOCTYPE html>

<html>

    <head>
        <title>A Simple form</title>
    </head> 

    <body>

        <form action="action.php" method="POST">

            Name:<br>
            <input type="text" name="name" ><br>
            Password:<br>
            <input type="password" name="password"><br>
            <input type="submit" value="Submit"><br>

        </form> 


    </body>

</html>
ngrashia
  • 9,869
  • 5
  • 43
  • 58
suraj bora
  • 61
  • 1
  • 11
  • 1
    Possible duplicate: http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-i-can-see-it-on-source-code-of-page – Ali Aug 20 '15 at 09:52
  • @ali i already mentioned that a php file with phpinfo() function in the same directory is executing properly but action.php is not. – suraj bora Aug 20 '15 at 11:48
  • What platform are you on? Did you check the permissions on the files are identical if you are on a *nix system? – Ali Aug 21 '15 at 13:24
  • @Ali I am using linux mint 17 and have installed LAMP server. – suraj bora Aug 21 '15 at 13:27
  • Okay, I would compare permissions and owner/group between php files that work and don't work – Ali Aug 21 '15 at 13:30
  • Both the files( action.php and the file whiach contains phpinfo() ) have same permissions. – suraj bora Aug 24 '15 at 15:44
  • if you go to action.php by putting the url in the browser instead of submitting the form does it execute or download? – Ali Aug 25 '15 at 14:58
  • @Ali fortunately it executes :) – suraj bora Aug 27 '15 at 13:21
  • So in your `form action="action.php"` can you use the full URL or use `/action.php` and see if it works then. – Ali Aug 27 '15 at 13:51
  • I used the full address of action.php. It still gets downloaded. When i open register.html in the browser , the url shows file:///var/www/html/register.html . Shouldn't it show localhost/... ? – suraj bora Aug 28 '15 at 16:44
  • Yes thats the problem. You need to open register.html by going to the apache url `http://localhost/ ... /register.html`. Do not open register.html by double-clicking the file – Ali Aug 30 '15 at 11:38

1 Answers1

1

OP is opening register.html by double clicking the file, which leads to the file being opened by the browser with the file:// protocol, instead of the http:// protocol. Once opened with the file:// protocol, the OS handles all form submissions and consequently action.php is also opened by the OS over the file:// protocol. Apache is not involved and hence the issue.

To fix the issue, the initial file register.html (or index.html) must be opened over http://localhost/ ... /register.html by manually entering that url in the browser. This will now be served by Apache and any subsequent form submission and navigation will continue to be served by Apache, interpreting any PHP on the server-side in the process.

Ali
  • 1,462
  • 2
  • 17
  • 32
  • I opened register.html with localhost/register.html but after submitting I get 404 ( The requested URL /var/www/html/action.php was not found on this server. ) even though action.php is in same directory( /var/www/html/ ) as register.html . – suraj bora Sep 01 '15 at 04:13
  • Why is your URL `/var/www/html/action.php`? Can you update the original question with the latest version of the `register.html` file and the URL you are typing in the browser to get to it. – Ali Sep 01 '15 at 09:30
  • It's done. Will ask for help if something bad occurs. Thank you a lot for bearing it with me. :) – suraj bora Sep 01 '15 at 13:42