简体   繁体   中英

Data not inserting into MySql remote databse via Phonegap app

I am trying to set up an android app with Jquery & PHP using Ajax to post the data from a form into a database on a remote server. It all works fine in local testing, but when I build the app and try to do it via my tablet it doesn't work at all.

I imagine it's something very simple which i'm doing wrong. Here is the code I am using:

<!DOCTYPE html>
<html>
<head>
<title>Submit a form via AJAX</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a4/jquery.mobile-1.0a4.min.css" />
<script src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0a4/jquery.mobile-1.0a4.min.js"></script>
</head>
<body>
<script>
function onSuccess(data, status)
{
data = $.trim(data);
$("#notification").text(data);
}

function onError(data, status)
{
// handle an error
}        

$(document).ready(function() {
$("#submit").click(function(){
var formData = $("#callAjaxForm").serialize();

$.ajax({
type: "POST",
url: "http://xxx.co.uk/cgi-bin/process.php",
cache: false,
data: formData,
success: onSuccess,
error: onError
});

return false;
});
});
</script>

<!-- call ajax page -->
<div data-role="page" id="callAjaxPage">
<div data-role="header">
<h1>Name Form</h1>
</div>

<div data-role="content">
<form id="callAjaxForm">
<div data-role="fieldcontain">
<label for="firstName">First Name</label>
<input type="text" name="firstName" id="firstName" value=""  />
<label for="lastName">Last Name</label>
<input type="text" name="lastName" id="lastName" value=""  />
<h3 id="notification"></h3>
<button data-theme="b" id="submit" type="submit">Submit</button>
</div>
</form>
</div>
<div data-role="footer">
</div>
</div>
</body>
</html>

As you can see it's very basic - I have found a tutorial and used the code from that for testing.

The php code is:

<?php
header("Access-Control-Allow-Origin: *");
include("conn.inc");

$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];

$q1 = "insert into customers (Name,Contact) values ('$firstName','$lastName')";
$r1 = mysqli_query($cxn,$q1) or die ('Error Q1');

?>

Again very basic as i'm a newbie - but any help as to why the data submitted doesn't post when done via android would be massively appreciated.

Many thanks

Check with an alert if your deviceready is fired or not.

use this

$(document).on('pageshow', "#callAjaxPage",function () { ...... });

instead of this

$(document).ready(function() { ...... });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM