Jump to content

gmc1103

Members
  • Posts

    251
  • Joined

  • Last visited

Everything posted by gmc1103

  1. Done You said But, I'm still not understanding why you are creating the initial values in JavaScript instead of the back-end code. What do you mean by that?? Thanks for your help.
  2. Hi Thanks for your help You said Change getSalas.php to return the "Choose a value" option in the list it returns My getSalas.php is <?php error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); include 'conn.php'; mysql_query("SET NAMES 'utf8'"); $rs = mysql_query('SELECT `idsala`, `sala` FROM `ebspma_paad_ebspma`.`req_material_sala`;')or die ("Error in query: $rs. ".mysql_error()); $rows = array(); while(($row = mysql_fetch_assoc($rs))) { $rows[] = $row; } echo json_encode($rows); ?> How can i do this?
  3. Hi I'm having a problem with this code <script> $(function(){ var items=""; $.getJSON("getSalas.php",function(data){ $.each(data,function(index,item) { items+="<option value='"+item.idsala+"'>"+item.sala+"</option>"; }); $("#sala").html(items); }); }); </script> When the page load i would like to have the text "Escolha a sala" (Choose a value) I have this code in my htlm <div class="form-group"> <label for="exampleInputPassword1">Sala</label> <select class="form-control" id="sala" name="sala" onchange="verificaSala(this)"> <option selected="selected">Escolha a sala</option> </select> </div> But this text is not show....any help? Thanks
  4. Thanks mac_gyver I have modified my code <?php header('Content-Type: application/json'); error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); $mysqli = new mysqli('xxxxx', 'xxxxx', 'xxxxx'); if (mysqli_connect_errno()) { trigger_error('Database connection failed: ' . mysqli_connect_error(), E_USER_ERROR); } $id = intval($_REQUEST['id']); $dia = $_POST['dia']; $sala = $_POST['sala']; $hora = $_POST['hora']; $sql = "SELECT `id_dia` FROM `ebspma_paad_ebspma`.`semana_dias` where dia = ? "; $stmt = $mysqli->prepare($sql); if ($stmt === false) { trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $mysqli->error, E_USER_ERROR); } $stmt->bind_param('i', $dia); $stmt->execute(); $stmt->bind_result($iddia); $stmt->fetch(); $stmt->close(); echo ("Este é o idDia: " + $iddia); $sql = "SELECT `idsala` FROM `ebspma_paad_ebspma`.`req_material_sala` WHERE sala = ? "; $stmt = $mysqli->prepare($sql); if ($stmt === false) { trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $mysqli->error, E_USER_ERROR); } $stmt->bind_param('i', $sala); $stmt->execute(); $stmt->bind_result($idsala); $stmt->fetch(); $stmt->close(); echo ("Este é o $idsala: " + $idsala); $sql = "SELECT COUNT(id_dia) FROM `ebspma_paad_ebspma`. `sala_ocupacao` WHERE id_dia = ? AND id_sala= ? AND id_tempo = ? "; $stmt = $mysqli->prepare($sql); if ($stmt === false) { trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $mysqli->error, E_USER_ERROR); } $stmt->bind_param('iii', $iddia, $idsala, $hora); $stmt->execute(); $stmt->bind_result($existe); $stmt->fetch(); $stmt->close(); echo ("Este é o existe: " + $existe); if ($existe != 0) { echo json_encode(array('status' => 'error', 'message' => 'Este dia e tempo já estão registados na base de dados')); } else { $sql = "update `ebspma_paad_ebspma`. `sala_ocupacao` set id_dia = ?, id_sala= ?, id_tempo= ? where id_ocup = ?"; $stmt = $mysqli->prepare($sql); if ($stmt === false) { trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $mysqli->error, E_USER_ERROR); } $stmt->bind_param('iiii', $iddia, $idsala, $hora, $id); if (!$stmt->execute()) { echo json_encode(array('status' => 'error', 'message' => 'Opppss...O Registo não foi atualizado')); } $stmt->close(); echo json_encode(array('status' => 'success', 'message' => 'O Registo foi atualizado corretamente')); } and now it does work Thank you again
  5. Hi I need some help resolving this issue This is my code <?php header('Content-Type: application/json'); error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); $mysqli = new mysqli('localhost','xxxxxx','xxxxx'); if ($mysqli->connect_error) { die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error); } $id = intval($_REQUEST['id']); $dia = $_POST['dia']; $sala = $_POST['sala']; $hora = $_POST['hora']; $query = "SELECT `id_dia` FROM `semana_dias` where dia = ? " or trigger_error($mysqli->error); if ($stmt = $mysqli->prepare($query)) { $stmt->bind_param('i', $dia); $stmt->execute(); $stmt->bind_result($iddia); $stmt->fetch(); printf("Error: %d.\n", $stmt->errno); $stmt->close(); } $query1 = "SELECT `idsala` FROM `req_material_sala` WHERE sala = ? " or trigger_error($mysqli->error); if ($stmt = $mysqli->prepare($query1)) { $stmt->bind_param('i', $sala); $stmt->execute(); $stmt->bind_result($idsala); $stmt->fetch(); printf("Error: %d.\n", $stmt->errno); $stmt->close(); } echo "Dados"; echo "-----"; echo $iddia; echo "-----"; echo $idsala; echo "-----"; echo $hora; echo "-----"; $query2 = "SELECT COUNT(id_dia) FROM sala_ocupacao WHERE id_dia = ? AND id_sala= ? AND id_tempo = ? " or trigger_error($mysqli->error); $result = $mysqli->prepare($query2); $result->bind_param('sss', $iddia, $idsala, $hora); $result->execute(); $numberofrows = $result->fetchColumn(); echo $numberofrows; if ($numberofrows != 0) { echo json_encode(array('status' => 'error', 'message' => 'Este dia e tempo já estão registados na base de dados')); } else { $query3 = "update sala_ocupacao set id_dia = ?, id_sala= ?, id_tempo= ? where id_ocup = '$id'" or trigger_error($mysqli->error); if ($stmt = $mysqli->prepare($query3)) { $stmt->bind_param('iii', $iddia, $idsala, $hora); if (!$stmt->execute()) { echo json_encode(array('status' => 'error', 'message' => 'Opppss...Os Registo(s) não foram gravado(s)')); } $stmt->close(); echo json_encode(array('status' => 'success', 'message' => 'O Registo foi atulaizado corretamente')); } } It's to update some records into the database but que queries are not keeping the result (iddia, idsala) to the last query Notice: Undefined variable: iddia in /home/ebspma/public_html/gestaosalas/update_sala_ocup.php on line 37 Notice: Undefined variable: idsala in /home/ebspma/public_html/gestaosalas/update_sala_ocup.php on line 39 Fatal error: Call to a member function bind_param() on a non-object in /home/ebspma/public_html/gestaosalas/update_sala_ocup.php on line 46 The post are this one POST http://ebspma.edu.pt/gestaosalas/update_sala_ocup.php?id=61 HTTP/1.1 Host: ebspma.edu.pt Connection: keep-alive Content-Length: 26 Cache-Control: max-age=0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Origin: http://ebspma.edu.pt User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Content-Type: application/x-www-form-urlencoded Referer: http://ebspma.edu.pt/gestaosalas/docCheckSalas.php Accept-Encoding: gzip, deflate Accept-Language: pt-PT,pt;q=0.8,en-US;q=0.6,en;q=0.4,es;q=0.2,fr;q=0.2 dia=Segunda&sala=1&hora=11 Any help please? Thanks
  6. Hi It's now fixed...i receive the json {"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"} But in my ajax function is not receiving the result this is my 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; console.log(num); $.ajax({ type: "POST", dataType: 'json', url: "registerBd.php", data : {'dia': dia, 'sala': sala, 'hora': tempos, 'num': num}, success: function(data){ var result = data.status; console.log(result); if(result == 'success'){ $('#ajaxDivOk').html(data.status +':' + data.message); $("#ajaxDivOk").fadeIn(1500); } else{ $("#ajaxDivErro").html('Este dia já está registado na base de dados, use update'); $("#ajaxDivErro").fadeIn(1500); } } }); return false; } and is was working before moving to the remote server...so i don't get the information if the records have been inserted or not
  7. Thanks for your help So my table field id_tempo must be set to null to accept? Where do i must put $mysql->error? Thanks again
  8. Just to add some information more the json received is really strange 1{"status":"success","message":"Os Registo(s) foram gravado(s)"} 2{"status":"success","message":"Os Registo(s) foram gravado(s)"} 3{"status":"success","message":"Os Registo(s) foram gravado(s)"} 4{"status":"success","message":"Os Registo(s) foram gravado(s)"} {"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"} {"status":"success","message":"Os Registo(s) foram gravado(s)"} {"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"} {"status":"success","message":"Os Registo(s) foram gravado(s)"} {"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"} {"status":"success","message":"Os Registo(s) foram gravado(s)"} {"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"} {"status":"success","message":"Os Registo(s) foram gravado(s)"} {"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"} {"status":"success","message":"Os Registo(s) foram gravado(s)"} {"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"} {"status":"success","message":"Os Registo(s) foram gravado(s)" }{"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"} {"status":"success","message":"Os Registo(s) foram gravado(s)"} {"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"} {"status":"success","message":"Os Registo(s) foram gravado(s)"} {"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"} {"status":"success","message":"Os Registo(s) foram gravado(s)"} {"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"} {"status":"success","message":"Os Registo(s) foram gravado(s)"} {"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)" }{"status":"success","message":"Os Registo(s) foram gravado(s)"} {"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"} {"status":"success","message":"Os Registo(s) foram gravado(s)"} {"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"} {"status":"success","message":"Os Registo(s) foram gravado(s)"} As you can see...only 4 are correct...then...it says success and error in the same time... Whats wrong?
  9. Hi, no even worst, nothing is inserted into database
  10. Hi Barand It was, but i make some changes and put again ALTER TABLE `sala_ocupacao` CHANGE `id_ocup` `id_ocup` INT( 11 ) NOT NULL AUTO_INCREMENT , CHANGE `id_dia` `id_dia` INT( 11 ) NOT NULL , CHANGE `id_sala` `id_sala` INT( 11 ) NOT NULL , CHANGE `id_tempo` `id_tempo` INT( 11 ) NOT NULL ; But it still insert only the value selected in the form
  11. Hi I have this code and the strange is that all the array is not inserted into database, just a the values sent from the form <?php header('Content-Type: application/json'); error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); $mysqli = new mysqli("localhost", "xxxxx", "xxxxx", "xxxx"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } echo $mysqli->host_info . "\n"; $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); $query = "SELECT COUNT(`id_dia`) FROM sala_ocupacao WHERE `id_dia` = ? AND `id_sala`= ?" or trigger_error($mysqli->error); if ($stmt = $mysqli->prepare($query)) { $stmt->bind_param('ss', $dia, $sala); $stmt->execute(); $stmt->bind_result($existe); $stmt->fetch(); printf("Error: %d.\n", $stmt->errno); $stmt->close(); } if ($existe != 0) { $response = "Este dia já está registado na base de dados, use update"; echo json_encode(array('status' => 'error', 'message' => 'Este dia já está registado na base de dados, use update')); } else { for ($i = 0; $i < 17; $i++) { $horas = $c[$i]; $stmt = $mysqli->prepare("INSERT INTO `ebspma_paad_ebspma`.`sala_ocupacao` (id_dia, id_sala, id_tempo) VALUES (?, ?, ?);")or die(json_encode(mysqli_error($mysqli))); $stmt->bind_param('sss', $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'=> 'Os Registo(s) foram gravado(s)')); } $stmt->close(); } Anything wrong with this code for that issue?
  12. Hi Barand Thank you, the result inside the smtp query is 1 too, and should 17...i think
  13. Hi I'm testing a query, this one SELECT COUNT(sala_ocupacao.`id_dia`) AS ocup FROM sala_ocupacao WHERE sala_ocupacao.`id_dia` = 1 AND sala_ocupacao.`id_sala`= 1 and the return is 17 When i use the prepared statement in php file $stmt = $mysqli->prepare("SELECT COUNT(sala_ocupacao.`id_dia`) FROM sala_ocupacao WHERE sala_ocupacao.`id_dia` = ? AND sala_ocupacao.`id_sala`= ? "); $stmt->bind_param('ss', $dia, $sala); $existe = $stmt->execute(); if ($existe != 0) { echo "Values found"; } The $existe variable gives me 1...and boolean...is normal? Regards
  14. But now i have a problem running my query in php file <?php error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); include 'conn.php'; mysql_query("SET NAMES 'utf8'"); $rs = mysql_query('SELECT `semana_dias`.`dia` , `req_material_sala`.`sala` , `req_material_tempo`.`inicio` FROM sala_ocupacao INNER JOIN `semana_dias` ON (`sala_ocupacao`.`id_dia` = `semana_dias`.`id_dia`) INNER JOIN `req_material_sala` ON (`sala_ocupacao`.`id_sala` = `req_material_sala`.`idsala`) LEFT JOIN `req_material_tempo` ON (`sala_ocupacao`.`id_tempo` = req_material_tempo.`idtempo`) ORDER BY sala_ocupacao.`id_ocup`id_ocup;'); $result = array(); while($row = mysql_fetch_object($rs)){ array_push($result, $row); } echo json_encode($result); ?> i get this error </span> Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\multiple\get_ocupacao.php on line <i>16 ??? Whats wrong now i get this error
  15. Hi Barand You again, thank you, it was the fix for my issue, now i have You are the man Best regards
  16. Hi I have this query SELECT COALESCE(id_tempo, 0) AS id_tempos ,`semana_dias`.`dia` , `req_material_sala`.`sala` , `req_material_tempo`.`inicio` FROM sala_ocupacao INNER JOIN `semana_dias` ON (`sala_ocupacao`.`id_dia` = `semana_dias`.`id_dia`) INNER JOIN `req_material_sala` ON (`sala_ocupacao`.`id_sala` = `req_material_sala`.`idsala`) INNER JOIN `req_material_tempo` ON (`sala_ocupacao`.`id_tempo` = req_material_tempo.`idtempo`) ORDER BY id_ocup; That one returns only the values where there is no null If i remove those lines , `req_material_tempo`.`inicio` INNER JOIN `req_material_tempo` ON (`sala_ocupacao`.`id_tempo` = req_material_tempo.`idtempo`) I have the null values converted to zero but i don't have the column where i can get the names instead of the id How can i achive this?? Something like the picture with the 0 or null values Thanks for any help
  17. Well after some experience i getting nut... i receive the error alert in my function but the data is inserted into database <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; var data = 'dia='+ dia + '&sala='+ sala + '&hora='+ tempos + '&num='+ num; $.ajax({ type: "POST", dataType: "json", url: "registerBd.php", data: data, cache: false, success: function () { console.log("Aqui"); $('#ajaxDivOk').css("display", "block"); $('#ajaxDivOk').html("Registo(s) gravado(s) com sucesso"); }, error:function(){ console.log("Aqui 2"); alert("something went wrong"); } }); return false; } </script> So...is not receiving the json_encode from my php....but why? <?php header('Access-Control-Allow-Origin: *'); 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 $stmt->error; $stmt->close(); } $response = "Registo gravado"; echo json_encode($response); ?> Any help?? Thanks
  18. <tr> <td colspan="2" align="center" valign="middle" class="tg-6997"> <div class="col-sm-6"> <div id="ajaxDivOk" style="display:none" class="alert alert-success"></div> </div> </td> </tr> Well...now or i'm stupid or i don't see anything In netbeans if i run normally....nothing is showed... In netbeans if i run in debug mode...the div comes to light... check the image in debug mode http://postimg.org/image/qf6huodjb/ What's is going wrong??, does the main page do a quick refresh or something else??
  19. Sorry.....duplicate And i have echo json_encode($response);
  20. Hi Barand Sorry...not even with this, i have changed the php...to <?php header('Content-type: application/json'); 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++){ $stmt = $mysqli->prepare("INSERT INTO `ebspma`.`sala_ocupacao` (id_dia, id_sala, id_tempo) VALUES (?, ?, ?);")or die(mysql_error($mysqli)); $horas = $c[$i]; echo $mysqli->error, PHP_EOL; $stmt->bind_param('ssi', $dia, $sala, $horas); if($stmt->execute()){ $response = "Registo gravado"; echo ($response); } else{ echo $stmt->error; } $stmt->close(); } and the javascript is $.ajax({ type: "POST", url: "registerBd.php", data: data, cache: false, dataType:'html', success: function(data) { var dataR = data; console.log(dataR); console.log(data); $('#ajaxDivOk').css("display", "block"); $('#ajaxDivOk').html("Registo(s) gravado(s) com sucesso"); } }); Nothing is show...not even in console
  21. Hi Barand Sorry...not even with this, i have changed the php...to <?php header('Content-type: application/json'); 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++){ $stmt = $mysqli->prepare("INSERT INTO `ebspma`.`sala_ocupacao` (id_dia, id_sala, id_tempo) VALUES (?, ?, ?);")or die(mysql_error($mysqli)); $horas = $c[$i]; echo $mysqli->error, PHP_EOL; $stmt->bind_param('ssi', $dia, $sala, $horas); if($stmt->execute()){ $response = "Registo gravado"; echo ($response); } else{ echo $stmt->error; } $stmt->close(); } and the javascript is $.ajax({ type: "POST", url: "registerBd.php", data: data, cache: false, dataType:'html', success: function(data) { var dataR = data; console.log(dataR); console.log(data); $('#ajaxDivOk').css("display", "block"); $('#ajaxDivOk').html("Registo(s) gravado(s) com sucesso"); } }); Nothing is show...not even in console
  22. No the query is working because the data is being inserted into the db. using echo "Gravado"; My function is not getting the echo $.ajax({ type: "POST", url: "registerBd.php", data: data, cache: false, dataType: 'html', success: function(data) { var dataR = data; console.log(data); console.log(dataR); $('#ajaxDivOk').css("display", "block"); $('#ajaxDivOk').html("Registo(s) gravado(s) com sucesso"); } }); the data variable doesn't receive anything....the console log is empty...i'm confused
  23. Hi Barnad With this code <?php header('Content-type: application/json'); 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++){ $stmt = $mysqli->prepare("INSERT INTO `ebspma`.`sala_ocupacao` (id_dia, id_sala, id_tempo) VALUES (?, ?, ?);")or die(mysql_error($mysqli)); $horas = $c[$i]; echo $mysqli->error, PHP_EOL; $stmt->bind_param('ssi', $dia, $sala, $horas); if(!$stmt->execute()) echo $stmt->error; $stmt->close(); } and this 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; var data = 'dia='+ dia + '&sala='+ sala + '&hora='+ tempos + '&num='+ num; $.ajax({ type: "POST", url: "registerBd.php", data: data, cache: false, success: function(data) { $('#ajaxDivOk').css("display", "block"); $('#ajaxDivOk').html("Registo(s) gravado(s) com sucesso"); } }); return false; } </script> Nothing is show...in my div...what i missing?? Regards and many thanks
  24. Hi Barand Thanks again for your help I have fixed now, the code is now like this <?php header('Content-type: application/json'); 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++){ $stmt = $mysqli->prepare("INSERT INTO `ebspma`.`sala_ocupacao` (id_dia, id_sala, id_tempo) VALUES (?, ?, ?);")or die(mysql_error($mysqli)); $horas = $c[$i]; echo $mysqli->error, PHP_EOL; $stmt->bind_param('ssi', $dia, $sala, $horas); if(!$stmt->execute()) echo $stmt->error; $response = "Registo gravado"; echo json_encode($response); $stmt->close(); } I'm sure the code can be improved I just have another small issue I have no json_encode response in my javascript, can you see why? <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; var data = 'dia='+ dia + '&sala='+ sala + '&hora='+ tempos + '&num='+ num; $.ajax({ type: "POST", url: "registerBd.php", data: data, cache: false, success: function(data) { y = data.y; console.log(y); document.getElementById('ajaxDivOk').style.display = "block"; document.getElementById('ajaxDivOk').innerHTML = "Registo(s) gravado(s) com sucesso"; alert("Form submitted successfully.\nReturned json: " + y ); } }); } </script> Thanks again for your help,
  25. $hora (or $_POST['hora']) contain in this example Array[4] [0] String = "1" [1] String = "2" [2] String = "5" [3] String = "6" I have tested in php online with this code $a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]; $hora = [1,4,7,9,10]; $c = array_fill_keys(array_keys(array_diff($a,$hora)),null) + $a; ksort($c); foreach ($c as $key => $val) { echo "$val\n"; } And i receive the correct values So the problem should be here inside the loop $horas = $val; because i just get one value Thanks
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.