0

I'm doing a form validator ( among other things ) and I've set a response code in my script so I can use AJAX.. for some wierd reason it's throwing this error when I execute the script:

Fatal error: Call to undefined function http_response_code()

My script:

if ($_SERVER["REQUEST_METHOD"] == "POST"){
    $email = filter_var(trim($_POST['email'],FILTER_SANITIZE_EMAIL));
    $code = trim($_POST['code']);

    if (!filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($code)){
        http_response_code(400);
        echo "Oops! There was a problem with your submission. Try again!";
        exit;
    }

    if(!$conn) die("Something wrong happened" . mysql_error());

    if(mysql_query($sql,$conn)){

        if(isset($_POST['newsletter'])){
            $MailChimp = new MailChimp();
            $list_id = '*********';
            $user_info = array('email_address' => $email,'status' => 'subscribed');
            $result = $MailChimp->post("lists/$list_id/members",$user_info);

            if(strpos($MailChimp->getLastError(), 'is already a list member') !== false) echo 'You are already subscribed, smarty pants!';
        }

        http_response_code(200);
        echo "Thank you!";
    }

    else{
       http_response_code(500);
       echo "Oops! Something went wrong";
    }

       if(!mysql_query($sql,$conn)) echo "<p>" . "There seems to be a problem :< ... Please check the info you provided and try again!" . "</p>";
       else echo "<p>" . "Much thanks! Very wow :D" . "</p>";
       mysql_close($conn);
}

Anyone has a clue on what could be going on or a better way to do this?

Onilol
  • 1,315
  • 16
  • 41
  • 1
    There is no code here for `http_response_code()` – Jay Blanchard Feb 12 '16 at 18:12
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Feb 12 '16 at 18:12
  • 1
    Perhaps your version of PHP doesn't support it? What version are you running? – Rasclatt Feb 12 '16 at 18:13
  • 1
    PHP >= 5.4.0 needed. _Much thanks! Very wow :D_ – AbraCadaver Feb 12 '16 at 18:13
  • 1
    http_response_code is available from php >= 5.4 – fusion3k Feb 12 '16 at 18:13
  • Well there you go! Your version is too old. You need to upgrade or not use it. – Rasclatt Feb 12 '16 at 18:17

0 Answers0