phpjqueryajaxpostbootstrapvalidator

Ajax POST turns GET with bootstrap validator


I have a problem with bootstrap validator. I have a form with nearly 40 fields, and I'm not ussing the native HTML / PHP way. I'm just using the Submit botton to call ajax and persist the data, but when I do that, the POST turns GET and I have the nasty "Request-URI Too Long, The requested URL's length exceeds the capacity limit for this server." I was reading all previous cases, but none works for me. The only thing that works, and the POST doesn't change, is taking away the bootstrap validator, but with that, I haven't any control over field validation in frontend. Aparently, the call in AJAX works, and even I can debug the PHP (save.php) but the response is a GET call that doesn't make sense. Here is my code, and for weeks I'm trying to solve it without success.

JS Function:

$('#modalcust').validator().on('submit', function(e) {
  if (e.isDefaultPrevented()) {
    swal("Error, error"); // This is just an example.
  } else {
    value = $("#field").val(); // Here I have nearly 40 different vars (field values).
    $.ajax({
      url: "db/save.php",
      type: "POST",
      data: value,
      success: function(data, textStatus, jqXHR) {
        $('#modalcust').modal('toggle');
      },
      error: function(jqXHR, textStatus, errorThrown) {
        swal("Error!", textStatus, "error");
      }
    });
  }
});

HTML form:

<form  data-toggle="validator" role="form">
....
<button type="submit" class="btn btn-primary" id="btnsave"><i class="fa fa-save"></i> Save</button>
</form>

PHP:

<?php
include "conect.php";

$crud=$_POST['crudmethod'];
$protocolo = time() . rand(10*45, 100*98);
if($crud=='E'){
    $protocol = $_POST['protocol'];
}

$rawData = $_POST['photosrc'];
if ($rawData) {
    $fotoFileName = 'uploads/'.$protocol.'.jpg';
    $fotosave = __DIR__ .'/../'.$fotoFileName;
    file_put_contents($fotosave, file_get_contents("data://".$rawData));
}

the 40 vars from POST

if($crud=='N'){
    mysqli_query($conn, "insert into people( 'vars' ) values('vars')");

    if(mysqli_error($conn)){
        $result['error']=mysqli_error($conn);
        $result['result']=0;
    }else{
        $result['error']='';
        $result['result']=1;
        $result['id']=mysqli_insert_id($conn);
    }
}else if($crud == 'E'){
    mysqli_query($conn, "update people set 'vars = vars' where id = $id");
    if(mysqli_error($conn)){
        $result['error']=mysqli_error($conn);
        $result['result']=0;
    }else{
        $result['error']='';
        $result['result']=1;
    }
}else{

    $result['error']='Invalid';
    $result['result']=0;
}
$result['crud']=$crud;

$result['crud']=$crud;
echo json_encode($result);
?>

Solution

  • HTML of form should be changed to:

    <form id="modalcust" data-toggle="validator" role="form">
    

    JS code should be changed to:

    $('#modalcust').validator();
    $('#modalcust').on('submit', function(e) {
        e.preventDefault();
        value = $("#field").val(); // Here I have nearly 40 different vars (field values).
        $.ajax({
          url: "db/save.php",
          type: "POST",
          data: value,
          success: function(data, textStatus, jqXHR) {
            $('#modalcust').modal('toggle');
          },
          error: function(jqXHR, textStatus, errorThrown) {
            swal("Error!", textStatus, "error");
          }
        });
    });