0

I have a contact-form.php and one index.html file as follows. but when click on submit to send email it reload the page and nothing happened, again the home page of site is refreshed. where is my problem?

I see lots of SO links but I couldn't find the problem. I attached here the codes. all of these two files are in the same folder on PHP Server.

<!-- CONTACT FORM -->
                                <div class="">
                                    <form id="contact-form" action="contact-form.php" method="POST">

                                        <div class="row">
                                            <div class="col-md-12 mb-30">
                                                <!-- <label>Your name *</label> -->
                                                <input type="text" value="" data-msg-required="Please enter your name"
                                                    maxlength="100" class="controled" name="name" id="name"
                                                    placeholder="NAME" required>
                                            </div>
                                        </div>

                                        <div class="row">
                                            <div class="col-md-12 mb-30">
                                                <!-- <label>Your email address *</label> -->
                                                <input type="email" value=""
                                                    data-msg-required="Please enter your email address"
                                                    data-msg-email="Please enter a valid email address" maxlength="100"
                                                    class="controled" name="email" id="email" placeholder="EMAIL"
                                                    required>
                                            </div>
                                        </div>

                                        <div class="row">
                                            <div class="col-md-12 mb-40">
                                                <!-- <label>Message *</label> -->
                                                <textarea maxlength="5000" data-msg-required="Please enter your message"
                                                    rows="3" class="controled" name="message" id="message"
                                                    placeholder="MESSAGE" required></textarea>
                                            </div>
                                        </div>

                                        <div class="row">
                                            <div class="col-md-12 text-center-xxs">
                                                <input type="submit" value="SEND MESSAGE" class="button medium gray"
                                                    data-loading-text="Loading...">
                                            </div>
                                        </div>

                                    </form>
                                    <div class="alert alert-success hidden animated fadeIn" id="contactSuccess">
                                        <strong>Success!</strong> Your message has been sent to us.
                                    </div>

                                    <div class="alert alert-danger hidden animated shake" id="contactError">
                                        <strong>Error!</strong> There was an error sending your message.
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>

                </div>

and

<?php 
if(isset($_POST['submit'])){
    $to = "bbbbb@gmail.com"; 
    $from = $_POST['email']; 
    $first_name = $_POST['name'];
    $subject = "Form submission";
    $message = $first_name . " " . " wrote the following:" . "\n\n" .             $_POST['message'];


    $headers = "From:" . $from;

   $mail_status= mail($to,$subject,$message,$headers);


    }

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        window.location.href = 'index.html#contactSuccess';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
                window.location.href = 'index.html#contactSuccess';
    </script>
<?php
}
?>
  • Possible duplicate of [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) –  May 23 '19 at 23:05
  • @tim I read it carefully nothing help to me. I catch error in html not PHP file –  May 23 '19 at 23:07

1 Answers1

0

Put a name on your HTML submit button:

<input type="submit" name="submit" value="SEND MESSAGE" class="button medium gray"
                                                        data-loading-text="Loading...">

EDIT:

  1. Firstly, put your final if inside the $_POST['submit'] if. You only ever do that on submission, and $mail_status is only ever set inside it:

  2. Use $_GET params, rather than# ... those are use to scroll to an anchor that is named the same.

  3. Convert index.html to index.php so you can control what DIV to display using PHP.

$result = $_GET['result'] will contain anything after the = in the URL with ?result=

So then

if($result == 'success') { 
?> <!-- Show Success Div --> <?php 
} else { 
?><!-- Show error DIV --> <?php

Here's your PHP with the changes to the redirect URL to use $_GET params and index.html convert to PHP:

<?php
if (isset($_POST['submit'])) {
    $to         = "bbbbb@gmail.com";
    $from       = $_POST['email'];
    $first_name = $_POST['name'];
    $subject    = "Form submission";
    $message    = $first_name . " " . " wrote the following:" . "\n\n" . $_POST['message'];


    $headers = "From:" . $from;

    $mail_status = mail($to, $subject, $message, $headers);
    if ($mail_status) {
?>
   <script language="javascript" type="text/javascript">
        window.location.href = 'index.php?result=success';
    </script>
<?php
    } else {
?>
   <script language="javascript" type="text/javascript">
                window.location.href = 'index.php?result=failed';
    </script>
<?php
    }

}
?>
Adam
  • 1,294
  • 11
  • 24
  • solved very thanks ! but one problem is remain. why the "
    –  May 23 '19 at 23:10
  • How I can pass status to html? why this is not done exactly? –  May 23 '19 at 23:34
  • I've made edits... you'll never see those `DIVs` from that code, firstly your using the same `#` name on the links, and that would only ever scroll you to the `Div`. You need `$_GET` params and a PHP index page. The use the `Get` param to control what `DIV` you add to the `DOM`. – Adam May 24 '19 at 09:09
  • My main file is index.html not index.php. –  May 24 '19 at 10:01
  • please see last part in html template. I think the author of this template send back result from php-contact-form to html without modification. and just one of the Div successes or error shown. I test it ! but php form is not available. –  May 24 '19 at 10:07
  • You can use .php extention with HTML. This then allows you to do server side processing. If you're looking for some other sort of solution than this then good luck! – Adam May 24 '19 at 12:05
  • because this is a new problem I post a new question for the last problems https://stackoverflow.com/questions/56292445/response-from-php-email-form-to-html-not-succed –  May 24 '19 at 12:12