skip to Main Content

I have the below php page, which is a holding page with a contact form:

    <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <meta name="author" content="">

    <!--<link rel="icon" href="../../favicon.ico">-->


    <title>Coming Soon</title>

    <!-- Bootstrap -->
    <link href="assets/css/bootstrap.css" rel="stylesheet">
    <link href="assets/css/bootstrap-theme.css" rel="stylesheet">
    <link href="assets/css/font-awesome.css" rel="stylesheet">

    <!-- siimple style -->
    <link href="assets/css/style.css" rel="stylesheet">

  </head>

  <body>

    <div id="wrapper">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <h1>TEST</h1>
                    <h2 class="subtitle">We're working hard to improve our website and we'll be ready to launch soon
                    <h2 class="subtitle">Feel free to contact us below with any enquiries</h2>

                    <p>

                    <?php
                        $name = $_POST["contactname"];
                    ?>

                    <form class="Contact" method="post" action="">

                        <label class="alignleft">Name</label>
                        <input type="text" name="contactname" id="contactname" placeholder="Enter Name">

                        <label class="alignleft">Email</label>
                        <input name="Email" placeholder="Email Address">

                        <label class="alignleft">Enquiry</label>
                        <textarea name="Enquiry" placeholder="Your enquiry"></textarea><br>

                        <input id="submit" name="submit" type="submit" value="Submit!" class="btn btn-theme">

                    </form>

                <div class="social">
                        <a href="#"><i class="fa fa-facebook" aria-hidden="true"></i></a>
                         <a href="#"><i class="fa fa-twitter" aria-hidden="true"></i></a>
                         <!--<a href="#"><i class="fa fa-google-plus" aria-hidden="true"></i></a>
                         <a href="#"><i class="fa fa-linkedin" aria-hidden="true"></i></a>-->
                    </div>
                </div>

            </div>

        </div>
    </div>
    <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>

</body>
</html>

Within the php section I am trying to read the values of the input boxes, however even with only

$_POST["contactname"]; 

the website returns a 500 Internal Server Error. If i remove that line and replace with a simple:

echo "Test";

Then the site works and displays “Test”

If there something I am missing with the assigning of the variable from the input box?

4

Answers


  1. You should validate it first

    if(isset($_POST['contactname'])){
      // Do your post handling
    }
    
    Login or Signup to reply.
  2. Firstable , you need to check if $_POST is defined , and not empty :

    if(isset($_POST["contactname"]) && !empty($_POST["contactname"])){
    
    echo $_POST["contactname"];
    }
    

    Second , i think you need to configure you’r server to show errors and not to redirect you to 500 error:
    Just modify your php.ini with this line:

    display_errors = on
    
    Login or Signup to reply.
  3. You should make sure the key of the POST array is set and then validate your values first before you show them, and be sure to handle errors and invalid values!

       if(isset($_POST["contactname"])){
           // Do validation, like making sure its not a empty string
           if (!empty($_POST["contactname"])) {
               echo $_POST["contactname"];
           } else {
               echo "A validation error";
           }
        }
    

    Also enable PHP error output which will greatly help you debug issues like this:

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    

    And display_errors = on in your php.ini to make PHP show parse errors.

    Login or Signup to reply.
  4. I would recommend using a second file to process the form instead of having it all on one page. Not only does it make your code look cleaner but I find it can also help with debugging. So it will look something like this:

    index.php:

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <meta name="author" content="">
    
        <!--<link rel="icon" href="../../favicon.ico">-->
    
    
        <title>Coming Soon</title>
    
        <!-- Bootstrap -->
        <link href="assets/css/bootstrap.css" rel="stylesheet">
        <link href="assets/css/bootstrap-theme.css" rel="stylesheet">
        <link href="assets/css/font-awesome.css" rel="stylesheet">
    
        <!-- siimple style -->
        <link href="assets/css/style.css" rel="stylesheet">
    
      </head>
    
      <body>
    
        <div id="wrapper">
            <div class="container">
                <div class="row">
                    <div class="col-md-12">
                        <h1>TEST</h1>
                        <h2 class="subtitle">We're working hard to improve our website and we'll be ready to launch soon
                        <h2 class="subtitle">Feel free to contact us below with any enquiries</h2>
    
                        <form class="Contact" method="post" action="form_process.php">
    
                            <label class="alignleft">Name</label>
                            <input type="text" name="contactname" id="contactname" placeholder="Enter Name">
    
                            <label class="alignleft">Email</label>
                            <input name="Email" placeholder="Email Address">
    
                            <label class="alignleft">Enquiry</label>
                            <textarea name="Enquiry" placeholder="Your enquiry"></textarea><br>
    
                            <input id="submit" name="submit" type="submit" value="Submit!" class="btn btn-theme">
    
                        </form>
    
                    <div class="social">
                            <a href="#"><i class="fa fa-facebook" aria-hidden="true"></i></a>
                             <a href="#"><i class="fa fa-twitter" aria-hidden="true"></i></a>
                             <!--<a href="#"><i class="fa fa-google-plus" aria-hidden="true"></i></a>
                             <a href="#"><i class="fa fa-linkedin" aria-hidden="true"></i></a>-->
                        </div>
                    </div>
    
                </div>
    
            </div>
        </div>
        <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
        <script src="assets/js/bootstrap.min.js"></script>
    
    </body>
    </html>
    

    form_process.php:

    <?php
        if(isset($_POST['contactname'])
        {
            $name = $_POST['contactname'];
            if(empty($name))
            {
                die("contact name is empty")
            }
            else
            {
                echo $name;
            }   
        }
        // Continue processing form data
    ?>
    

    I would also follow the recommendations from other users about displaying PHP errors.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search