在phonegap上使用jquery在Xampp上插入数据
我正在开发一个使用phonegap在mysql数据库(xampp)上保存记录的简单应用程序。然而,我在某处被绊倒了。 我的代码一直在写“连接…”而不保存数据库中的记录。我一直在研究,但我似乎无法解决问题。请告诉我代码中的错误?谢谢 也许是url,我不确定, 注意:我的url来自phonegap服务器在phonegap上使用jquery在Xampp上插入数据,jquery,cordova,mysqli,phonegap-desktop-app,Jquery,Cordova,Mysqli,Phonegap Desktop App,我正在开发一个使用phonegap在mysql数据库(xampp)上保存记录的简单应用程序。然而,我在某处被绊倒了。 我的代码一直在写“连接…”而不保存数据库中的记录。我一直在研究,但我似乎无法解决问题。请告诉我代码中的错误?谢谢 也许是url,我不确定, 注意:我的url来自phonegap服务器 index.html <link rel="stylesheet" type="text/css" href="css/index.css" />
index.html
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>Hello World</title>
</head>
<body>
<script src="jquery-3.1.1.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
$(document).ready(function()
{
$("#insert").click(function(){
var firstname=$("#firstname").val();
var lastname=$("#lastname").val();
var phone=$("#phone").val();
var dataString="firstname="+firstname+"&lastname="+lastname+"&phone="+phone+"&insert=";
if($.trim(firstname).length>0 & $.trim(lastname).length>0 & $.trim(phone).length>0)
{
$.ajax({
type: "POST",
url: "http://10.250.216.195:3000/insert.php",
data: dataString,
crossDomain: true,
cache: false,
beforeSend: function(){ $("#insert").val('Connecting...');},
success: function(data){
if(data=="success"){
console.log(data);
alert("inserted");
$("insert").val('submit');
}
else if (data=="error"){
console.log(data);
alert("error");
}
}
});
} return false;
});
}
);
</script>
<div data-role ="page" id ="page1">
<div data-role ="header">
<h1>Hello World</h1>
<center><h3>Register</h3>
<input type="hidden" id="id" value=""/>
<label for ="Firstname" class="ui-hidden-accessible"></label> <input type = "text" id="firstname" placeholder ="firstname" required="required"><br>
<label for ="Lastname" class="ui-hidden-accessible"></label> <input type = "text" id="lastname" placeholder ="lastname" required="required"><br>
<label for ="Phone" class="ui-hidden-accessible"></label> <input type = "text" id="phone" placeholder ="Phone" required="required"><br>
<input type="button" value="insert" id = "insert"></center>
</div>
insert.php
<?php
include "dbconfig.php";
if(isset($_POST['insert']))
{
$first=$_POST['firstname'];
$last=$_POST['lastname'];
$phone=$_POST['phone'];
$first = mysqli_real_escape_string($con,$_POST['firstname']);
$last = mysqli_real_escape_string($con,$_POST['lastname']);
$phone = mysqli_real_escape_string($con,$_POST['phone']);
$q=mysqli_query($con,"INSERT INTO phone (firstname,lastname,phone) VALUES ('$first','$last','$phone')");
if($q)
echo "success";
else
echo "error";
}
?>
index.html
你好,世界
app.initialize();
$(文档).ready(函数()
{
$(“#插入”)。单击(函数(){
var firstname=$(“#firstname”).val();
var lastname=$(“#lastname”).val();
var phone=$(“#phone”).val();
var dataString=“firstname=“+firstname+”&lastname=“+lastname+”&phone=“+phone+”&insert=“;
if($.trim(firstname).length>0&$.trim(lastname).length>0&$.trim(phone).length>0)
{
$.ajax({
类型:“POST”,
url:“http://10.250.216.195:3000/insert.php",
数据:dataString,
跨域:是的,
cache:false,
beforeSend:function(){$(“#insert”).val('Connecting…');},
成功:功能(数据){
如果(数据=“成功”){
控制台日志(数据);
警告(“插入”);
$(“插入”).val(“提交”);
}
else if(数据==“错误”){
控制台日志(数据);
警报(“错误”);
}
}
});
}返回false;
});
}
);
你好,世界
登记
insert.php
如果您使用的是XAMPP服务器,我假定该服务器与您开发的计算机在同一台计算机上
如果是这种情况,请使用google chrome在浏览器中测试phonegap应用程序,并将URL更改为:
http://10.250.216.195/insert.php
端口3000用于phonegap构建测试。上面的URL应该可以正常工作。我想没有人知道这一点:(是的,我使用的是同一台计算机,我确实在没有端口3000的情况下将URL更改为上面提到的URL,但它仍然不工作。