title: Insert data into mysql database with php and ajax in wordpress
body: <p>inside my function.php I added new top level admin menu. I added input fields and inside it and put it into html form element.</p>
  
  <pre><code>&lt;form id="prices_form" method="post" action=""&gt;
      &lt;div style=font-weight:bold;font-size:16px;&gt;Location 1&lt;/div&gt;
      &lt;input id="location1" name="location1" type="text" /&gt;
      &lt;input type="hidden" name="count" value="1" /&gt;
      &lt;div style=font-weight:bold;font-size:16px;&gt;Location 2&lt;/div&gt;
  
      &lt;input class="input" id="location2" name="location2" type="text" placeholder="Type something"/&gt;
  &lt;div style=font-weight:bold;font-size:16px;&gt;Price(KN)&lt;/div&gt;
  &lt;input type="number" id="price" name="price" min="0" step="0.01"/&gt;&lt;br&gt;
  &lt;input id="submit" name="submit" type="submit" value="Save prices" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Then I added php where I call ajax via ajax-admin.php and gives user possibility to use ajax. So I want to add input fields into database on submit click.</p>
  
  <pre><code>function ajax_savePrice(){
      $conn = new mysqli($servername, $username, $password, $dbname);
      if ($conn-&gt;connect_error) {
          die("Connection failed: " . $conn-&gt;connect_error);
      }
      $location1 = $_POST['location1'];
      $location2 = $_POST['location2'];
      $price = $_POST['price'];
  
      $result = $conn-&gt;query("SELECT * FROM prices WHERE location1 = '$location1' AND location2='$location2' OR location1 = '$location2' AND location2='$location1'");
      $row_count = $result-&gt;num_rows;
      if ($row_count &gt;= 1) {
          echo 'That locations are already inserted. Do you want to update price?';
      } else {
          $query = "INSERT INTO prices (location1, location2, price) VALUES(?, ?, ?)";
          $statement = $conn-&gt;prepare($query);
  
          //bind parameters for markers, where (s = string, i = integer, d = double,  b = blob)
          $statement-&gt;bind_param('ssi', $location1, $location2, $price);
  
          if ($statement-&gt;execute()) {
              print 'Success! ID of last inserted record is : ' . $statement-&gt;insert_id . '&lt;br /&gt;';
          } else {
              die('Error : (' . $conn-&gt;errno . ') ' . $conn-&gt;error);
          }
          $statement-&gt;close();
      }
  
  }
  
  function ajax_savePrice_init(){
  
  wp_register_script('ajax-savePrice-script', get_template_directory_uri() . '/ajax-savePrice-script.js', array('jquery') ); 
  wp_enqueue_script('ajax-savePrice-script');
  
  wp_localize_script( 'ajax-savePrice-script', 'ajax_savePrice_object', array( 
  'ajaxurl' =&gt; admin_url( 'admin-ajax.php' ),
  'redirecturl' =&gt; home_url(),
  'loadingmessage' =&gt; __('Sending data, please wait...')
  ));
  
  // Enable the user with no privileges to run ajax_login() in AJAX
  add_action( 'wp_ajax_nopriv_ajaxsavePrice', 'ajaxsavePrice' );
  add_action( 'wp_ajax_ajaxsavePrice', 'ajaxsavePrice' );
  }
  add_action('init', 'ajax_savePrice_init');
  </code></pre>
  
  <p>And I made .js file to proccess ajax request:</p>
  
  <pre><code>jQuery(document).ready(function($) {
  
  // Perform AJAX login on form submit
  $('#prices_form').on('submit', function(e){
  
  $.ajax({
      type: 'POST',
      dataType: 'json',
      url: ajax_savePrice_object.ajaxurl,
      data: { 
          'action': 'ajaxsavePrice', 
          'location1': $('#location1').val(), 
          'location2': $('#location2').val(), 
          'price': $('#price').val() },
      success: function(data){
          $('#prices_form').hide();
  
      }
  });
  e.preventDefault();
  });
  
  }); 
  </code></pre>
  
  <p>Page reloads and nothing happens...</p>
  
  <p>Any hint?</p>
  
  <p>EDIT:</p>
  
  <p>I succeed to call ajax and added 3 echo-s to my php so I can get response via server.</p>
  
  <pre><code>$result = $conn-&gt;query("SELECT * FROM prices WHERE location1 = '$location1' AND location2='$location2' OR location1 = '$location2' AND location2='$location1'");
          $row_count = $result-&gt;num_rows;
          if ($row_count &gt;= 1) {
             // echo 'That locations are already inserted. Do you want to update price?';
              echo 'exist';
          } else {
              $query = "INSERT INTO prices (location1, location2, price) VALUES(?, ?, ?)";
              $statement = $conn-&gt;prepare($query);
  
              //bind parameters for markers, where (s = string, i = integer, d = double,  b = blob)
              $statement-&gt;bind_param('ssi', $location1, $location2, $price);
  
              if ($statement-&gt;execute()) {
                 // print 'Success! ID of last inserted record is : ' . $statement-&gt;insert_id . '&lt;br /&gt;';
                  echo 'yes';
              } else {
                  //die('Error : (' . $conn-&gt;errno . ') ' . $conn-&gt;error);
                  echo 'no';
              }
              $statement-&gt;close();
          }
  </code></pre>
  
  <p>Now in my js:</p>
  
  <pre><code>    location1=$("#location1").val();
      location2=$("#location2").val();
      price=$("#price").val();
  data: "location1="+location1+"location2="+location2+"price="+price,
          success: function(html){
            if(html==='exist')
            {
              $("#prices_form").fadeOut("normal");        
            }
            else
            {
                 $("#aaa").fadeOut("normal"); 
            }
          },
          beforeSend:function()
          {
  
          }
      });
       return false;
      });
  </code></pre>
  
  <p>So whatever I enter in my input fields and post to php I got this else part. I tried with all 3 states that php can return to js but always else get executed.</p>
  
  <p>Any hint now?</p>