0

api.php

<?php
include "connection.php";
$route = $_POST['route'];
if($route == "register"){
    
    $email = $_POST['email'];
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $password = $_POST['pwd'];
    
    $sql_check_email = "SELECT * FROM userstable WHERE email='$email'";
    $result = $conn->query($sql_check_email);
    
        
            if ($result->num_rows == 1)
            {
            $response = array('result' => false);
            echo json_encode($response);
            }
        else
            {
        $sql = "INSERT INTO userstable (email, name, surname, password)
        VALUES ('$email', '$firstname', '$lastname', '$password')";

    if ($conn->query($sql) === TRUE) {
        echo "New user account created successfully";
        $response = array('result' => true, 'email' => $email, 'name' => $firstname, 'surname' => $lastname);
        echo json_encode($response);
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
        $response = array('result' => false, 'error' => $conn->error);
        echo json_encode($response);
        }           
            }
}
$conn->close();

?>

?>

services.js

$(document).ready(function(){
    $('#register_container').show();

$('#register').click(function(e){
  e.preventDefault();
  
  var route = "register";
  var email= $('#reg_email').val();
  var firstname= $('#firstname').val();
  var lastname= $('#lastname').val();
  var pass= $('#reg_password').val();
  var rptpass= $('#password_rpt').val();
 
    if(pass == rptpass){
   $.ajax({
    url:"api.php",
    method:"POST",
    data:{route:route, email:email, firstname:firstname, lastname:lastname, pwd:pass},
    dataType:"json"})
   .done(function( data ) {
       if (data.result == false){
         $('#email_message').text("This" + data.email + "is not available");
       }
       else if (data.result == true){
     console.log('Success!!' + data.result + ", " + data.email);
     $('#register_container').hide();
     $('#main_container').show();
     $('#main_message').text("Welcome " + data.email);
       }
    })
    .fail(function( data ) {
    {
     console.log('Error!!' + data.result + ", " + data.error);
     $('#register_message').text("An error has occured!" + data.error);
    }
   });  
    }
  else
  {
   $('#register_message').text("Password and repeat password do not match!!!");
  }
    });
});

I try to create a registration form which check if the password matches with repeat password and check if email exist in database, when I run the code in localhost the data inserted into the database but the ajax call fails. Is there any solution or something that is not correct in my code.

PKd
  • 1
  • You code is vulnerable to [sql injection attacks](https://stackoverflow.com/questions/601300/what-is-sql-injection) please take time to inform yourself on the subject as it will save you a lot of headache in the future – litelite Jan 05 '22 at 20:02
  • Also, you said the call fails. Do you have the HTTP status code, any error message whether in the ajax call response or in the browser console? – litelite Jan 05 '22 at 20:05
  • Can you share the failed error message? – mail2bapi Jan 05 '22 at 20:17
  • @litelite It is just an experiment to see how it works. The console response is that "Error!!undefined, undefined" services.js:33 – PKd Jan 05 '22 at 20:32
  • @mail2bapi console reponse is that "Error!!undefined, undefined" services.js:33 – PKd Jan 05 '22 at 20:44
  • You've implemented .fail() wrong. It won't include the JSON response provided by your PHP. That's why you're seeing `Error!!undefined, undefined`, which isn't much use to you. Refer to https://api.jquery.com/deferred.fail/ and https://stackoverflow.com/questions/9847244/what-are-the-parameters-sent-to-fail-in-jquery to see what arguments are passed to the function. They do actually contain useful info, just not the info you're trying to read. Meanwhile I suggest checking your Network tool to see if there's a clue about why it failed. Or see if there are any other related Console errors. – ADyson Jan 05 '22 at 22:42

0 Answers0