0

When using insert query outside the function its working. But when using into function it doesn't work.

php code:

 <?php

function registerusers(){
  $username =$_POST['username'];
  $password =$_POST['password'];
  $error = "Your Login Name or Password is invalid";
  echo "$username";
  $sql = "SELECT id FROM users WHERE username = '$username' and password = '$password'";
  $result = mysqli_query($conn,$sql);
  $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
  $count = mysqli_num_rows($result);
  if($count == 1) {
  ?>
  <script type="text/javascript">
  $("div#ack").html("Successfully");
  </script>
  <?php
  }else {
  ?>
  <script type="text/javascript">
  $("div#ack").html("Invalid user and password");
  </script>
  <?php
  }
}
registerusers()

jquery code:

$("button#submit").click( function() {
    if( $("#username").val() == "" || $("#password").val() == "" ){
    $("div#ack").html("Please enter both username and password");
    }else{
    $.post( $("#myForm").attr("action"),
    $("#myForm :input").serializeArray(),
    function(data) {
    $("div#ack").html(data);
    });
    $("#myForm").submit( function() {
    return false;   
    });
    }
});
<form id="myForm" action="functions.php" method="POST">
    username: <input type="text" name="username" /><br />
    password: <input type="password" name="password" /><br />
    <button id="submit">Login</button>
</form>

I have using both method button type button and button type submit as well

chris85
  • 23,846
  • 7
  • 34
  • 51

0 Answers0