0

I have a HTML file which have Last Name and Code to access a website. I want to do a JavaScript validation on my html Form to validate credential.

action="https://www.somewebsite.com/doctor_autho.php?action=login&type=login"

The form action in HTML file send this kind of data

{"result":"true","msg":"redirect to home.html"}
{"result":"false","msg":"The Invitaion code or Last Name is wrong used!"}
{"result":"false","msg":"Parameter wrong used!"}

Please suggest how to do validation as my page is html. And want to do it using Javascript or JQuery as I have limited knowledge on PHP.

Can something like this work ?

 <script type='text/javascript'>
 $(document).ready(function(){
 $('#accessform').on('submit', function(event){
  event.preventDefault();
  $.ajax({
   url:"https://www/doctor_autho.php?action=login&type=login",
   method:"POST",
   data:$(this).serialize(),
   dataType:"json",
   beforeSend:function(){
    $('#submit').attr('disabled', 'disabled');
   },
   success:function(data)
   {
    if(data.result)
    {
     if(data.result = 'false')
     {
      $('#login_error').html("<strong> Valid credential required </strong>");
     }
     else
     {
      $('#name_error').html('');
      }
     }  
     $('#submit').attr('disabled', false);
    }
   })
  });
 });
</script>
FLVO
  • 1
  • 1
  • you can use event.preventDefault onsubmit event, check this answer https://stackoverflow.com/a/3350351/3204473 – mpc Jan 29 '20 at 07:26
  • 1
    Client side validation can be easily spoofed (hacked). Limited knowledge of PHP is not good enough reason to skip out on learning how to use `$_POST` or `$_GET`. I'd suggest you learn both client side *and* server side validation. With that said, can you post your source code, and not just the data you expect. – GetSet Jan 29 '20 at 07:26
  • As GetSet mentionned, client-side (javascript) validation is equivalent to no validation at all. It should only be used for UX reasons only, not security. – Laurent S. Jan 29 '20 at 08:43
  • actually i want to redirect to a new page, if these credentials are valid. – FLVO Jan 29 '20 at 08:54

0 Answers0