# Why isn't my form handler running?

**URL:** <https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367>\
**Category:** Beginners - Learning PHP\
**Created:** [March 25, 2020, 3:45pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367 "2020-03-25T15:45:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![willjd](https://www.phphelp.com/letter_avatar_proxy/v4/letter/w/9fc29f/32.png) [@willjd](https://www.phphelp.com/u/willjd)\
**Post date:** [March 25, 2020, 3:45pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/1 "2020-03-25T15:45:17Z")

</div>

Hey everyone,

this is my first experience working with PHP to bear with me. I wrote a form handler to process data on my website and installed an apache server to run it on. Everything works fine until I submit the form, and the code either appears on the following page or displays a blank page. I’m not sure if this is an issue in my code or the way I stored it in htdocs. below is a snippet of my code:

```
<?php
if(isset($_POST['submit'])){
    $name=$_POST['name'];
    $lname=$_POST['lname'];
    $email=$_POST['email'];
    $phone=$_POST['phone'];
    $msg=$_POST['msg'];

    $to='me@gmail.com';
    $subject='Submission';
    $message="First Name: ".$name."\n". "Last Name: ".$lname."\n" ."Phone: "."\n". "Wrote the following: "."\n\n".$msg;
    $headers="From: ".$email;

    if (mail($to, $subject, $message, $headers)){
      echo "<h1>Thanks for reaching out!</h1>";
    }
    else{
      echo "Something went wrong";
    }
}

```

?\>

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [March 25, 2020, 4:06pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/2 "2020-03-25T16:06:38Z")

</div>

Code displaying usually means the file does not have a .php file extension, assuming that Php is actually installed and the file is running on a server.

---

<div class="post-metadata">

**Author:** ![willjd](https://www.phphelp.com/letter_avatar_proxy/v4/letter/w/9fc29f/32.png) [@willjd](https://www.phphelp.com/u/willjd)\
**Post date:** [March 25, 2020, 4:08pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/3 "2020-03-25T16:08:35Z")

</div>

the file is saved as ‘send.php’

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [March 25, 2020, 4:11pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/4 "2020-03-25T16:11:17Z")

</div>

Post the form code please.

---

<div class="post-metadata">

**Author:** ![willjd](https://www.phphelp.com/letter_avatar_proxy/v4/letter/w/9fc29f/32.png) [@willjd](https://www.phphelp.com/u/willjd)\
**Post date:** [March 25, 2020, 4:21pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/5 "2020-03-25T16:21:44Z")

</div>

Below is the form code, sorry for the excess bootstrap

```
 <form id="myForm" action="send.php" method="post" class="p-5 bg-white">

          <div class="row form-group">
            <div class="col-md-6 mb-3 mb-md-0">
              <label class="text-black" for="fname">First Name</label>
              <input type="text" id="fname" class="form-control rounded-0" name="name">
            </div>
            <div class="col-md-6">
              <label class="text-black" for="lname">Last Name</label>
              <input type="text" id="lname" class="form-control rounded-0" name="name-last">
            </div>
          </div>

          <div class="row form-group">
            
            <div class="col-md-12">
              <label class="text-black" for="email">Email</label> 
              <input type="email" id="email" class="form-control rounded-0" name="email">
            </div>
          </div>

          <div class="row form-group">
            
            <div class="col-md-12">
              <label class="text-black" for="number">Phone Number</label> 
              <input type="tel" id="clv-number" class="form-control rounded-0" name="phone">
            </div>
          </div>

          <div class="row form-group">
            <div class="col-md-12">
              <label class="text-black" for="message">Message</label> 
              <textarea name="msg" id="message" cols="30" rows="7" class="form-control rounded-0" placeholder="Tell us about your project..."></textarea>
            </div>
          </div>

          <div class="row form-group">
            <div class="col-md-12">
              <input type="submit" value="Send Message" class="btn btn-primary mr-2 mb-2">
            </div>
          </div>

        </form>
```

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [March 25, 2020, 4:54pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/6 "2020-03-25T16:54:54Z")

</div>

The main problem is that you are depending on the name of a button to be submitted in order for your script to work. This will completely fail in certain cases such as yours. You need to check the Request Method instead.

You are also sending a field named name-last that does not exist in your code.

---

<div class="post-metadata">

**Author:** ![willjd](https://www.phphelp.com/letter_avatar_proxy/v4/letter/w/9fc29f/32.png) [@willjd](https://www.phphelp.com/u/willjd)\
**Post date:** [March 25, 2020, 5:07pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/7 "2020-03-25T17:07:38Z")

</div>

Okay, I updated the name of the button and changed it in the request method but am still getting sent to a blank page. Is it something other than the name that should be changed? Also, thanks for pointing out the error in the last-name, I have updated that too.

[https://jsfiddle.net/Orthodocstation/Lpc4nzw8/4/](https://jsfiddle.net/Orthodocstation/Lpc4nzw8/4/)

```
if(isset($_POST['update'])){
        $name=$_POST['name'];
        $lname=$_POST['lname'];
        $email=$_POST['email'];
        $phone=$_POST['phone'];
        $msg=$_POST['msg'];

        $to='johnwal512@gmail.com';
        $subject='Deseinstein Submission';
        $message="First Name: ".$name."\n". "Last Name: ".$lname."\n" ."Phone: "."\n". "Wrote the following: "."\n\n".$msg;
        $headers="From: ".$email;

        if (mail($to, $subject, $message, $headers)){
          echo "<h1>Thanks for reaching out!</h1>";
        }
        else{
          echo "Something went wrong";
        }
    }
```

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [March 25, 2020, 5:35pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/8 "2020-03-25T17:35:48Z")

</div>

> [@willjd](#):
>
> I updated the name of the button

That isnt what I said to do. Where is the code showing the Request Method?

---

<div class="post-metadata">

**Author:** ![willjd](https://www.phphelp.com/letter_avatar_proxy/v4/letter/w/9fc29f/32.png) [@willjd](https://www.phphelp.com/u/willjd)\
**Post date:** [March 25, 2020, 5:49pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/9 "2020-03-25T17:49:44Z")

</div>

Would this be the request method?

```
<form id="myForm" action="send.php" method="post">
```

---

<div class="post-metadata">

**Author:** ![astonecipher](https://www.phphelp.com/user_avatar/www.phphelp.com/astonecipher/32/2797_2.png) [@astonecipher](https://www.phphelp.com/u/astonecipher)\
**Post date:** [March 25, 2020, 5:51pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/10 "2020-03-25T17:51:35Z")

</div>

> [@willjd](#):
>
> if(isset($\_POST[‘update’])){

What is being stated, your code depends on a specific button being in the transfer object. You don’t want to use that.

```auto
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    // a POST method or form submission sent the user here
} else {
    // you came here on your own or the form method is wrong.
}
```

---

<div class="post-metadata">

**Author:** ![willjd](https://www.phphelp.com/letter_avatar_proxy/v4/letter/w/9fc29f/32.png) [@willjd](https://www.phphelp.com/u/willjd)\
**Post date:** [March 25, 2020, 5:56pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/11 "2020-03-25T17:56:14Z")

</div>

Should I simply update the request method to the one you shared then?

---

<div class="post-metadata">

**Author:** ![astonecipher](https://www.phphelp.com/user_avatar/www.phphelp.com/astonecipher/32/2797_2.png) [@astonecipher](https://www.phphelp.com/u/astonecipher)\
**Post date:** [March 25, 2020, 6:00pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/12 "2020-03-25T18:00:29Z")

</div>

you need to change the if statement I quoted to the if line i added

---

<div class="post-metadata">

**Author:** ![willjd](https://www.phphelp.com/letter_avatar_proxy/v4/letter/w/9fc29f/32.png) [@willjd](https://www.phphelp.com/u/willjd)\
**Post date:** [March 25, 2020, 6:08pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/13 "2020-03-25T18:08:38Z")

</div>

I’m still getting a page with just the php text. This is how my code looks now:

```
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $name=$_POST['name'];
    $lname=$_POST['lname'];
    $email=$_POST['email'];
    $phone=$_POST['phone'];
    $msg=$_POST['msg'];

    $to='me@gmail.com';
    $subject='Submission';
    $message="First Name: ".$name."\n". "Last Name: ".$lname."\n" ."Phone: "."\n". "Wrote the following: "."\n\n".$msg;
    $headers="From: ".$email;

    if (mail($to, $subject, $message, $headers)){
      echo "<h1>Thanks for reaching out!</h1>";
    }
    else{
      echo "Something went wrong";
    }
}
```

---

<div class="post-metadata">

**Author:** ![astonecipher](https://www.phphelp.com/user_avatar/www.phphelp.com/astonecipher/32/2797_2.png) [@astonecipher](https://www.phphelp.com/u/astonecipher)\
**Post date:** [March 25, 2020, 6:10pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/14 "2020-03-25T18:10:17Z")

</div>

```auto
<?php

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $name=$_POST['name'];
    $lname=$_POST['lname'];
    $email=$_POST['email'];
    $phone=$_POST['phone'];
    $msg=$_POST['msg'];

    $to='johnwal512@gmail.com';
    $subject='Deseinstein Submission';
    $message="First Name: ".$name."\n". "Last Name: ".$lname."\n" ."Phone: "."\n". "Wrote the following: "."\n\n".$msg;
    $headers="From: ".$email;

    if (mail($to, $subject, $message, $headers)){
      echo "<h1>Thanks for reaching out!</h1>";
    }
    else{
      echo "Something went wrong";
    }
} else {
    echo "No direct connections.";
}
```

---

<div class="post-metadata">

**Author:** ![willjd](https://www.phphelp.com/letter_avatar_proxy/v4/letter/w/9fc29f/32.png) [@willjd](https://www.phphelp.com/u/willjd)\
**Post date:** [March 25, 2020, 6:24pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/16 "2020-03-25T18:24:01Z")

</div>

After updating the file I am still getting the code page after submitting.

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [March 25, 2020, 6:41pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/17 "2020-03-25T18:41:05Z")

</div>

> [@willjd](#):
>
> I’m still getting a page with just the php text.

Are you running this on a server? Do you have php installed on that server?

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [March 25, 2020, 7:08pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/18 "2020-03-25T19:08:39Z")

</div>

You are opening the form page (and therefore the form processing page) through the file system, not through a URL. The URL you use to open the form should be similar to - http:/localhost/deinproject/Einstein Design/send.php (if your form and form processing code are on separate pages, you need to put them both on the same page.)

---

<div class="post-metadata">

**Author:** ![willjd](https://www.phphelp.com/letter_avatar_proxy/v4/letter/w/9fc29f/32.png) [@willjd](https://www.phphelp.com/u/willjd)\
**Post date:** [March 25, 2020, 7:37pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/19 "2020-03-25T19:37:24Z")

</div>

Yes I am using XAMPP

---

<div class="post-metadata">

**Author:** ![willjd](https://www.phphelp.com/letter_avatar_proxy/v4/letter/w/9fc29f/32.png) [@willjd](https://www.phphelp.com/u/willjd)\
**Post date:** [March 25, 2020, 7:39pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/20 "2020-03-25T19:39:18Z")

</div>

Is there a way you could give me a visual representation?

---

<div class="post-metadata">

**Author:** ![frankbeen](https://www.phphelp.com/letter_avatar_proxy/v4/letter/f/f1d935/32.png) [@frankbeen](https://www.phphelp.com/u/frankbeen)\
**Post date:** [March 25, 2020, 8:10pm UTC](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367/21 "2020-03-25T20:10:10Z")

</div>

type [http://localhost/](http://localhost/) you should see your very own WAMP server.

If so you should read the documentation of WAMP and find out which directory the **DocumentRoot** is of your webserver. very likely something as C:\wamp64\htdocs. But i am not sure since i do not use WAMP. If you know where your DocumentRoot is that is also the place where you must store your php files. For example if C:\wamp64\htdocs is your DocumentRoot and you save a php file there which you call test.php then you should type in your in your browsers URL bar: [http://localhost/test.php](http://localhost/test.php)

[Next page](https://www.phphelp.com/t/why-isnt-my-form-handler-running/31367.md?page=2)
