好吧,这个问题并不是新问题,正如我所看到的(确实冲浪了很多),但仍然找不到适合自己的解决方案。
问题描述:
并尝试使用$ .ajax发送POST
function checkUser(){
$.ajax({
type: 'POST',
url: 'ajax/checkUser.php',
data: 'uname='+$("#username").val()+'&pword='+$("#password").val(),
success: function(){
someNewFunc();
},
});
};
我正在调用的函数:
$(document).ready(function(){
$(".button.postfix.login").on("click", function(){
if (($("#username").val() != "") && ($("#password").val() != "")) {
hide(); <-- hide pop ups about empty field
checkUser();
} else {$("#empty").show();} <-- pop ups
});
});
这是带有元素的页面代码:
<input id="username" type="text" placeholder="Username" />
<input id="password" type="password" placeholder="Password" />
<a class="button postfix login" href="#"
onclick="event.preventDefault();">Sign in</a>
我猜数据正在消失,但是页面checkUser.php没有任何信息。我究竟做错了什么?
PS checkUser.php:
<?php
print_r($_POST);
?>
结果-数组()
PPS顺便说一句,如果POST更改为GET,则可以使用,但需要进行POST
$(document).ready(function(){
$(".button.postfix.login").on("click", function() {
var username = $("#username").val();
var password = $("#password").val();
if ((username != "") && (password != "")) {
hide(); <-- hide pop ups about empty field
checkUser(username, password); // Pass the variables here as a parameter
} else {
$("#empty").show();} <-- pop ups
});
// Put the parameters here
function checkUser(username, password) {
$.ajax({
type: 'POST',
url: 'ajax/checkUser.php',
data: { username:username, password:password} //It would be best to put it like that, make it the same name so it wouldn't be confusing
success: function(data){
alert(data);
},
});
};
});
因此,当您要在php上调用它时
$username = $_POST['username'];
echo $username;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句