I'm some kind of lost with the way i have to deal with Javascript, JSON and Perl and most of the examples are in PHP which is not helpfull for me.
I have a page (called main.html) where i have a with data from MySQL and i have an option to delete a row by id.
Then i have the Javascript sending the id to page apagar.html which is some kind mess now cos i was trying to deal with JSON, but with GET it works, and what is missing is a refresh after the delete request. But i wanted to introduce JSON in my code and make the page more dinamic but don't know how.
And in my apagar.html i have only the code to delete if there is any id in the url.
I have read the IBM Series (Mastering Ajax) : http://www.ibm.com/developerworks/web/library/wa-ajaxintro11.html?S_TACT=105AGX08&S_CMP=EDU
Thank you for your help, above is the javascript i'm using:
<script language="javascript" type="text/javascript">
var pedido = false; // pedido = request
try {
pedido = new XMLHttpRequest();
}
catch (failed) {
pedido = false
}
if (!pedido) {
alert("O seu browser não é suportado."); // Browser not supported
}
function Eliminar(rid) { // relativo a main.html & apagar.html
//alert("SK"); //debug
if ( confirm("Deseja realmente eliminar?") ) {
var url = "/back/apagar.html?rid=" + escape(rid);
/*POST*/
pedido.open("POST", url, true);
pedido.onreadystatechange = updatePage;
pedido.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
pedido.send( contacto.toJSONString() );
//pedido.open("GET", url, true);
//pedido.send(null);
}
return false;
}
AND the Mason apagar.html page
<%args>
$rid => ''
</%args>
<%once>
use lib '/var/www/projectox/';
use db::Conexao; #interacao com a DB
use DBI;
use Apache2::Cookie; #interacao com cookies
</%once>
<%init>
% # vê se existem cookies
my $cookies = Apache2::Cookie->fetch($r);
if(!$cookies){ #sem cookies é enviado para o login
$m->redirect('login.html');
}
if($rid) {
# vai eliminar os dados pelo valor do id ($rid)
#ligacao à DB
my $tomada = db::Conexao->Conexao();
my $sql = $tomada->prepare("Delete from Contactos where id=?") ||
die "Impossivel de realizar a operação: $!";
$sql->execute($rid) || die "Não foi possivel executar: $!";
#desliga da DB
$tomada->disconnect || warn "Não foi possivel terminar a ligação!";
</%init>
sorry for making you guys lost your time. I finally resolved my issue:
In my Javascript (using JQuery) i've done:
});
Then in my apagar.html :
and finally in my main.html: