Jump to content

Ajax response from php


gmc1103

Recommended Posts

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 by gmc1103
Link to comment
Share on other sites

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);
   }
}
Link to comment
Share on other sites

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'));
Link to comment
Share on other sites

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>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.