Aly Hassan Aly Hassan - 3 months ago 26
Javascript Question

Send data through ajax to php and get response

I wanna know what is wrong with my code.
I just wanted to post data to php through ajax and get the response of the same page, but that doesnt work idk why?

Js Jquery

$("#signup-verify").submit(function(e) {
input_val = $("input#verify-username").val();
url = 'verify-user.php';
$.ajax({
type : 'POST',
url : url,
dataType : 'json',
data : {username: input_val},
success : function(user_verification){
alert("Success");
}
});
});


PHP

<?php
session_start();
include_once '../login/user.class.php';
$username = $_POST['username'];
$object = new User();
$user_verification = $object->VerifyUser($username);
echo json_encode($user_verification);

Answer Source
$("#signup-verify").submit(function(e) {
    e.preventDefault();
    var input_val = $("input#verify-username").val();
    var url = 'verify-user.php';
    $.ajax({
        type : 'POST',
        url : url,
        dataType : 'json',
        data : {username: input_val},
        success : function(user_verification){
            alert("Success");   
        }
    });
});