我正在HTML5 / Javascript / CSS3实现中创建一个应用程序。 我在localhost上有一个外部PHP文件,我正在尝试使用jQuery和AJAX与它进行通信。如果我在浏览器中加载文件,一切正常,我从PHP文件中获得所需的响应。但是当在Xcode的模拟器中运行它时,我只接收整个PHP脚本作为响应。我不知道为什么它没有执行。
HTML表单:
<form id="loginForm" method = "post" action="PHP/login.php">
<input type="text" name="username" id="username" placeholder="Username" style="width: 200px;"><br>
<input type="password" name ="password" id="password" placeholder="Password" style="width: 200px;"><br>
<button type="submit" id="login" style="float: right;">Login</button>
</form>
jQuery的:
$(document).ready(function (){
$("button#login").click(function (){
u = $("#username").val();
p = $("#password").val();
if($("#username").val()==""||$("#password").val()=="")
alert("Please enter username and password");
else
$.post( $("#loginForm").attr("action"),
function(data){
alert(data);
});
$("#loginForm").submit( function(){
return false;
});
});
});
PHP连接到MYSQL数据库:
//connection to the database
$dbhandle = mysql_connect("localhost", "root", "************")
or die("Unable to connect to MySQL");
//select a database to work with
$selected = mysql_select_db("VLA",$dbhandle)
or die("Could not select examples");
$username = mysql_real_escape_string($_POST["username"]);
$pass = mysql_real_escape_string($_POST["password"]);
$query = "SELECT count(*) FROM users WHERE (username='$username' AND password = '$pass')";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if ($row[0] > 0){
echo "true";
}else{
echo "false";
}
非常感谢任何帮助!
答案 0 :(得分:0)
修改你的php文件,在json数据中转换结果数据。
$(document).ready(function (){
$("button#login").click(function (){
u = $("#username").val();
p = $("#password").val();
if($("#username").val()==""||$("#password").val()==""){
alert("Please enter username and password");
}else{
var ajax_call = your_servicepath+'PHP/login.php';
var form = $('#loginForm').serialize();
$.ajax({
type: "POST",
url: ajax_call,
data: form,
dataType: "json",
success: function(response){
alert(JSON.stringify(response));
}
});
}
});
});