-2

I am having problem send email using php and jquery if some one can take a look and help me i would be thankful to him. This is my HTML

        <form id="contact" name="contact" method="post">
        <h2>
            <span>
                Read This FREE Report
            </span>
            Discover How You Could Become a Successful Forex Trader In As Little As 72 Hours From Now (or Less)!
        </h2>
        <input type="text" id="name" placeholder="Name" name="name" required />
        <input type="email" id="email" placeholder="Email" name="email" required />
        <button type="submit">
            Free Instant Access
        </button>
    </form>

Jquery

        // Get the messages div.
    var formMessages = $('#form-messages');

    // Set up an event listener for the contact form.
    $('form').submit(function (event) {
        // Stop the browser from submitting the form.
        event.preventDefault();

        // Serialize the form data.
        var formData = $('form').serialize();

        // Submit the form using AJAX.
        $.ajax({
            type: 'POST',
            url: 'contact_form.php',
            data: formData
        })



          .done(function (response) {
              $(formMessages).text("Your form has been send..");

              // Set the message text.
              $(formMessages).text(response);

              // Clear the form.
              $('#name').val('');
              $('#email').val('');
          })
          .fail(function (data) {
              $(formMessages).text("Oops! An error occured and your message could not be sent");

              // Set the message text.
              if (data.responseText !== '') {
                  $(formMessages).text(data.responseText);
              } else {
                  $(formMessages).text('Oops! An error occured and your message could not be sent.');
              }
          });
    });

Php

<?php

// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace.
    $name = strip_tags(trim($_POST["name"]));
            $name = str_replace(array("\r","\n"),array(" "," "),$name);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);

    // Check that data was sent to the mailer.
    if ( empty($name)) {
        // Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo "Oops! Dogodio se problem za vašim zahtjevom. Molimo pokušajte opet.";
        exit;
    }

    // Set the recipient email address.
    // FIXME: Update this to your desired email address.
    $recipient = "email@hotmail.com";

    // Set the email subject.
    $subject = "Web contact from $name";

    // Build the email content.
    $email_content = "Person: $name\n";
    $email_content = "Email: $email\n";

    // Build the email headers.
    $email_headers = "From: $name <$email>";

    // Send the email.
    if (mail($recipient, $subject, $email_content, $email_headers)) {
        // Set a 200 (okay) response code.
       http_response_code(200);
        echo "Thank You! Your message has been sent.";
    } else {
        // Set a 500 (internal server error) response code.
        http_response_code(500);
        echo "Oops! Došlo je do pogreške, nismo uspjeli poslati vašu poruku.";
    }

} else {
    // Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo "Dogodio se problem za vašim zahtjevom. Molimo pokušajte opet.";
}

?>

And this is error i get:
Fatal error: Call to undefined function http_response_code() in /home2/speanut/public_html/freeforexreport.com/contact_form.php on line 35

mocni
  • 9
  • 1
    The error is pretty clear about the problem. Where is that function defined? Are you using a version of PHP which has that function built-in? – David Jan 11 '16 at 12:05
  • i am not so good with php so i dont now it, maybe it is clear for you but not for me.. – mocni Jan 11 '16 at 12:07
  • have you checked the version of PHP? per the API description of `http_response_code()` , it should work on (`PHP 5 >= 5.4.0`). http://php.net/manual/en/function.http-response-code.php – Codeone Jan 11 '16 at 12:14
  • i am using 5.4.41 php version – mocni Jan 12 '16 at 10:21

1 Answers1

0

Seems like your PHP Version is below 5.4. The http_response_code() function was introduced in PHP 5.4. I would advice you to update for PHP 5.4 or higher. If you can not do that, you can use following compatibility code:

// For 4.3.0 <= PHP <= 5.4.0
if (!function_exists('http_response_code'))
{
    function http_response_code($newcode = NULL)
    {
        static $code = 200;
        if($newcode !== NULL)
        {
            header('X-PHP-Response-Code: '.$newcode, true, $newcode);
            if(!headers_sent())
                $code = $newcode;
        }       
        return $code;
    }
}

For more reference look at the following question.

Community
  • 1
  • 1
Bfcm
  • 2,686
  • 2
  • 27
  • 34