Return php result to javascript

1

I am doing the POST request for AJAX:

HTML FORM (admin.php):

<form align="center" width="50" name="excluir_documento" id="excluir_documento" onClick='return confirma_excluir_documento()' method="POST"> 
<input type="text" name="prefixo" id='prefixo' hidden value="DE"></a>
<input type="text" name="sequencial" id='sequencial' hidden value="310"></a>
<input type="text" name="nome_conf" id='nome_conf' hidden value=""></a>
<input type='submit' name='excluir_documento' id='excluir_documento' value='' class='imgExcluir' title='Excluir' ></a> 

Javascript (funcoes.js):

function confirma_excluir_documento() {
            vex.defaultOptions.className = 'vex-theme-default';
            vex.dialog.confirm({
                message: 'Você irá excluir o documento. Deseja continuar?',
                callback: function(resultado) {
                    if (resultado) {
                        $.ajax({
                            type: "POST",
                            url: "excluir.php",
                            data:{
                                prefixo : $("#prefixo").val(),
                                sequencial : $("#sequencial").val(),
                                nome_conf : $("#nome_conf").val(),
                                excluir_documento : 1
                            },
                            success: function (response) {
                                vex.dialog.alert('response');
                            }
                        }); 
                    }
                }
            });
                return false;
        }

I would like to send the page to php delete.php:

if(isset($_post['excluir_documento']))...
... if($excluir_resultado){
echo "Documento excluído com sucesso";} 
else{
echo "Erro ao excluir";}

The problem I need to return from what happened to the database query, I need to give the result inside the success: function (response){ vex.dialog.alert('response') ;} dialog box, the problem that the information that comes and the entire page, deformatando the dialog .

How do I get only the echo "Documento excluído com sucesso";} ? I have tried in several ways but I could not, I searched the afternoon for a solution and nothing.

    
asked by anonymous 24.12.2016 / 00:30

1 answer

1

Initially, the delete page does not require any extra returns, otherwise the confirmation of the deletion, or not. Therefore a 1 for "deleted" and 0 for "not deleted" would already suffice. Post her code to help you better.

With the return of the Boolean value, you can treat it with an "If" and set the response you want within the HTML5 element you need.

Using, for example, the "ajax ()" function of JQuery you can handle the events of the requests, as well as work on many other options.

(See: link )

var menuId = $( "ul.nav" ).first().attr( "id" );
//pega a variavel, monta o objeto e faz o pedido
var request = $.ajax({
  url: "excluir.php",
  method: "POST",
  data: { id : menuId },
  dataType: "html"
});

//se ok com a requisição, trata o resultado (exluido sim ou não)
request.done(function( msg ) {
  $( "#retorno" ).html( msg ); //coloca o resultado dentro do container "#log"
});
 
//caso a requisiçao falhe
request.fail(function( jqXHR, textStatus ) {
  alert( "Request failed: " + textStatus ); //alerta do erro
});

I hope I have helped.

Hug.

UPDATE //

Your delete.php code will look like this:

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');

/* isso retornara o numero correto de linhas excluidas se houver mais de 1 */
mysql_query('DELETE FROM mytable WHERE id < 10');
printf("Registros excluídos: %d\n", mysql_affected_rows());

/* se for somente uma linha, retonará sempre 0 */
mysql_query('DELETE FROM mytable WHERE 0');
printf("Registros excluídos: %d\n", mysql_affected_rows());
?>
    
24.12.2016 / 00:51