我有一个非常简单的ajax调用,所以我可以先测试一下:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
function validateLogIn()
{
var username = $("#username").val();
console.log(username,password,login,remember);
//alert('Given data is not correct' + username + " " + password);
$.ajax({
url: 'validate.html'
type: 'POST', // POST/GET
data: { 'username' : username } //for now just pass username
}).done(function(response){ //Attach a succes handler
alert(response); //my complete code in validate.html
// displays in this alert!
});
});
}
</head>
<body>
<form action="crud.html" method="post" name="form_submit" onsubmit="return validateLogIn()">
<input required placeholder="Username" type="text" name="username" id="username"/>
<input required placeholder="Password" type="password" name="password" id="password"/>
<label for="remember">Remember Me:</label>
<input type="checkbox" name="remember" value="yes" id="remember" />
<br />
<br />
<input type="submit" name="login" value="login" id="login"/>
</form>
validate.html中的:没有一个被执行
<?php
echo htmlspecialchars($_POST['username'], ENT_QUOTES, 'UTF-8');
echo $username = $_POST['username'];
<script type='text/javascript'>alert('$username');</script>
答案 0 :(得分:1)
我认为你根本不懂AJAX。我在下面做了一个非常基本的例子和评论。
var username = $("#username").val();
$.ajax({
url: 'validate.php', // << You can't have php in html files.
type: 'POST', // POST/GET
data: { 'username' : username } //Data to be send will be in $_POST
}).done(function(response){ //Attach a succes handler
alert(response); //Alert the response from validate.php
});
}
PHP文件:
<?php
echo htmlspecialchars($_POST['username'], ENT_QUOTES, 'UTF-8'); //XSS PREVENTION