在我的PHP Web服务中,我有一个函数:
function create_promo($codeBarre, $designation, $dateDebut, $dateFin, $prix, $TVA)
{
$db = new PDO('mysql:host=localhost;dbname=phone', 'root', '');
$req = $db->prepare("insert into promotion
values
(:codeBarre, :designation, :dateDebut, :dateFin, :prix, :TVA)");
$req->execute(array(':codeBarre'=>$codeBarre, ':designation'=>$designation,
':dateDebut'=>date("Y-m-d", strtotime($dateDebut)),
':dateFin'=>date("Y-m-d", strtotime($dateFin)),
':prix'=>$prix, ':TVA'=>$TVA));
return "promotion créer";
}
此功能位于C:\ Wamp \ WWW。
下我创建了我的Cordova项目并编写了这段代码:
<script type="text/javascript" src="js/jquery.js"></script>
<title>Hello World</title>
<script type="text/javascript">
$(document).ready(function() {
$("#Ajouter").click(function() {
var codeBarre = $("#codeBarre").val();
var designation = $("#designation").val();
var dateDebut = $("#dateDebut").val();
var dateFin = $("#dateFin").val();
var prix = $("#prix").val();
var TVA = $("#TVA").val();
var dataString = "codeBarre=" + codeBarre + "&designation=" + designation + "&dateDebut=" + dateDebut + "&dateFin=" + dateFin + "&prix=" + prix + "&TVA=" + TVA + "&Ajouter=";
$.ajax({
url:"https://192.168.1.12/testphone/server.php//create_promo",
type:"POST",
data:dataString,
crossDomain:true,
cache:false,
beforeSend: function(){ $("#insert").val('Connecting...');},
success: function(data){
if (data=="ok")
{
alert("inserted");
$("#Ajouter").val('submit');
}
else if (data=="error")
{
alert("error");
}
}
});
return false;
});
});
</script>
</head>
<body>
<!--<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>-->
<form method="post">
<table>
<tr>
<td>Code Barre:</td>
<td> <input type="text" id="codeBarre"></td>
</tr>
<tr>
<td>Designation:</td>
<td> <input type="text" id="designation"></td>
</tr>
<tr>
<td>Date Debut:</td>
<td> <input type="date" id="dateDebut"></td>
</tr>
<tr>
<td>Date fin:</td>
<td><input type="date" id="dateFin"></td>
</tr>
<tr>
<td>Prix:</td>
<td> <input type="number" id="prix" step="any"></td>
</tr>
<tr>
<td>TVA:</td>
<td> <input type="number" id="TVA"></td>
</tr>
<tr>
<td> <input type="button" id="Ajouter" value="Ajouter"></td>
</tr>
</table>
</form>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
Cordova项目位于C:目录中。 警报消息未显示;我怎么能显示错误?
答案 0 :(得分:0)
这可以生成,因为在设备准备就绪时脚本没有加载,尝试更改
$(document).ready(function(){
...
到
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady(){
$("#Ajouter").click(function() {
//.... rest of code
}
添加错误函数以在请求失败时捕获
$.ajax({
url:"https://192.168.1.12/testphone/server.php//create_promo",
type:"POST",
data:dataString,
crossDomain:true,
cache:false,
beforeSend: function(){ $("#insert").val('Connecting...');},
success: function(data){
if (data=="ok")
{
alert("inserted");
$("#Ajouter").val('submit');
}
else if (data=="error")
{
alert("error");
}
},
error: function(err){
alert('error'+err);
}
});
我还建议您更改输入的类型:
<input type="submit" id="Ajouter" value="Ajouter">
并将点击事件更改为提交,并使用“阻止默认值”,如下所示:
$("#Ajouter").on('submit',function(e){
e.preventDefault();
以下是您的代码:std::stoi