0

Hii guys I need some help.

When I try and test my bootstrap form, it displays a white screen. Here's my code.NB Am newbie to web developing, #ExcuseMyFrenchThough

Here is my index.html

<html>
<div class="container">
<div class="row">
      <div class="col-md-6 col-md-offset-3" id="offer">

            <h2 id="form"> LET'S WORK ? </h2>

            </div>

            <div class="col-md-6 col-md-offset-3">

            <form role="form" method="post" action="contact.php">


            <div class="form-group">
              <input type="text" class="form-control" placeholder="Enter Your Name">
                <?php echo "<p class='text-danger'>$errName</p>";?>
              </div>

              <div class="form-group">

               <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Email">
               <?php echo "<p class='text-danger'>$errEmail</p>";?>
              </div>

              <div class="form-group">
              <textarea class="form-control" id="textarea1" rows="3" placeholder="Enter Your Message here"> </textarea>
              <?php echo "<p class='text-danger'>$errMessage</p>";?>

              </div>

              <div class="form-group">
              <button type="submit" class="default-submit btn btn-large propClone bg-fast-pink btn-circle font-weight-300 text-white tz-text">SEND MESSAGE</button>
              </div>

              <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                      <?php echo $result; ?>  
                   </div>
  </div>
            </form>
            </div>

</div>
</div>

PHP CODE [CONTACT.PHP]

<?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        /*$human = intval($_POST['human']); */
        $from = 'Geofrey Zellah'; 
        $to = 'hotbonge@gmail.com'; 
        $subject = 'Message from Geofrey Zellah ';

        $body = "From: $name\n E-Mail: $email\n Message:\n $message";

        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Please enter your message';
        }

        /*
        //Check if simple anti-bot test is correct
        if ($human !== 5) {
            $errHuman = 'Your anti-spam is incorrect';
        } */

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage /*&& !$errHuman*/) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
}
    }
?>

Screenshot of what I get after I click submit on my page, NB live not local
enter image description here

anyone ?

Jefferson X Masonic
  • 583
  • 2
  • 12
  • 27

2 Answers2

1
  1. The submit variable is never set in your form. Note the button is now an input of the type submit with the name attribute of submit.

  2. Also your other form variables were not set.

  3. You were never echoing anything. So i put an echo in your last condition.

  4. If you want the form to display after submitting the form, you need to rename your index.html to index.php and include contact.php at the top. See below.

  5. If you just plainly check if a $_POST variable is true, PHP will throw E_NOTICE errors. So best wrap the variable into the isset() (as in is this variable set) function. See below.

I refactored to prevent E_NOTICE errors and commented the changes.

contact.php

<?php
if (isset($_POST["submit"])) {

    $error = [];

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    /*$human = intval($_POST['human']); */
    $from = 'Geofrey Zellah';
    $to = 'hotbonge@gmail.com';
    $subject = 'Message from Geofrey Zellah ';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    // Check if name has been entered
    if (!isset($_POST['name']) || strlen($_POST['name']) === 0) {
        $error['name'] = 'Please enter your name';
    }

    // Check if email has been entered and is valid
    if (!isset($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $error['email'] = 'Please enter a valid email address';
    }

    //Check if message has been entered
    if (!isset($_POST['message']) || strlen($_POST['name']) === 0) {
        $error['message'] = 'Please enter your message';
    }

    /*
    //Check if simple anti-bot test is correct
    if ($human !== 5) {
        $errHuman = 'Your anti-spam is incorrect';
    } */

// If there are no errors, send the email
    if (empty($error)) {
        if (mail ($to, $subject, $body, $from)) {
            $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
        } else {
            $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
        }
    }

}
?>

index.php <-- Note the change of file extension

<?php include 'contact.php';?>
<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3" id="offer">

            <h2 id="form"> LET'S WORK ? </h2>

        </div>

        <div class="col-md-6 col-md-offset-3">

            <form role="form" method="post">

                <div class="form-group">
                    <input name="name" type="text" class="form-control" placeholder="Enter Your Name">
                    <?php if(isset($error['name'])) echo '<p class="text-danger">'.$error['name'].'</p>'; ?>
                </div>

                <div class="form-group">

                    <input name="email" type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Email">
                    <?php if(isset($error['email'])) echo '<p class="text-danger">'.$error['email'].'</p>'; ?>
                </div>

                <div class="form-group">
                    <textarea name="message" class="form-control" id="textarea1" rows="3" placeholder="Enter Your Message here"> </textarea>
                    <?php if(isset($error['message'])) echo '<p class="text-danger">'.$error['message'].'</p>'; ?>

                </div>

                <div class="form-group">
                    <input name="submit" type="submit" class="default-submit btn btn-large propClone bg-fast-pink btn-circle font-weight-300 text-white tz-text">SEND MESSAGE</input>
                </div>

                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <?php if(isset($result)) echo $result; ?>
                    </div>
                </div>
            </form>
        </div>

    </div>
</div>

UPDATE

If you do not want a reload of the page when submitting the form, you will need some jQuery ajax action and alter your HTML and PHP file.

First remove the first line of your index.php that we added before:

 <?php include 'contact.php';?><!-- Remove this one -->

You do not want the file to be included, but rather send data to it.

Next edit the HTML file and include jQuery library underneath your HTML (common practice to do JS stuff below HTML). Then alter your PHP file accordingly.

So your new HTML:

<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3" id="offer">

            <h2 id="form"> LET'S WORK ? </h2>

        </div>

        <div class="col-md-6 col-md-offset-3">

            <form role="form" name="contact" method="post">

                <div class="form-group">
                    <input name="name" type="text" class="form-control" placeholder="Enter Your Name" value="test">
                </div>

                <div class="form-group">

                    <input name="email" type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Email" value="giroteam@localhost.com">
                </div>

                <div class="form-group">
                    <textarea name="message" class="form-control" id="textarea1" rows="3" placeholder="Enter Your Message here">test </textarea>
                </div>

                <div class="form-group">
                    <input name="submit" type="submit" class="default-submit btn btn-large propClone bg-fast-pink btn-circle font-weight-300 text-white tz-text" value="SEND MESSAGE">
                </div>

                <div class="form-group" id="result">
                </div>
            </form>
        </div>

    </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){ // launch when DOM is fully loaded

        $('form[name="contact"]').submit(function(event){ // fire when you hit submit

            event.preventDefault(); // prevent default form submission since you want to send data via ajax

            $('#result').html('');
            $('.alert').remove();

            var values = $(this).serialize();

            // Post form data to your contact.php script and work with response
            $.ajax({
                url: "contact.php",
                type: "POST",
                data: values ,
                success: function (response) {

                    if(response.success) {
                        $('#result').html('<div class="alert alert-success">'+response.success+'</div>');
                    }
                    if(response.error_form) {
                        $.each( response.error_form, function( key, value ) {
                            $('input[name="'+key+'"]').parent().append('<p class="help-block text-danger">'+value+'</p>');
                        });
                    }
                    if(response.error_mail) {
                        $('#result').html('<div class="alert alert-danger">'+response.error_mail+'</div>');
                    }

                },
                error: function(jqXHR, textStatus, errorThrown) {
                    console.log(textStatus, errorThrown);
                }


            });

        });
    });
</script>

And finally the changed PHP:

<?php

ini_set('display_errors',0);
$result = [];

// Check if name has been entered
if (!isset($_POST['name']) || strlen($_POST['name']) === 0) {
    $result['error_form']['name'] = 'Please enter your name';
}

// Check if email has been entered and is valid
if (!isset($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    $result['error_form']['email'] = 'Please enter a valid email address';
}

//Check if message has been entered
if (!isset($_POST['message']) || strlen($_POST['message']) === 0) {
    $result['error_form']['message'] = 'Please enter your message';
}

/*
//Check if simple anti-bot test is correct
if ($human !== 5) {
    $errHuman = 'Your anti-spam is incorrect';
} */

// If there are no errors, send the email
if (empty($result['error_form'])) {


    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    /*$human = intval($_POST['human']); */
    $from = 'Geofrey Zellah';
    $to = 'hotbonge@gmail.com';
    $subject = 'Message from Geofrey Zellah ';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if (mail ($to, $subject, $body, $from)) {
        $result['success']='Thank You! I will be in touch';
    } else {
        $result['error_mail']='Sorry there was an error sending your message. Please try again later';
    }
}

header('Content-type: application/json'); // tell browser what to expect
echo json_encode($result); // encode array to json object so javascript can work with it

I made such an elaborate example since many people decide to go ajax once they are successful in sending a regular form but notice that the page reloads ;)

Yolo
  • 1,569
  • 1
  • 11
  • 16
1

I get what you want to achieve. You want to submit this form and if something is wrong, you want to show some error messages.

What has happened in your case is that, when you submit your form, it gets submitted to contact.php. Your browser will show what you return from the contact.php file. Since your contact.php do not include any HTML content and you don't write anything either, returned page doesn't include anything.

You have two options.

First way:

echo the error messages to the contact.php file. Add the following to the end of contact.php file.

echo $result;

This way, when someone submit your form. You will direct him to a new page which only have some line saying whether there was error or successful.

Second way

Move that logic to your index.html. You have to rename it to index.php. Then set the form to submit to the same page.

<form .... action="">

This will submit the form to the current page, which is index.php. Then, put your logic in contact.php at top of your index.php.

Community
  • 1
  • 1
Imesha Sudasingha
  • 3,462
  • 1
  • 23
  • 34
  • Imeesha thanks, I tried the second way to change index.html to index.php, unfortunately, my page display white screen just blank page with errors or nothing, but when I put index.HTML displays my page, what can I do to solve this issue also – Jefferson X Masonic Mar 16 '17 at 00:46
  • Imeesha thank you, your second method works now, I needed to change index.html to index.php also put all together into one page without contact,php, now my contact form is able to send email as I wanted but unfortunately It does not display "success message" thanks again – Jefferson X Masonic Mar 16 '17 at 01:05