-1

I've been trying to grab a username based on registered email when user enters his email and click on a button I've provide two data to my ajax request first is [cf] which identify which portion of code should be processed and second is the email but when clicking the button nothing happens and no errors appear in the console only when I open the page that have the php code of ajax request it says "Notice: Undefined variable: funcCall in D:\XAMPP\htdocs\lahj\framework\signin.php on line 19"
Here I will provide my code for ajax:

$("#login-next-btn").click(function(){
        if(!$("#signin-email").val().trim()){
            var email = $("#signin-email").val().trim();
            $.ajax({
                url: "framework/signin.php",
                type: "POST",
                data: {cf:'cf1',email:email},
                dataType:"json",
                success: function(data){
                    if(data.result != "0")
                    {
                        alert("helo");
                        $("#signin-box-header p:first-of-type").text("مرحبًا");
                        $("#signin-box-header p:last-of-type").remove();
                        $("#signin-box-header").append("<p id='sigin-email-display'>"+data.result+"</p>");
                        $("#first-loader").fadeOut(600,function(){
                            $("#second-loader").css({"display":"flex"}).fadeIn(400);
                        });
                    }
                    else
                    {
                        alert("fail");
                    }
                }
            });
        }
    });

and here is my php code in a file called signin.php in a folder called framework:

<?php
ini_set( 'error_reporting', E_ALL );
ini_set( 'display_errors', true );
include_once 'db.php';

$email = null;
$funcCall = null;

if(isset($_POST['email']))
{
    $email = $_POST['email'];
}

if(isset($_POST['cf']))
{
    $funcCall = $_POST['cf'];
}

if($funcCall == 'cf1' && !empty($email))
{
    try
    {
        $database = new db();
        $db = $database->openConnection();
        $stmt = $db->prepare("select userName from khUsers where email = ?");
        $stmt->execute(array($email));
        $usersName = $stmt->fetchColumn();
        $data = array('result' => $usersName);
        echo json_encode($data);
    }
    catch (PDOException $e)
    {
        echo "There is some problem in connection: " . $e->getMessage();
        $data = array('result' => "0");
        echo json_encode($data);
    }
}
?>
Raizada Dev
  • 63
  • 11
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – aynber Sep 18 '19 at 13:34
  • 1
    That's not how you create variables in PHP. You need to actually assign it a value, even if it's just `NULL` – aynber Sep 18 '19 at 13:35
  • `$funcCall;` is not an assignment, it's a reference. Just do something like `$funcCall = null;` – Alex Howansky Sep 18 '19 at 13:36
  • What sort of _"button"_ is `#login-next-btn`? Also, do you realise your AJAX call will only be made if `$("#signin-email").val().trim()` evaluates as _falsy_ (ie an empty string)? From looking at the rest of your code, that doesn't appear to be what you want so try removing the `!` – Phil Sep 19 '19 at 05:54
  • when you `console.log(email);` before ajax request, does it work ? – Akhtar Munir Sep 19 '19 at 07:17

1 Answers1

-1

Add exit() function at the end of json_encode() function. it will show you result/error on your request in console.

if($funcCall == 'cf1' && !empty($email))
{
    try
    {
        $database = new db();
        $db = $database->openConnection();
        $stmt = $db->prepare("select userName from khUsers where email = ?");
        $stmt->execute(array($email));
        $usersName = $stmt->fetchColumn();
        $data = array('result' => $usersName);
        echo json_encode($data); exit();
    }
    catch (PDOException $e)
    {
        echo "There is some problem in connection: " . $e->getMessage();
        $data = array('result' => "0");
        echo json_encode($data); exit();
    }
}
Hari Om Gupta
  • 61
  • 1
  • 8