Error jQuery and Ajax, returns [object Object] of a SQL query

0

I have an error, and PHP does not return the query sql well.

This is what I do:

$(document).on('click', '.editarCliente', function () {

var idc = $(this).attr("id");
console.log(idc);

  $.ajax({
    type: 'POST',
    url: 'php/editar_cliente.php',
    data: {'idc': idc}
  })
  .done(function(listas_rep){
    console.log(listas_rep);
    $('#editarCliente').show();
    $('#resultEditCliente').html(listas_rep);
  })
  .fail(function(err){
    console.log(err);
    alert(err);
  })

});

I get the id right, (checked by console)

Now, this is the php :

<?php  

require_once 'php/conexion.php';

    $idcliente = $_POST['idc'];

    $mysqli = getConn();

if (isset($idcliente)) {
    function editarCliente() {

        $query = "SELECT * FROM clientes WHERE id_cliente = ?";

        $stmt = $mysqli->prepare($query);
        $stmt->bind_param("i", $idcliente);
        $stmt->execute();
        $resultado = $stmt->get_result();
        $resultado = $stmt->fetch(MYSQLI_ASSOC);
        $stmt->close();
       // $result = $mysqli->query($query);

        $ec = '<form id="form_edit_cliente" method="POST">
                <table id="table" width="100%" class="table table-hover table-mc-light-blue">';

        for ($i=0; $i < count($resultado); $i++) { 
        //while($resultado[$i] = $result->fetch_array(MYSQLI_ASSOC)){
            $ec .= '<h2>Editar cliente #'.$resultado[$i]["id_cliente"].'</h2>
                    <h3>Datos Personales</h3>

                    <input id="idc" type="hidden" value="'.$resultado[$i]["id_cliente"].'">
                    <table>
                        <tr>
                            <td class="campoP">Nombre</td>
                            <td class="td"><input id="nombre" type="text" value="'.$resultado[$i]["nombre"].'"></td>
                            <td class="td barcode" style="text-align:center" colspan="6" resultado[$i]span="2"></td>
                            <td class="campoP">Fecha Registro:</td>
                            <td class="td">' .$resultado[$i]["fecha_registro"]. '</td>
                        </tr>
                        <tr>
                            <td class="campoP">Apellidos</td>
                            <td class="td"><input id="apellidos" type="text" value="'.$resultado[$i]["apellidos"].'"></td>
                            <td class="td" colspan="5"></td>
                            <td class="td"></td>
                        </tr>
                        <tr>
                            <td class="campoP">Telefono</td>
                            <td class="td"><input id="telefono" type="text" value="'.$resultado[$i]["telefono"].'"></td>
                            <td class="td" colspan="5"></td>
                            <td class="td"></td>
                        </tr>
                        <tr>
                            <td class="campoP">Email</td>
                            <td class="td"><input id="email" type="text" value="'.$resultado[$i]["email"].'"></td>
                            <td class="td" colspan="5"></td>
                            <td class="td"></td>
                        </tr>
                    </table>
                    <h3>Datos Comerciales</h3>
                    <table>
                        <tr>
                            <td class="campoP" style="width: 10%">Nombre Comercial</td>
                            <td class="td" style="width: 25%"><input id="ncomercial" type="text" value="'.$resultado[$i]["ncomercial"].'"></td>
                            <td class="td" colspan="5"></td>
                            <td class="campoP" style="width: 10%">Nombre Fiscal</td>
                            <td class="td" style="width: 25%"><input id="nfiscal" type="text" value="'.$resultado[$i]["nfiscal"].'"></td>
                            <td class="campoP">NIF/CIF</td>
                            <td class="td"><input id="cif" type="text" value="'.$resultado[$i]["cif"].'"></td>
                        </tr>
                        <tr>
                            <td class="campoP">Direccion</td>
                            <td class="td"><input id="direccion" type="text" value="'.$resultado[$i]["direccion"].'"></td>
                            <td class="td" colspan="5"></td>
                            <td class="campoP">Ciudad</td>
                            <td class="td"><input id="ciudad" type="text" value="'.$resultado[$i]["ciudad"].'"></td>
                            <td class="campoP">CP</td>
                            <td class="td"><input id="cp" type="text" value="'.$resultado[$i]["cp"].'"></td>
                        </tr>
                    </table>';

        }

        $ec .= '</table>
                <br><br>
                <center><input type="submit" id="submit" value="Editar Cliente"></center>
                </form>';

        return $ec;
    }
} else {
    $ec .= "No existe la variable id cliente";
}
echo editarCliente();
?>

It does not even show me that "No existe la variable id cliente"

It returns me in the alert [object Object]

And by console:

jquery.min.js:4 POST http://localhost:8888/seoagency/php/editar_cliente.php 500 (Internal Server Error)

I do not see any logic that this does not work: /

I appreciate that you can help me solve it, thank you.

    
asked by Javier Avila Fernandez 17.09.2018 в 21:35
source

1 answer

0

Does not answer the "There is no variable client id" because you have to make a return to the variable $ ec in that else

** Return this to me **

{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
abort
:
ƒ (a)
always
:
ƒ ()
catch
:
ƒ (a)
done
:
ƒ ()
fail
:
ƒ ()
getAllResponseHeaders
:
ƒ ()
getResponseHeader
:
ƒ (a)
overrideMimeType
:
ƒ (a)
pipe
:
ƒ ()
progress
:
ƒ ()
promise
:
ƒ (a)
readyState
:
4
responseText
:
""
setRequestHeader
:
ƒ (a,b)
state
:
ƒ ()
status
:
500
statusCode
:
ƒ (a)
statusText
:
"Internal Server Error"
then
:
ƒ (b,d,e)
__proto__
:
Object
    
answered by 17.09.2018 в 21:39