title: Ajax form required select and input fields
body: <p>im new at this so i couldnt work this out with required fields on script.</p>
  
  <p>html form </p>
  
  <pre><code> &lt;form name="rezform" onsubmit="return validation()" id="loginForm" method="" action="" novalidate="novalidate"&gt;
  </code></pre>
  
  <p>javascript</p>
  
  <pre><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
    function insertData() {
      var rez_ad = $("#rez_ad").val();
      var rez_saat = $("#rez_saat").val();
      var rez_gsm = $("#rez_gsm").val();
      var rez_tarih = $("#rez_tarih").val();
      var rez_email = $("#rez_email").val();
      var rez_tip = $("select#rez_tip").val();
      var rez_sayi = $("select#rez_sayi").val();
      var rez_aciklama = $("#rez_aciklama").val();
  
      // AJAX code to send data to php file.
      $.ajax({
        type: "POST",
        url: "rez/insert-data.php",
        data: {
          rez_ad: rez_ad,
          rez_saat: rez_saat,
          rez_gsm: rez_gsm,
          rez_email: rez_email,
          rez_tarih: rez_tarih,
          rez_tip: rez_tip,
          rez_sayi: rez_sayi,
          rez_aciklama: rez_aciklama
        },
        dataType: "JSON",
        success: function(data) {
          $("#message").html(data);
          $("#message").addClass("alert alert-success");
        },
        error: function(err) {
          alert(err);
        }
      });
  
    }
  &lt;/script&gt;
  </code></pre>
  
  <p>insert-data.php
      
  
  <pre><code>/*
  Developer: Ehtesham Mehmood
  Site:      PHPCodify.com
  Script:    Insert Data in PHP using jQuery AJAX without Page Refresh
  File:      Insert-Data.php
  */
  include('db.php');
  $rez_ad=$_POST['rez_ad'];
  $rez_saat=$_POST['rez_saat'];
  $rez_gsm=$_POST['rez_gsm'];
  $rez_email=$_POST['rez_email'];
  $rez_tarih=$_POST['rez_tarih'];
  $rez_tip=$_POST['rez_tip'];
  $rez_sayi=$_POST['rez_sayi'];
  $rez_aciklama=$_POST['rez_aciklama'];
  
  $stmt = $DBcon-&gt;prepare("INSERT INTO rezervasyon(rez_ad,rez_saat,rez_gsm,rez_email,rez_tarih,rez_tip,rez_sayi,rez_aciklama) VALUES(:rez_ad, :rez_saat,:rez_gsm,:rez_email,:rez_tarih,:rez_tip,:rez_sayi,:rez_aciklama)");
  
  $stmt-&gt;bindparam(':rez_ad', $rez_ad);
  $stmt-&gt;bindparam(':rez_saat', $rez_saat);
  $stmt-&gt;bindparam(':rez_gsm', $rez_gsm);
  $stmt-&gt;bindparam(':rez_email', $rez_email);
  $stmt-&gt;bindparam(':rez_tarih', $rez_tarih);
  $stmt-&gt;bindparam(':rez_tip', $rez_tip);
  $stmt-&gt;bindparam(':rez_sayi', $rez_sayi);
  $stmt-&gt;bindparam(':rez_aciklama', $rez_aciklama);
  if($stmt-&gt;execute())
  {
    $res="Rezervasyonunuz tarafımıza ulaşmıştır. En yakın sürede girmiş olduğunuz GSM numaranıza dönüş yapılacaktır.";
    echo json_encode($res);
  }
  else {
    $error="Sistemsel bir hata meydana geldi lütfen bir süre sonra tekrar deneyiniz veya iletişime geçiniz.";
    echo json_encode($error);
  }
  
  
  
   ?&gt;
  </code></pre>
  
  <p>So my problem is when i click my send button on html/php data is going mysql even if the inputs and select boxes are empty/not selected. So I have to do it required like in html input or select box. I dont know how to do it properly in javascript. I have found another script but couldnt work it same as like this one. So how can i put required field on this one and if you can explain it the logic it would be nice ! </p>
  
  <p>Also do we have a trick for bot protection on javascript form ?</p>
  
  <p>Thanks !
  And have a nice day.</p>