0

tengo una duda al enviar datos desde una funcion en JS usando $.post de JQuery, ya que al enviar los datos no ingresa al php siempre lo echo aunque con java esta vez en php pero no funciona me da el error en la consola el siguiente

ReferenceError: $ is not definedscript.js:5:5
enviar http://localhost/Talleranimales/js/script.js:5
onclick http://localhost/Talleranimales/index.php:1

La linea 1 del html es <!DOCTYPE html> y la linea 5 del js es la del $.post('recibe.php

mi html


 <input type="text" id="txt">
        <button type="button" onclick="enviar()">Enviar</button>

js

function enviar() {
    nombre = document.getElementById('txt').value;
    console.log(nombre);

    $.post('recibe.php', { opcion: nombre }, function(rs) {
        console.log(rs);
    });
}

2 respuestas 2

1

Te falta agregar la libreria jQuery o la estas cargando después de tu script principal:

<script src = "https://code.jquery.com/jquery-3.5.1.js"></script>
<script src = "mainScript.js"></script>  <!--Tu script principal-->
0

efectivamente falta agregar la librería JQUERY

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <input type="text" id="txt">
        <button type="button" onclick="enviar()">Enviar</button>

        <script
                      src="https://code.jquery.com/jquery-3.5.1.min.js"
                      integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
                      crossorigin="anonymous"></script>


    <script>
      function enviar() {
          nombre = document.getElementById('txt').value;
          console.log(nombre);

          $.post('recibe.php', { opcion: nombre }, function(rs) {
              console.log(rs);
          });
      }
    </script>
  </body>
</html>

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.