Oath Comrade
Oath Comrade

Reputation: 293

Submit form and get echo from php in jquery

I have a form and a submit button that sends some input values per POST to a php file. The php validates and echos a value. How can I handle this echo value per jquery (in a jquery function)?

Sample code:

        <form id="login" method="post" action="/php/login.php">
            <input type="text" id="username" name="username" placeholder="Username" autofocus="true"></input>
            <input type="password" id="password" name="password" placeholder="Password"></input>
            <button id="loginButton" type="submit" name="submit">Login</button>
        </form>

just pretend /php/login.php is

<?php
    echo "OK";
?>

now, I imagine there must be a way to get this echo value in some sort of a form-submit callback in jquery?

Upvotes: 0

Views: 569

Answers (1)

Oath Comrade
Oath Comrade

Reputation: 293

Ok, I found out that I had to elude the actual submit and do a seperate post

$("#login").submit(function(event) {
    event.preventDefault();
    var myUsername = … 
    var myPassword = …
     $.post("/php/login.php", { username:myUsername, password:myPassword }, function(data){
         alert(data); // which is the php echo.
    });

});

Upvotes: 0

Related Questions