1
Hello, I am trying to connect to Mysql database with a Phonegap/Cordova project and am experiencing problems. First I created the Cordova project and the only change I made was importing Jquery. Then I followed a tutorial to make an ajax call of a php file that should insert the data in the database, all roughly but still does not work. Somebody please help me!
index.html
<!DOCTYPE html>
https://ssl.gstatic.com 'unsafe-Eval'; style-src 'self' 'unsafe-inline'; media-src *"> Hello World
<form id="formUsuario">
<input id="nomeUsuario" type="text" placeholder="Nome"></input> <br>
<input id="sobrenomeUsuario" type="text" placeholder="Sobrenome"></input> <br>
<input id="idadeUsuario" type="text" placeholder="Idade"></input> <br>
<button id="enviar" type="submit" onclick="salvar()">Enviar</button>
</form>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="js/jquery-3.1.0.min.js"></script>
<script type="text/javascript">
function salvar(){
alert('Botão salvar clicado!');
var formula = $('#formUsuario').serialize();
$.ajax({
type: 'POST',
dados: formula,
url: 'http://localhost/projetos/wstest/cadastrar.php',
success: function(data){
if (data == 0) {
alert('Erro ao se comunicar com o banco de dados!');
window.location = "";
}
else if (data == 1) {
alert('Registro salvo com sucesso!');
}
else{
alert('Algo de errado aconteceu!');
}
}
});
}
</script>
</body>
index js.
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
app.initialize();
php connection.
<?php
//Conecta ao banco de dados usando MySQLi
function conectar()
{
try
{
//Localhost
$host = "localhost";
$user = "root";
$password = "";
$database = "webservice";
$conn = mysqli_connect($host, $user, $password, $database);
// mysqli_autocommit($conn, FALSE);
echo 'Conexão bem sucedida!';
} catch (Exception $ex) {
die("Erro de conexão: " . mysqli_connect_error());
}
return $conn;
}
register.php
<?php
include './conexao.php';
header("Access-Control-Allow-Origin: *");
$link = conectar();
$nome = mysqli_real_escape_string($_REQUEST['nomeUsuario']);
$sobrenome = mysqli_real_escape_string($_REQUEST['sobrenomeUsuario']);
$idade = mysqli_real_escape_string($_REQUEST['idadeUsuario']);
$query = "INSERT INTO `usuario`(`nome`, `sobrenome`, `idade`) "
."VALUES ('$nome','$sobrenome','$idade')";
$res = mysqli_query($link, $query);
if($res == true){
$resultado = 1;
}else{
$resultado = 0;
}
echo (json_encode($resultado));
I run this in the browser using Ripple and it doesn’t work. Neither does error Alert I get. Let alone debugging on phone. If anyone can help me, I’d really appreciate it.
In
mysqli_real_escape_string
need to pass the connection as first argument, if the problem is php, test it isolated.– rray
Note that you have an error sending js,
dados: 'formula',
, must bedata: formula,
– Guilherme Lautert
I’ve corrected that part, thank you.
– Guilherme Ramalho
@Guillermo You set with
dados
ordata
? remember that you are English.– Guilherme Lautert