Reputation:
Can someone help me with this I have this php file for insert data into my database and all is sent by ajax function
<?php
header('Content-type: application/json');
ini_set('max_execution_time', 300);
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');
$mysqli = new mysqli('localhost', 'root', '', 'ebspma');
// Works as of PHP 5.2.9 and 5.3.0.
if ($mysqli->connect_error) {
die('Connect Error: ' . $mysqli->connect_error);
}
$num= $_POST['num'];
$dia = $_POST['dia'];
$sala = $_POST['sala'];
$hora = explode(",", $_POST['hora']);
$a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17];
$c = array_fill_keys(array_keys(array_diff($a,$hora)),null) + $a;
ksort($c);
for ($i=0; $i<17; $i++){
$horas = $c[$i];
$stmt = $mysqli->prepare("INSERT INTO `ebspma`.`sala_ocupacao` (id_dia, id_sala, id_tempo) VALUES (?, ?, ?);")or die(mysql_error($mysqli));
$stmt->bind_param('ssi', $dia, $sala, $horas);
if(!$stmt->execute()){
echo json_encode(array('status' => 'error','message'=> 'Opppss...Os Registo(s) não foram gravado(s)'));
}
else{
echo json_encode(array('status' => 'success','message'=> 'Registo(s) gravado(s) com sucesso'));
}
$stmt->close();
}
$mysqli->close();
?>
And this my ajax function
function postData(){
var dia = document.getElementById('dia').value;
var sala = document.getElementById('sala').value;
var tempos = [];
var s = document.getElementById('hora');
for (var i = 0; i < s.options.length; i++) {
if (s.options[i].selected == true) {
var valores = s.options[i].value;
tempos.push(valores);
}
}
console.log(tempos);
var num = document.getElementById('num').value;
var data = 'dia='+ dia + '&sala='+ sala + '&hora='+ tempos + '&num='+ num;
$.ajax({
type: "POST",
dataType: "html",
url: "registerBd.php",
data: data,
success: function (response) {
console.log(response);
$('#ajaxDivOk').css("display", "block");
$('#ajaxDivOk').html("Registo(s) gravado(s) com sucesso");
alert(response);
},
error:function(response){
console.log("Aqui 2");
alert("something went wrong");
}});
return false;
}
Called by my form like this
<form method="post" id="salas" name="salas" onsubmit="postData()" >
But i have always error in my function even my data is inserted into dabase
New update
New insert php named registerBd.php
<?php
header('Content-type: application/json');
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');
$mysqli = new mysqli('localhost', 'root', '', 'ebspma');
if ($mysqli->connect_error) {
die('Connect Error: ' . $mysqli->connect_error);
}
$num = $_POST['num'];
$dia = $_POST['dia'];
$sala = $_POST['sala'];
$hora = $_POST['hora'];
$a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17];
$c = array_fill_keys(array_keys(array_diff($a, $hora)), null) + $a;
ksort($c);
for ($i = 0; $i < 17; $i++) {
$horas = $c[$i];
$stmt = $mysqli->prepare("INSERT INTO `ebspma`.`sala_ocupacao` (id_dia, id_sala, id_tempo) VALUES (?, ?, ?);")or die(json_encode(mysqli_error($mysqli)));
$stmt->bind_param('ssi', $dia, $sala, $horas);
if (!$stmt->execute()) {
echo json_encode(array('status' => 'error', 'message' => 'Opppss...Os Registo(s) não foram gravado(s)'));
}
}
echo json_encode(array('status' => 'success', 'message' => 'Registo(s) gravado(s) com sucesso'));
$stmt->close();
unset($stmt);
?>
And modification to ajax
<script>
function postData(){
var dia = document.getElementById('dia').value;
var sala = document.getElementById('sala').value;
var tempos = [];
var s = document.getElementById('hora');
for (var i = 0; i < s.options.length; i++) {
if (s.options[i].selected == true) {
var valores = s.options[i].value;
tempos.push(valores);
}
}
console.log(tempos);
var num = document.getElementById('num').value;
$.ajax({
type: "POST",
dataType: "json",
url: "registerBd.php",
data : {'dia': dia, 'sala': sala, 'hora': tempos, 'num': num},
success: function (response) {
console.log(response);
$('#ajaxDivOk').css("display", "block");
$('#ajaxDivOk').html("Registo(s) gravado(s) com sucesso");
alert(response);
},
error: function(jq,status,message) {
console.log( message );
alert('A jQuery error has occurred. Status: ' + status + ' - Message: ' + message);
}
});
return false;
}
</script>
But still have the same problem, and in chrome console the registerBd.php is red after the insert, so i think the problem should be here even if everything is recorded into database
New Update...this is getting really strange Removed 2 lines in registerDb.php
unset($smtp);
?>
With debug and without i get this check image http://postimg.org/image/8wtwxod3r/
Without debuger http://postimg.org/image/5nctzrjkv/
Check at the bottom it seems that the registerBd.php is canceled...why?
So now i'm lost
Update 3
Where i call my ajax function
<form method="post" id="salas" name="salas" onsubmit="postData()" >
My ajax function
<script>
function postData(){
var dia = document.getElementById('dia').value;
var sala = document.getElementById('sala').value;
var tempos = [];
var s = document.getElementById('hora');
for (var i = 0; i < s.options.length; i++) {
if (s.options[i].selected == true) {
var valores = s.options[i].value;
tempos.push(valores);
}
}
console.log(tempos);
var num = document.getElementById('num').value;
$.ajax({
type: "POST",
dataType: "json",
url: "registerBd.php",
data : {'dia': dia, 'sala': sala, 'hora': tempos, 'num': num},
success: function (response) {
console.log(response);
$('#ajaxDivOk').css("display", "block");
$('#ajaxDivOk').html("Registo(s) gravado(s) com sucesso");
alert(response);
},
error: function(jq,status,message) {
console.log( message );
alert('A jQuery error has occurred. Status: ' + status + ' - Message: ' + message);
}
});
return false;
}
</script>
and my registerBd.php
<?php
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');
$mysqli = new mysqli('localhost', 'root', '', 'ebspma');
if ($mysqli->connect_error) {
die('Connect Error: ' . $mysqli->connect_error);
}
$num = $_POST['num'];
$dia = $_POST['dia'];
$sala = $_POST['sala'];
$hora = $_POST['hora'];
$a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17];
$c = array_fill_keys(array_keys(array_diff($a, $hora)), null) + $a;
ksort($c);
for ($i = 0; $i < 17; $i++) {
$horas = $c[$i];
$stmt = $mysqli->prepare("INSERT INTO `ebspma`.`sala_ocupacao` (id_dia, id_sala, id_tempo) VALUES (?, ?, ?);")or die(json_encode(mysqli_error($mysqli)));
$stmt->bind_param('ssi', $dia, $sala, $horas);
if (!$stmt->execute()) {
echo json_encode(array('status' => 'error', 'message' => 'Opppss...Os Registo(s) não foram gravado(s)'));
}
}
$response = "Registo(s) gravado(s) com sucesso";
echo json_encode($response);
$stmt->close();
In real mode i have this
Request URL: http://localhost/multiple/registerBd.php
Method: POST
Status: Request was cancelled.
Request Headers
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Origin: http://localhost
Referer: http://localhost/multiple/registo_salas.php
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.44 (KHTML, like Gecko) JavaFX/8.0 Safari/537.44
X-Requested-With: XMLHttpRequest
Request data
dia=2&sala=5&hora%5B%5D=1&hora%5B%5D=2&hora%5B%5D=3&num=3
CallStack
send ([native code]:0:0)
send (https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js:4:25552)
ajax (https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js:4:21305)
postData (registo_salas.php:104:26)
onsubmit (registo_salas.php:116:27)
Update 4
I don't know if if this what you mean...but i don't think it is (my fault...)
<script>
$('#salas').on('submit', function(event) {
event.preventDefault();
function postData(){
var dia = document.getElementById('dia').value;
var sala = document.getElementById('sala').value;
var tempos = [];
var s = document.getElementById('hora');
for (var i = 0; i < s.options.length; i++) {
if (s.options[i].selected == true) {
var valores = s.options[i].value;
tempos.push(valores);
}
}
console.log(tempos);
var num = document.getElementById('num').value;
event.preventDefault();
$.ajax({
type: "POST",
dataType: "json",
url: "registerBd.php",
data : {'dia': dia, 'sala': sala, 'hora': tempos, 'num': num},
success: function (response) {
console.log(response);
$('#ajaxDivOk').css("display", "block");
$('#ajaxDivOk').html("Registo(s) gravado(s) com sucesso");
alert(response);
},
error: function(jq,status,message) {
console.log( message );
alert('A jQuery error has occurred. Status: ' + status + ' - Message: ' + message);
}
});
return false;
}
});
i get
Uncaught ReferenceError: postData is not definedonsubmit @ registo_salas.php:120
Upvotes: 0
Views: 194
Reputation: 91734
I see a few problems here:
dataType: "html",
in your ajax call. You should change that to dataType: "json",
or die(mysql_error($mysqli));
mysql_*
function as you are using mysqli_*
. You need something like:or die(mysqli_error($mysqli));
or die(json_encode(mysqli_error($mysqli)));
var data = {'dia': dia, 'sala': sala, etc.};
Edit: Based on your last update, it seems that you are not cancelling the default submit event. You do return false
from your function, but you don't do anything with that value.
You should change:
<form method="post" id="salas" name="salas" onsubmit="postData()" >
to:
<form method="post" id="salas" name="salas" onsubmit="return postData();" >
Although, as you are using jQuery, a better solution would be to remove the inline javascript and move it to your script section:
$('form').on('submit', function(event) {
event.preventDefault();
// the rest of your javascript that is now in your `postData()` function
});
Upvotes: 1