php - Json response to ajax -
the data inserted , receive in console json response page move insert php , gives json response start in page (form) , after submit moves page b (insert)giving me json response. i'm having problem getting response of following php ajax
if (move_uploaded_file($_files['filetoupload']['tmp_name'], $uploadfile)) { $target_path = "/uploads/" . basename($_files['filetoupload']['name']); $sql = "insert `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')); this ajax
$.ajax({ url: 'nova_formacaobd.php', datatype: 'json', data: data, success: function(data) { $('#ajaxdivok').html('informação: esta formação foi registada na base de dados'); if(data.status == "success"){ $('#ajaxdivok').append(data.message); } } }); and form
<form action="nova_formacaobd.php" method="post" id="formacao" name="formacao" enctype="multipart/form-data"> update
the data input fields in form
seven strings , upload of 1 file
<form 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"> <label for="exampleinputemail1">nome formação: </label> <input class="form-control" id="form" name="form" onchange="verificanome()"> </input> <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"> <label for="exampleinputemail1">horas: </label><input type="text" class="form-control" id="horas" name="horas" onchange="verificahoras()"> </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"> <label for="exampleinputemail1">destinatários: </label> <input class="form-control" id="dest" name="dest" onchange="verificadest()"> </input> </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"> <label for="exampleinputfile">programa da formação</label> <input type="file" name="filetoupload" id="filetoupload" name="filetoupload"> </div> <button type="submit" class="btn btn-default" onclick="return checkboxes(this)">registar</button> </form> this form complete
update 2 (now works have warning in console synchronous xmlhttprequest on main thread deprecated because of detrimental effects end user's experience. more help, check http://xhr.spec.whatwg.org/.
and beforesend funtion doesn't work don't see div "wait while updating file) js code
<script type="text/javascript"> $(document).ready(function () { $("#formacao").submit(function (event) { event.preventdefault(); //grab form data var formdata = new formdata($(this)[0]); $.ajax({ url: 'nova_formacaobd.php', type: 'post', data: formdata, async: false, cache: false, contenttype: false, processdata: false, beforesend: function(data){ $('#ajaxdivalert').html('a carregar o ficheiro...aguarde por favor') }, success: function (data) { var result = data.status; console.log(result); if(result == 'success'){ $('#ajaxdivalert').empty(); $('#ajaxdivok').html('informação: ' + data.message); $("#ajaxdivok").fadein(); $("#ajaxdivok").fadeout(5000); } }, error: function(){ $("#ajaxdiverro").html('esta formação já está registada na base de dados'); $("#ajaxdiverro").fadein(); $("#ajaxdiverro").fadeout(5000); } }); return false; }); }); </script> so, need put info complete code, rest working
you have changes :
html : remove action="nova_formacaobd.php" :
<form method="post" id="formacao" name="formacao" enctype="multipart/form-data"> then suppose have ajax called :
update
$(document).ready(function () { $("#formacao").submit(function (e) { e.preventdefault(); var formdata = new formdata($(this)[0]); var ajaxdivalert = $('#ajaxdivalert'); $.ajax({ url: 'nova_formacaobd.php', type: 'post', data: formdata, cache: false, contenttype: false, processdata: false, beforesend: function () { ajaxdivalert.empty().html('a carregar o ficheiro...aguarde por favor') } }).done(function (data) { var result = data.status; if (result == 'success'){ ajaxdivalert.empty().fadein().html('informação: ' + data.message).fadeout(5000); } }).fail(function () { ajaxdivalert.empty().fadein().html('esta formação já está registada na base de dados').fadeout(5000); }); }); }); you had error xmlhttprequest because put async: false. removed because since jquery 1.8, use of async: false jqxhr ($ .deferred) deprecated
Comments
Post a Comment