Andreu Adrover Andreu Adrover - 3 months ago 7
jQuery Question

the response of $.ajax POST includes an sql INSERT that I made beforeand I don't know why

I have a jquery code my webpage uses to call a php file that inserts a row in a mysql BD. The problem is I should get data from server in JSON but I receive the data I expect but I recive the sql instruction sent in the php file to the mysql DB too. Why?

jquery code:

$("#botoPreReserva").click(function(){
var dataArribada=$("#dataArribada").val();
var dataSortida=$("#dataSortida").val();
var firstname=$("#firstname").val();
var lastname=$("#lastname").val();
var email=$("#email").val();
var message=$("#message").val();
var req=$.ajax({
method:"POST",
url:"comprovardates.php",
data:{arribada:dataArribada,
sortida:dataSortida,
firstname:firstname,
lastname:lastname,
email:email,
message:message},
dataType:"json"


});

req.done(function(result){

if(result.error != "0"){
$("#resultatPreReserva").text(result.errorTexte);
}
else{
$("#resultatPreReserva").text(" ");
//posar el calendari a l'inici de la preReserva
$.post("nomMes.php", {mesN:result.mesPre },
function(result2){
$("#mes").text(result2);
});
$("#any").text(result.anyPre);
$("#taulaCalendari").load("calendari.php",{actualmonth:result.mesPre, actualyear:result.anyPre});

alert(result.result);
}

});
req.fail(function(jqXHR, textStatus){
alert(textStatus);


});


and this is a comprovardades.php simplified:

$calendari= new Calendar(SERVERNAME,DBNAME,USERNAME,PASSWORD);

//conectam a la base de dades
$calendari->connect();

// onsultam reserves d'agost del 2016
$min= new DateTime("2016-11-01");$max= new DateTime("2016-11-31"); $tipus="preReserva";
$event = new Event(); $event->setEntrada($min); $event->setSortida($max); $event->setTipus($tipus);

$eventList=$calendari->insertEvent($event);

echo json_encode(array("error"=>"0","errorTexte"=>"prova","result"=>"result","mesPre"=>"01","anyPre"=>"2016"));


where connect():

function connect(){
try {
$this->conn = new PDO("mysql:host=$this->servername; dbname=$this->dbname", $this->username, $this->password);
// set the PDO error mode to exception
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
error_log("Connected successfully");
}
catch(PDOException $e)
{
error_log("Connection failed: " . $e->getMessage());
}
}


where insertEvent():

function insertEvent(Event $event){

$sql="INSERT INTO reserves(entrada,sortida,tipus,summary)"
. " VALUES ('".$event->getEntrada()->format('Y-m-d')."','".$event->getSortida()->format('Y-m-d')."','".$event->getTipus()."','".$event->getSummary()."')";
echo $sql;
try{
$this->conn->exec($sql);
error_log("SQL:".$sql);
}
catch(PDOException $e)
{
error_log("INSERT failed " . $e->getMessage());
}

}
}


the response I get back to jquery something like


INSERT INTO reserves(entrada,sortida,tipus,summary) VALUES ('2016-11-01','2016-12-01','preReserva',''
){"error":"0","errorTexte":"prova","result":"result","mesPre":"01","anyPre":"201>6"}


The only way i found to solve this problem is using ob_clean before the echo in the php file but I think it has to be a better way. I'm a noob and not sure if I skip somthing

Thanks,

Answer

where insertEvent():

Commented out echo statement of query

function insertEvent(Event $event){

$sql="INSERT INTO reserves(entrada,sortida,tipus,summary)"
        . " VALUES ('".$event->getEntrada()->format('Y-m-d')."','".$event->getSortida()->format('Y-m-d')."','".$event->getTipus()."','".$event->getSummary()."')";
//echo $sql; // commented this echo 
try{
  $this->conn->exec($sql);
  error_log("SQL:".$sql);
}
catch(PDOException $e)
{
  error_log("INSERT failed " . $e->getMessage());
}

}
}
Comments