0

I have a problem with a signup option that I’ve created on my website. For my signup option, I have followed this tutorial exactly: https://code.tutsplus.com/tutorials/building-a-sleek-ajax-email-signup-form--net-13645

It worked like a charm on my test website, but now that I’ve moved everything to another server, I get two different types of error messages.

The first one: Error1

The error concerns the following line in my javascript file:

var responseData = jQuery.parseJSON(data),

This is what Safari, Firefox and Opera are telling me. And then Chrome is also reporting this error: Error2

Is this maybe because mysql functions have been removed in PHP7? My new server uses PHP 5.6 though… My PHP looks like this at the moment:

    <?php
if(isset($_GET['action'])&& $_GET['action'] == 'signup'){
  mysql_connect('localhost:3306','db_username','db_password');
  mysql_select_db('db_name');

  //sanitize data
  $email = mysql_real_escape_string($_POST['signup-email']);

  //validate email address - check if input was empty
  if(empty($email)){
    $status = "error";
    $message = "You did not enter an email address";
  }
  else if(!preg_match('/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/', $email)){ //validate email address - check if is a valid email address
    $status = "error";
    $message = "Invalid email address";
  }
  else {
    $existingSignup = mysql_query("SELECT * FROM signups WHERE signup_email_address='$email'");
    if(mysql_num_rows($existingSignup) < 1){

  $date = date('Y-m-d');
  $time = date('H:i:s');

  $insertSignup = mysql_query("INSERT INTO signups (signup_email_address, signup_date, signup_time) VALUES ('$email','$date','$time')");
  if($insertSignup){
    $status = "success";
    $message = "You have been signed up";
  }
  else {
    $status = "error";
    $message = "Ooops, there has been a technical error";
  }
}
else {
  $status = "error";
  $message = "This email address has already been registered";
}
  }

      //return json response
      $data = array(
        'status' => $status,
        'message' => $message
      );

      echo json_encode($data);

      exit;
    }
    ?>

Would I have to rewrite this with the new mysqli_connect()? How would I have to do that? But then I’m still left with first problem… Does anyone have an idea how I can solve these two problems? Thank you very much!

L. Mayer
  • 45
  • 7
  • 1
    The mysql functions are still available in 5.6. You may need to enable this extension in your php.ini file. But it would be better if you converted to mysqli or PDO. – Barmar Jan 03 '18 at 22:45
  • 2
    The first problem is happening because of the second problem. The "F" that the error is complaining about is actually the "F" of the error message "Fatal error: Uncaught error...". That's what's being sent back instead of the JSON. And yes, you should drop the mysql_* functions like a burning deprecated potato and switch to using PDO or mysqli. – Matt Gibson Jan 03 '18 at 22:45
  • 1
    In a nutshell - yes, the problem is using `mysql` functions - rewrite your database code to either `mysqli` or `PDO` – Professor Abronsius Jan 03 '18 at 22:47
  • Possible duplicate of [Why shouldn't I use mysql\_\* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Matt Gibson Jan 03 '18 at 22:49

1 Answers1

1

Option 1

Probably your server has newer version of PHP (>= 7.0.0). The MySQL extension was deprecated in PHP5.5.0 and removed in 7.0.0

The reason mysql_* functions were removed is that they were implemented in the early 2.0 version of PHP, which means that they are over 15 years old. This library limits MySQL functionality and has become very hard to maintain.

If you are a beginner I would recommend looking for a MySQLi or PDO_MySQL library tutorial, or you can port the code yourself if you have a little bit more experience.

Option 2

Another possible cause is because the MySQL extension is disabled in your php.ini file. To see more info about your current PHP configuration create and open a new PHP file containing the following:

<?php phpinfo(); ?>
Andrey Tsarev
  • 769
  • 1
  • 8
  • 25