[英]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. 我有一个表单和一个提交按钮 ,每个POST将一些输入值发送到一个php文件。 The php validates and echos a value.
php验证并回显一个值。 How can I handle this echo value per jquery (in a jquery function)?
如何处理每个jquery的回显值(在jquery函数中)?
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/login.php是
<?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? 现在,我想必须有某种方法可以在jquery中的某种形式提交回调中获取此echo值?
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.
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.