0

First of all, I’m new to web dev, and I’ve also never used PHP before, so please bear with me and my ignorance.

I’m working on a website, and part of it has an HTML form used to send an email. After some Google searching, I found that a simple PHP script would be the easiest way to do it.

Here’s what I have in my HTML file:

<form method="post" name="myemailform" action="form-to-email.php">
    <fieldset>
        <legend>Contact us</legend>
        <div>
            <label for="name">Name: </label>
            <input name="name" id="name">
        </div>
        <div>
            <label for="email">Email: </label>
            <input name="email" id="email">
        </div>
        <div>
            <label for="message">Message: </label>
            <textarea rows="10" cols="40" name="message" id="message"></textarea>
            <input type="submit" value="Send Email" /> 
        </div>
    </fieldset>
</form>

I’m testing the file locally and for some reason, when I click the submit button, instead of running the script, a new tab opens up displaying the contents of the .php file in its entirety.

Can anybody please explain to me why this is happening, and what can I do to fix it? Any help would be appreciated!

Edit: Thanks guys! I didn’t know I needed a local server, and I didn’t know that PHP was one of those things I’d need to install. This is really awkward.... Well now I know! Thanks!

Sharanya Dutta
  • 3,981
  • 2
  • 17
  • 27
danharel
  • 15
  • 1
  • 4

5 Answers5

1

The only way the PHP would run is if you were viewing the whole thing on a webserver.

If this is a local installation, using XAMPP might be the way to go. This is a bundle including Apache (the web server), MySQL (the database system) and PHP, the dynamic programming language.

If you are directly viewing any PHP script in your browser, it will display the raw PHP code. If you want the actual result to come up, you will need to have a webserver, like the XAMPP bundle (link above).

Also, if you are using Mac, MAMP is an alternative.

Lucas
  • 16,930
  • 31
  • 110
  • 182
1

I'm testing the file locally

Do you have Apache and PHP installed locally?

If you do, you have to make sure Apache is configured to execute PHP.

Community
  • 1
  • 1
ryanwinchester
  • 11,737
  • 5
  • 27
  • 45
1

Normally you can’t see .php file content when you open it with a browser. You actually see HTML, translated from PHP.

There are three reasons for your problem. If you did not close opening tags, brackets etc, balance them. Another is, if it is an action page your .php file should be only script, there is no need for HTML content.

Also if you’re working on localhost you MUST install and configure Apache and PHP. There are many tools for this like WAMP, XAMP, LAMP which you can download and install easily. Then your PHP file must be in your local server.

Sharanya Dutta
  • 3,981
  • 2
  • 17
  • 27
hakki
  • 6,181
  • 6
  • 62
  • 106
1

First of all check for <?php at the start of the php script and ?> at the end but what I think is likely the problem is that you don't have php installed. How to do that will vary depending on your platform but regardless there is a ton of info out there on how to install php.

You should also note that you need to be using a webserver application such as Apache, IIS, Nginx etc.... to run the code so if your just opening your php file with a web browser that's not going to work as there is nothing to actually execute it. Make sure that your url starts with http:// or https:// and not file:///

nettux
  • 5,270
  • 2
  • 23
  • 33
0

You should use a local server.

I personally recommend Wamp Server