Error making an UPDATE from PHP with Ajax

0

I try to do an "edit" clients, but it does not work at all.

  • Is it possible that it is because the form , I get it by PHP in a query?

I will explain my code:

First of all:

The form , comes from a query, by clicking on edit client in the following way:

function verCliente() {

    $idcliente = $_POST['id'];

    $mysqli = getConn();

    $query = "SELECT * FROM clientes WHERE id_cliente = $idcliente";
    $result = $mysqli->query($query);

    $ec = '<table>';

    while($row = $result->fetch_array(MYSQLI_ASSOC)){
        $ec .= '<h2>Editar cliente #'.$row[id_cliente].'</h2>
                <h3>Datos Personales</h3>
                <form id="form_edit_cliente" method="post">
                <input id="idc" type="hidden" value="'.$row[id_cliente].'">
                <table>
                    <tr>
                        <td class="campoP">Nombre</td>
                        <td class="td"><input id="nombre" type="text" value="'.$row[nombre].'"></td>
                        <td class="td barcode" style="text-align:center" colspan="6" rowspan="2"></td>
                        <td class="campoP">Fecha Registro:</td>
                        <td class="td">' .$row[fecha_registro]. '</td>
                    </tr>
                    <tr>
                        <td class="campoP">Apellidos</td>
                        <td class="td"><input id="apellidos" type="text" value="'.$row[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="'.$row[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="'.$row[email].'"></td>
                        <td class="td" colspan="5"></td>
                        <td class="td"></td>
                    </tr>
                </table>
                <h3>Datos Comerciales</h3>
                <table>
                    <tr>
                        <td class="campoP">Nombre Comercial</td>
                        <td class="td"><input id="ncomercial" type="text" value="'.$row[ncomercial].'"></td>
                        <td class="td" colspan="5"></td>
                        <td class="campoP">Nombre Fiscal</td>
                        <td class="td"><input id="nfiscal" type="text" value="'.$row[nfiscal].'"></td>
                        <td class="campoP">NIF/CIF</td>
                        <td class="td"><input id="cif" type="text" value="'.$row[cif].'"></td>
                        <td class="campoP">IVA</td>
                        <td class="td"><input id="iva" type="text" value="'.$row[iva].'"></td>
                    </tr>
                    <tr>
                        <td class="campoP">Direccion</td>
                        <td class="td"><input id="direccion" type="text" value="'.$row[direccion].'"></td>
                        <td class="td" colspan="5"></td>
                        <td class="campoP">Ciudad</td>
                        <td class="td"><input id="ciudad" type="text" value="'.$row[ciudad].'"></td>
                        <td class="campoP">CP</td>
                        <td class="td"><input id="cp" type="text" value="'.$row[cp].'"></td>
                    </tr>';
    }

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

    return $ec;
}
echo verCliente();

This, I am showing it in a DIV , it is a form , inside a table, with its simple button of submit

This is my call from ajax:

$(document).ready(function(){

    $("#form_edit_cliente").submit(function(e){
      var idc = $('#idc').val()
      var nombre = $('#nombre').val()
      var apellidos = $('#apellidos').val()
      var telefono = $('#telefono').val()
      var email = $('#email').val()
      var ncomercial = $('#ncomercial').val()
      var nfiscal = $('#nfiscal').val()
      var cif = $('#cif').val()
      var iva = $('#iva').val()
      var direccion = $('#direccion').val()
      var ciudad = $('#ciudad').val()
      var cp = $('#cp').val()

      e.preventDefault();

      $.ajax({
        type: 'POST',
        url: 'php/update_cliente.php',
        data: {'idc': idc,
               'nombre': nombre,
               'apellidos': apellidos,
               'telefono': telefono,
               'email': email,
               'ncomercial': ncomercial,
               'nfiscal': nfiscal,
               'cif': cif,
               'iva': iva,
               'direccion': direccion,
               'ciudad': ciudad,
               'cp': cp
              }
      })
      .done(function(){

        alert('El cliente ha sido editado correctamente');
        $('#editarCliente').hide();
        location.reload();

      })
      .fail(function(err){
        console.log(err);
        alert("Error 01x234");
      })
    });
})

And the PHP that Ajax looks for is this: it contains the UPDATE :

function updateCliente() {

    $mysqli = getConn();

    $idc = $_POST['idc'];
    $nombre = $_POST['nombre'];
    $apellidos = $_POST['apellidos'];
    $telefono = $_POST['telefono'];
    $email = $_POST['email'];
    $ncomercial = $_POST['ncomercial'];
    $nfiscal = $_POST['nfiscal'];
    $cif = $_POST['cif'];
    $iva = $_POST['iva'];
    $direccion = $_POST['direccion'];
    $ciudad = $_POST['ciudad'];
    $cp = $_POST['cp'];

    $query = "UPDATE clientes SET nombre = $nombre, apellidos = $apellidos, telefono = $telefono, email = $email, ncomercial = $ncomercial, nfiscal = $nfiscal, cif = $cif, iva = $iva, direccion = $direccion, ciudad = $ciudad, cp = $cp WHERE id_cliente = $idc";

    if (mysqli_query($mysqli, $query)) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . mysqli_error($mysqli);
    }

}

echo updateCliente();

What is happening?

  • I think you're not even detecting Ajax, when you hit the submit button, absolutely nothing happens.

I would appreciate help to solve this problem. Thank you very much.

    
asked by Javier Avila Fernandez 08.09.2018 в 18:22
source

0 answers