[Solved] submit a form with php and provide response to user with ajax


I am not sure what you need exactly but I see the “data:” is empty, you can use serialize to post all input data in your form to your php, like this:

here is a test form:

<form id="createaccount">
<input type="text" name="a" id="a">
<input type="submit" value="Submit">
</form>

here is your javascript part:

$("#createaccount").submit(function(){
    $.ajax({
    type: POST,
    url: "createaccount2.php",
    data: $("#createaccount").serialize(),
    success: function(server_response){
        if(server_response == 0){
            alert('HELLO');
        }
        else if(server_response == 1){
            alert('WORLD');
        }
        else if(server_response == 2){
            alert('AAAA');
        }
        else if(server_response == 3){
            alert('zzZzZzZ');
        }

    });
});

then in your php you can access the data by the $_POST variable:

<?php

$a=$_POST['a'];
if($a=='hello'){
    echo "0";
}elseif($a=='world'){
    echo "1";
}else{
    echo "2";
}

?>

if you enter “hello” and submit it to the php, it will echo “0” and display “HELLO”
(ps: not tested!)

solved submit a form with php and provide response to user with ajax