title: why the ajax call runs and create duplicates
body: <p>I have added an ajax button to send some data to a custom table in wordpress database. </p>
  
  <p>Here is what i have there. </p>
  
  <pre><code>jQuery(document).ready(function(){
  
      jQuery("#holiday-submit").click(function(event){
          event.preventDefault();
          //var name = jQuery("#dname").val();
          var date = jQuery("#holiday").val();
         var reason =jQuery("#holiday-reason").val();
          jQuery.ajax({
              type: 'POST',
              url: MyAjax.ajaxurl,
              data: {"action": "add_holiday", "holiday":date, "reason" :reason},
              success:function(data){
                  jQuery("#holiday-status").addClass("success");
                  jQuery("#holiday-status").html( "New Holiday added " + date +" " + reason);
                  }
          });
      });
  });
  </code></pre>
  
  <p>Then in the function file, i have the following</p>
  
  <pre><code>function add_holiday(){
      $holiday = $_POST['holiday'];
      $reason = $_POST['reason'];
      global $wpdb;
  
      $table1= $wpdb-&gt;prefix.'holidays';
      $results = $wpdb-&gt;get_results("select * from $table1 where holiday=$holiday");
          if($wpdb-&gt;num_rows == 0 ){
              $wpdb-&gt;insert(
                      $table1,
                  array(
                      'holiday' =&gt; $holiday,
                      'reason'  =&gt; $reason
                  ),
                  array(
                      '%s'
                  )
              );
              die();
  
              return true;
          }
          else{
              die();
              return false;
          }
      }
  
  add_action('wp_ajax_add_holiday', 'add_holiday');
  add_action('wp_ajax_nopriv_add_holiday', 'add_holiday');
  </code></pre>
  
  <p>Both the ajax call and the relevant call back functions are working fine, and once the data is submitted using the following form, it sends to database and saves.</p>
  
  <p>But, I do not want to have duplicate dates added to the database.</p>
  
  <p>that is why I have added a condition to check whether the date already exists in the database table. Ideally, I need to show an error if the date already added to the database. nevertheless, the ajax call runs without considering the said condition. </p>
  
  <p>what is the best way to address this. </p>
  
  <p>this is my form and the output area .</p>
  
  <pre><code>&lt;form action="" method="post"&gt;
  
              &lt;input type="date" id="holiday" name="holiday"&gt;
              &lt;input type="text" id="holiday-reason" placeholder="Holiday reason"&gt;
              &lt;!--&lt;input type="submit" id="holiday-submit"&gt;--&gt;
              &lt;input type="button" id="holiday-submit" value="submit" class="btn btn-primary"&gt;
          &lt;/form&gt;
  &lt;div id="holiday-status"&gt;
  
  &lt;/div&gt;
  
          &lt;div id="current-holidays"&gt;
              &lt;table id="declared-holidays" class="table table-success"&gt;
                  &lt;thead&gt;
                  &lt;tr&gt;
                      &lt;th&gt;Date&lt;/th&gt;
                      &lt;th&gt;Reason&lt;/th&gt;
                  &lt;/tr&gt;
                  &lt;/thead&gt;
                  &lt;tbody&gt;
                  &lt;?php
                  $table = $wpdb-&gt;prefix.'hr_holidays';
  
  $result = $wpdb-&gt;get_results("select * from $table");
  
  foreach ($result as $holiday_item){?&gt;
  &lt;tr&gt;
      &lt;td&gt;&lt;?php echo $holiday_item-&gt;holiday ;?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;?php echo $holiday_item-&gt;reason ;?&gt;&lt;/td&gt;
  &lt;/tr&gt;
      &lt;?php
  }
  ?&gt;
                  &lt;/tbody&gt;
              &lt;/table&gt;
  </code></pre>