简体   繁体   中英

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?

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.
    });

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM