gmc1103 Posted May 28, 2015 Share Posted May 28, 2015 (edited) Hi I'm having problem (no response is received in my html page) with ajax This is the php with echo echo json_encode(array('status' => 'success', 'message' => 'Nova formação gravada')); And this is my ajax <script type="text/javascript"> $.ajax({ url: 'nova_formacaoBD.php', data: { format: 'json' }, error: function() { $("#ajaxDivErro").html('Esta formação já está registada na base de dados'); }, dataType: 'json', success: function(data) { $('#ajaxDivOk').html('Informação: Esta formação foi registada na base de dados'); }, type: 'GET' }); </script> So after inserting the data into my database i would like to receive the response to a div. Whats wrong? Thanks Edited May 28, 2015 by gmc1103 Quote Link to comment https://forums.phpfreaks.com/topic/296523-ajax-response-from-php/ Share on other sites More sharing options...
CroNiX Posted May 28, 2015 Share Posted May 28, 2015 (edited) What does your browsers console say? Is the request making it to the script? Edited May 28, 2015 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/296523-ajax-response-from-php/#findComment-1512763 Share on other sites More sharing options...
fastsol Posted May 28, 2015 Share Posted May 28, 2015 Assuming that you are actually getting the json response back from the server, you're not doing anything with it in your ajax success block. You could do something like this with the returned message. success: function(data) { $('#ajaxDivOk').html('Informação: Esta formação foi registada na base de dados'); if(data.status == "success"){ // That is the json returned success in your array with message $('#ajaxDivOk').append(data.message); } } Quote Link to comment https://forums.phpfreaks.com/topic/296523-ajax-response-from-php/#findComment-1512766 Share on other sites More sharing options...
gmc1103 Posted May 28, 2015 Author Share Posted May 28, 2015 In console i don't get anything but i receive the json response in the browser in json format Quote Link to comment https://forums.phpfreaks.com/topic/296523-ajax-response-from-php/#findComment-1512767 Share on other sites More sharing options...
sKunKbad Posted May 29, 2015 Share Posted May 29, 2015 If you don't see anything in the console, you may need to switch to the network activity tab. It depends what console you are using. How do you know that you are receiving the json response? Quote Link to comment https://forums.phpfreaks.com/topic/296523-ajax-response-from-php/#findComment-1512787 Share on other sites More sharing options...
gmc1103 Posted May 29, 2015 Author Share Posted May 29, 2015 Hi This is the response in console {"status":"success","message":"Nova forma\u00e7\u00e3o gravada"} So i receive the the json array The problem is i don't stay in my page form (/superNewFormacao.php) and i get the response from my insert php file (nova_formacaoBD.php) This is my php <?php header('Content-Type: application/json'); error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); $mysqli = new mysqli('localhost', 'xxxxxx', 'xxxxxxx'); if (mysqli_connect_errno()) { trigger_error('Database connection failed: ' . mysqli_connect_error(), E_USER_ERROR); } $uploaddir = $_SERVER['DOCUMENT_ROOT'] . '/uploads'; $uploadfile = $uploaddir . '/' . basename($_FILES['fileToUpload']['name']); $escola = $_POST['escola']; $form = $_POST['form']; $data = $_POST['data']; $horas = $_POST['horas']; $local = $_POST['local']; $dest = $_POST['dest']; $datas = $_POST['datas']; $visto = 0; if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $uploadfile)) { $target_path = "/uploads/" . basename($_FILES['fileToUpload']['name']); $sql = "INSERT INTO `ebspma_paad_ebspma`.`formacoes`(idescola, nome, inicio, horas, local, destinatarios, dataLimite, visto, path) VALUES(?, ?, ?, ?, ? ,?, ?, ?, ? )"; $stmt = $mysqli->prepare($sql); if ($stmt === false) { trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $mysqli->error, E_USER_ERROR); } $stmt->bind_param('issssssis', $escola, $form, $data, $horas, $local, $dest, $datas, $visto, $target_path); if (!$stmt->execute()) { echo json_encode(array('status' => 'error', 'message' => 'Opppss...A formação não foi gravada')); } } else { echo json_encode(array('status' => 'error', 'message' => 'Opppss...A formação não foi gravada')); } $stmt->close(); echo json_encode(array('status' => 'success', 'message' => 'Nova formação gravada')); Quote Link to comment https://forums.phpfreaks.com/topic/296523-ajax-response-from-php/#findComment-1512800 Share on other sites More sharing options...
gmc1103 Posted May 29, 2015 Author Share Posted May 29, 2015 And this is my form <form action="nova_formacaoBD.php" method="post" id="formacao" name="formacao" enctype="multipart/form-data"> <div class="form-group"> <label for="exampleInputFile">Escola: </label> <select class="form-control" id="escola" name="escola" onchange="verificaEscola()"> <?php echo $escolaOptions; ?> </select> </div> <div class="form-group"> <div id="ajaxDivErroEscola" style="display:none" class="alert alert-danger"></div> </div> <div class="form-group"> <label for="exampleInputEmail1">Nome Formação: </label> <input class="form-control" id="form" name="form" onchange="verificaNome()"> </input> </div> <div class="form-group"> <div id="ajaxDivErroForm" style="display:none" class="alert alert-danger"></div> </div> <div class="form-group"> <label for="exampleInputEmail1">Data de Início: </label><input type="text" class="form-control" id="data" name="data" onchange="verificaData()" /> </div> <div class="form-group"> <div id="ajaxDivErroData" style="display:none" class="alert alert-danger"></div> </div> <div class="form-group"> <label for="exampleInputEmail1">Horas: </label><input type="text" class="form-control" id="horas" name="horas" onchange="verificaHoras()"> </div> <div class="form-group"> <div id="ajaxDivErroHoras" style="display:none" class="alert alert-danger"></div> </div> <div class="form-group"> <label for="exampleInputEmail1">Local: </label> <input class="form-control" id="local" name="local" onchange="verificaLocal()"> </input> </div> <div class="form-group"> <div id="ajaxDivErroLocal" style="display:none" class="alert alert-danger"></div> </div> <div class="form-group"> <label for="exampleInputEmail1">Destinatários: </label> <input class="form-control" id="dest" name="dest" onchange="verificaDest()"> </input> </div> <div class="form-group"> <div id="ajaxDivErroDest" style="display:none" class="alert alert-danger"></div> </div> <div class="form-group"> <label for="exampleInputEmail1">Data Limite: </label><input type="text" class="form-control" id="datas" name="datas" onchange="verificaDataLimite()"/> </div> <div class="form-group"> <div id="ajaxDivErroDataLimite" style="display:none" class="alert alert-danger"></div> </div> <div class="form-group"> <label for="exampleInputFile">Programa da Formação</label> <input type="file" name="fileToUpload" id="fileToUpload" name="fileToUpload"> </div> <div class="form-group"> <div id="ajaxDivErroFile" style="display:none" class="alert alert-danger"></div> </div> <div class="form-group"> <div id="ajaxDivOk" style="display:none" class="alert alert-success">Aqui</div> </div> <div class="form-group"> <div id="ajaxDivErro" style="display:none" class="alert alert-danger"></div> </div> <button type="submit" class="btn btn-default" onclick="return checkBoxes(this)">Registar</button> </form> Quote Link to comment https://forums.phpfreaks.com/topic/296523-ajax-response-from-php/#findComment-1512801 Share on other sites More sharing options...
sKunKbad Posted May 30, 2015 Share Posted May 30, 2015 It sounds like you need to use preventDefault in your javascript to keep the HTML form from submitting. Quote Link to comment https://forums.phpfreaks.com/topic/296523-ajax-response-from-php/#findComment-1512842 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.