0

good afternoon I have a problem, I am trying to save the data values from the form in the database. AJAX is used to send the data using the POST method but when evaluating the outputs from the .done and .fail functions, they return no information.

This is the html

      <div class="form">
         <h1>Ingreso Alumno-Trabajo</h1>

         <form name="formulario_contacto" id="formdata">
            <p>
               <label for="cest">cedula estudiante</label>
               <input type="number" id="cest" name="cest" autofocus required>
            </p>
            <p>
               <label for="nest">nombre estudiante</label>
               <input type="text" id="nest" name="nest" required>
            </p>
            <p>
               <label for="aest">Apellido estudiante</label>
               <input type="text" id="aest" name="aest" required>
            </p>
            <p class="full-width">
               <label for="tit">Titulo</label>
               <textarea name="" id="tit" cols="30" rows="3" name="tit" required></textarea>
            </p>
            <p>
               <label for="nase">nombre asesor</label>
               <input type="text" id="nase" name="nase" required>
            </p>
            <p>
               <label for="fsede">Sede :</label>
               <select id="fSede" name="fSede">
                  <option value="Montalban">Montalban</option>
                  <option value="Guyana">Guayana</option>
                  <option value="Virtual">Virtual</option>
               </select>
            </p>
            <p class="full-width button">
               <input type="submit" value="Ingresar">
               <input type="reset" value="Restaurar">
            </p>

         </form>

      </div>
      <div id="mensaje">
         <h3></h3>
      </div>

This is the JS using AJAX

<pre>
$(document).ready(function () {
    // This part of the code will automatically execute when doc is ready.
    $("#mensaje").html("");

    $("input:submit").click(function () {
        // We set the default action to be made once we submit.
        // Let's validate the form first

        if (validaForm()) {
            var formulario = $("#formdata").serializeArray();

            $.ajax({
                //          url: "php/enviar.php",
                // type: "POST",
                type: "POST",
                url: "php/enviar.php",
                dataType: "json",
                data: formulario,
            })
                //do something when get response            })
                .done(function (respuesta) {
                    //do something when any erro
                    $("#mensaje").html(respuesta.mensaje);
                    alert("coño de la madre");
                })

                .fail(function (respuesta) {
                    //do something when any erro
                    alert("coño de la madre falla" + respuesta);
                });
        } else {
            // We show this when the validation fails
            $("#mensaje").html("falta Ingresar la data");
        }
    });

        $("input:reset").click(function () {
            $("#mensaje").html("");
        });
    });
    </pre>

This is the PHP

<?php
    echo "Estoy entrando aqui"; #I'm entering here
    if(isset($_POST["cest"]))
    {
      $cedula = $_POST["cest"]; 
      $nombre = $_POST["nest"];
      $apellido = $_POST["aest"];
      $titulo = $_POST["tit"];
      $nomase = $_POST["nase"];
      $sede = $_POST["fsede"];
    
    
      $inserta = "INSERT INTO tbl-est-teg (cedula,nombre, apellido, titulo, nomase, sede) VALUES ('$cedula','$nombre','$apellido','$titulo','$nomase','$sede')";
      
      $conexion = new mysqli("localhost","yguer2","s&4gBz6nPrA8*S7","estudiante-trabajo",3306);
      
      $respuesta = new stdClass();
      
      if($conexion->query($inserta)){
        $respuesta->mensaje = "Se guardo correctamente";
      }
      else {
        $respuesta->mensaje = "Ocurrió un error";
      }
      echo json_encode($respuesta);
      
    }
    ?>
Flexo
  • 87,323
  • 22
  • 191
  • 272
  • Anyway what debugging have you done? You say that neither done() nor fail() returns anything. That should not be possible - you should always get one or the other. The only reason you might not is it the AJAX request doesn't run at all. Do you have any console errors? Did you see the request in the browser's network log? Have you used the JS debugger and/or added any log commands to see what path the code takes? Etc etc. – ADyson Aug 11 '21 at 20:29
  • Some suggestions: you should replace `$("#formdata").serializeArray()` with `$("#formdata").serialize()` - serializeArray has a different purpose and won't put your data into the right format. – ADyson Aug 11 '21 at 20:31
  • And you should replace `$("input:submit").click(function () {` with `$("#formdata").submit(function (event) { event.preventDefault();` so that a) you handle any kind of event which causes the form to submit, not just a click on one specific button, and b) event.preventDefault() is needed to stop the form just posting back normally - in fact that's probably why you don't see results from the Ajax, because the form posts the whole page before it can run. – ADyson Aug 11 '21 at 20:34
  • And lastly you should read https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php and then fix your PHP code because it is horribly vulnerable to hacking. – ADyson Aug 11 '21 at 20:34
  • @Noé thanks for the edit. For what it's worth closing until translation has occurred is quite a sensible thing todo. It's also quick and easy to reopen once it has happened and if it isn't you can always use a custom flag for moderators to get someone to look at it. – Flexo Aug 11 '21 at 20:35
  • @Flexo Yeah I think it was quite rude, specially when it comes to new programmers trying to learn to use a new platform and making effort to use it in a different language. – Noé Aug 11 '21 at 20:38
  • @Noé it really isn't rude, easy to close, easy to edit and easy to reopen is how things are by design – Flexo Aug 11 '21 at 20:40
  • @Flexo well that's your opinion which differs from mine, here on SO we should understand that there will be new poeple and we should try to be as friendly as we can, closing the question in this particular case was not fair since the user provided good information to receive an answer. – Noé Aug 11 '21 at 20:50
  • 2
    @Noé this is a commonly asked question that [has been addressed](https://meta.stackexchange.com/q/8162/399952). The general stance is to close the question and redirect the user to the appropriate alternative resource for their language, in this case https://es.stackoverflow.com, since they may not understand answers that are not also translated or be able to address other issues as a result of the language barrier. – Will B. Aug 11 '21 at 21:10
  • Good morning I understood the answers, however if you want to direct me to another forum it is fine, I have no problem with that. On the other hand, doing the recommendations that you suggest in the trace of .fail shows the NaN result, thanks to all – NéstorCurcho Aug 12 '21 at 14:27

1 Answers1

-1
<script>
$(document).ready(function () {
    // This part of the code will automatically execute when doc is ready.
    $("#mensaje").html("");

    $("#formdata").on("submit",function () {
        // We set the default action to be made once we submit.
        // Let's validate the form first

        if (validaForm()) {
            var formulario = $("#formdata").serializeArray();

            $.ajax({
                //          url: "php/enviar.php",
                // type: "POST",
                type: "POST",
                url: "php/enviar.php",
                dataType: "json",
                data: formulario,
            }).done(function (respuesta) {
                    //do something when any erro
                    $("#mensaje").html(respuesta.mensaje);
                    alert("coño de la madre");
            }).fail(function (respuesta) {
                //do something when any erro
                alert("coño de la madre falla" + respuesta);
            });
        } else {
            // We show this when the validation fails
            $("#mensaje").html("falta Ingresar la data");
        }
    });

        $("input:reset").click(function () {
            $("#mensaje").html("");
        });
    });
</script>

You could try by just doing that but as some people have told you in the comments, there are some serious vulnerability issues in your PHP file, but I guess you should solve this first,

Use the console, if you're using chrome you could just press F12,

There are several things to be asked, I'm guessing your validaForm() function is working well

If you have more questions please edit the original questions so we can help.

Post this in the Spanish version of this site and I could help you there too, just let me know.

Publica esto en el sitio en Español y te puedo ayudar allí, solo deja un comentario con el link a la pregunta.

Noé
  • 149
  • 8