Why isn't my form handler running?

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='[email protected]';
    $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";
    }
}

?>

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.

the file is saved as ‘send.php’

Post the form code please.

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>

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.

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/

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

        $to='[email protected]';
        $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";
        }
    }

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

Would this be the request method?

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

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

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.
}
1 Like

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

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

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='[email protected]';
    $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";
    }
}
<?php

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

    $to='[email protected]';
    $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.";
}

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

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

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.)

Yes I am using XAMPP

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

type 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

Sponsor our Newsletter | Privacy Policy | Terms of Service