0

Hello so I'm new to ajax and jquery and I ran into a problem. I'm currently trying to check if a button was pressed and if the button was pressed I'd like to call a PHP file that has some code in it that checks some database stuff. My problem is that when I press the button nothing happens, so no console output, PHP file doesn't execute(I think nothing happens though) and I don't know how to fix it.

My javascript file:

$(".button").click(function() {
  $.ajax({
    method: "POST",
    url: "./checkData.php",
    data: { action: "call_this" }
  }).done(function() {
    console.log("success");
  });
});

my html file:

<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
   <div style="width:300px;height:500px;" class="centered">
   <div class="form">
    <form class="login-form" method="POST">
      <input type="text" name="username" placeholder="Uporabniško ime" value="" />
      <input type="password" name="password" placeholder="Geslo" value="" />
    <button>prijava</button> <!-- TO PA JE GUMB -->
      <p class="message">Nisi administrator? <br style="line-height:150%" /><a id="guest" href="#">Prijava kot gost</a></p>
    </form>
  </div>
  <script src="java.js"></script>
</body>
Anze
  • 27
  • 6
  • Buttons are `type="submit" by default, so it's submitting the form, which reloads the page. – Barmar Feb 20 '20 at 21:12
  • ajax wont work because there is no dom element with class named '.button', also you have to use event.preventDefault() , tp prevent the form to submit –  Feb 20 '20 at 21:15
  • thanks for your help. I added type="button" to my button so the page doesn't refresh and added them id so there is an existing dom element. – Anze Feb 20 '20 at 21:22

0 Answers0