title: Ajax call within Wordpress wont work at all (500 Internal Server Error)
body: <p>Wordpress way to make an Ajax call in a custom page template is unbelievable complicated. I cant get it to run at all without crashing my whole site. Why I need to do it that way? My other Ajax calls are not needing this complicated and complex way too.</p>
  
  <p><strong>So what I am trying to do:</strong></p>
  
  <p>I want to add a feature to the Shop that Resellers can enter there ID and reorder new bags for there Shops. So long so good.</p>
  
  <p><strong>I got my landing page:</strong></p>
  
  <pre><code>    &lt;?php
      /*
      Template Name: 
      */
      ?&gt;
      &lt;?php
          get_header();
      ?&gt;
      &lt;section id="main" class="reseller-page"&gt;
  
      &lt;form action="&lt;?php echo get_stylesheet_directory_uri(); ?&gt;/reseller.php" method="GET" id="resellerform"&gt;
          Reseller ID: &lt;br&gt;&lt;input type="text" name="rid" id="rid"&gt;
          &lt;button id="resellercheck"&gt;SUCHEN&lt;/button&gt;
      &lt;/form&gt; 
      &lt;div id="resellerwrapper" style="display: none;"&gt;
          &lt;div id="resellerinfo"&gt;&lt;/div&gt;
  
          &lt;div id="resellerorder" style="display: none;"&gt;
              &lt;h2&gt;Bestellformular:&lt;/h2&gt;
              &lt;?php
              $args = array('post_type' =&gt; 'product', 'posts_per_page' =&gt; -1, 'product_cat' =&gt; 'tabak-taschen');
              query_posts($args);
              if (have_posts()):
                  echo '&lt;div id="reseller-select-wrapper"&gt;&lt;select id="artikelselector"&gt;';
                  while (have_posts()) : the_post();
                      echo '&lt;option value="' . $product-&gt;get_sku() . ': ' . $product-&gt;get_title() . '" data-type-href="' . get_permalink() . '"&gt;' . $product-&gt;get_sku() . ': ' . $product-&gt;get_title() . '&lt;/option&gt;';
                  endwhile;
                  echo '&lt;/select&gt;Menge: &lt;input id="reseller-menge" type="text" class="input-text"&gt;&lt;button value="hinzufügen" id="reseller-add-to-form"&gt;+&lt;/button&gt;&lt;/div&gt;';
              endif;
              ?&gt;
              &lt;form action="" method="POST" id="reseller-bestell-liste"&gt;
                  &lt;ul&gt;
  
                  &lt;/ul&gt;
                  &lt;textarea name="nachricht" id="reseller-msg"&gt;&lt;/textarea&gt;
                  &lt;input type="submit" id="reseller-send-order" value="bestellen" title="verbindliche Bestellung" /&gt;
              &lt;/form&gt; 
  
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/section&gt;
  &lt;?php
  get_footer();
  ?&gt;
  </code></pre>
  
  <p>then the <strong>checkresellerid.php</strong> that checks the database if the RiD(ResellerID) is in the DB:</p>
  
  <pre><code>&lt;?php
  //header('Content-Type: text/html; charset=utf-8');
  include 'db_connect.php';
  
  // Check connection
  if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  
  if (isset($_GET['rid'])) {
  //  $rid = "R10369";
  $rid = $_GET['rid'];
  
  
  $result = mysqli_query($con, "SELECT rid FROM reseller WHERE rid = '" . $rid . "'");
  
  $ergebnis = mysqli_num_rows($result);
  
  if ($ergebnis == 1) {
      $RJSON = [];
      $RQUERY = mysqli_query($con, "SELECT * FROM reseller WHERE rid = '" . $rid . "'");
      while ($row = mysqli_fetch_array($RQUERY, MYSQL_ASSOC)) {
  
          $RJSON[] = array(
  //          'rid' =&gt; $row['rid'],
              'name' =&gt; $row['name'],
              'adresse' =&gt; $row['adresse'],
              'email' =&gt; $row['email'],
              'phone' =&gt; $row['phone']
  
          );
      }
  
  
      echo json_encode($RJSON);
  
  } else {
      echo 'false';
  }
  
  mysqli_close($con);
  }
  ?&gt;
  </code></pre>
  
  <p>When I enter something in the Input Field(no matter if its in the DB or not) I get a 500 Internal Server Error. But why my other Ajax calls on the Site like my "Love a product" feature in the woocommerce shop loop are working but not this little fella? on my localhost is everything runnning correctly. Did it has anything to do because its a custom php file(template page file)?</p>
  
  <p><strong>Check the Example Page</strong> <a href="http://staging.xxx/reseller/" rel="nofollow noreferrer">>>> Here &lt;&lt;&lt;</a></p>
  
  <p>I tried registering a function in functions.php but that crashed the whole site.
  How can I pack my php script in this functions.php and call it correctly? Why is it even neccesary to make it that complicated? I am googling this for hours and tryied many ways, nothing works...</p>
  
  <p>Thanks for any help and sorry for my bad englisch.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>my functions.php looks like this:</p>
  
  <pre><code>&lt;?php
  
  /****************************************
  Theme Setup
  *****************************************/
  
  require_once( get_template_directory() . '/lib/init.php' );
  require_once( get_template_directory() . '/lib/theme-helpers.php' );
  require_once( get_template_directory() . '/lib/theme-functions.php' );
  require_once( get_template_directory() . '/lib/theme-comments.php' );
  
  
  /****************************************
  Require Plugins
  *****************************************/
  
  require_once( get_template_directory() . '/lib/class-tgm-plugin-activation.php' );
  require_once( get_template_directory() . '/lib/theme-require-plugins.php' );
  
  add_action( 'tgmpa_register', 'mb_register_required_plugins' );
  add_theme_support( 'woocommerce' );
  
  
  /****************************************
  Misc Theme Functions
  *****************************************/
  
  /**
   * Define custom post type capabilities for use with Members
   */
  function mb_add_post_type_caps() {
      // mb_add_capabilities( 'portfolio' );
  }
  
  /**
   * Filter Yoast SEO Metabox Priority
   */
  add_filter( 'wpseo_metabox_prio', 'mb_filter_yoast_seo_metabox' );
  function mb_filter_yoast_seo_metabox() {
      return 'low';
  }
  
  
  function checkResellerId(){
      include 'db_connect.php';
      wp_localize_script( 'function', 'checkResellerId', array( 'ajaxurl' =&gt; admin_url( 'admin-ajax.php' ) ) );
  
      // Check connection
      if (mysqli_connect_errno()) {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
  
      if (isset($_GET['rid'])) {
      //  $rid = "R10369";
          $rid = $_GET['rid'];
  
  
          $result = mysqli_query($con, "SELECT rid FROM reseller WHERE rid = '" . $rid . "'");
  
          $ergebnis = mysqli_num_rows($result);
  
          if ($ergebnis == 1) {
              $RJSON = [];
              $RQUERY = mysqli_query($con, "SELECT * FROM reseller WHERE rid = '" . $rid . "'");
              while ($row = mysqli_fetch_array($RQUERY, MYSQL_ASSOC)) {
  
                  $RJSON[] = array(
      //              'rid' =&gt; $row['rid'],
                      'name' =&gt; $row['name'],
                      'adresse' =&gt; $row['adresse'],
                      'email' =&gt; $row['email'],
                      'phone' =&gt; $row['phone']
  
                  );
              }
  
  
              echo json_encode($RJSON);
  
          } else {
              echo 'false';
          }
  
          mysqli_close($con);
      }
  }
  
  add_action( 'wp_ajax_nopriv_product_s', 'checkResellerId' );
  add_action( 'wp_ajax_product_s', 'checkResellerId' );
  
  function resellerMail(){
      wp_localize_script( 'function', 'resellerMail', array( 'ajaxurl' =&gt; admin_url( 'admin-ajax.php' ) ) );
      $zieladresse = 'xxx';
  
      $absenderadresse = 'xxxReseller';
  
      $absendername = 'xxxReseller';
  
      $betreff = 'Reseller-Bestellung';
  
      $trenner = ":\t";
  
      if ($_SERVER['REQUEST_METHOD'] === "POST") {
  
              $header = array();
              $header[] = "From: ".mb_encode_mimeheader($absendername, "ISO-8859-15", "Q")." &lt;".$absenderadresse."&gt;";
              $header[] = "MIME-Version: 1.0";
              $header[] = "Content-type: text/plain; charset=ISO-8859-15";
              $header[] = "Content-transfer-encoding: 8bit";
  
          $mailtext = "";
  
          foreach ($_POST as $name =&gt; $wert) {
              if (is_array($wert)) {
                          foreach ($wert as $einzelwert) {
                                  $mailtext .= $name.$trenner.$einzelwert."\n";
                  }
              } else {
                  $mailtext .= $name.$trenner.$wert."\n";
              }
          }
  
          mail(
              $zieladresse, 
              mb_encode_mimeheader($betreff, "ISO-8859-15", "Q"), 
              $mailtext,
              implode("\n", $header)
          ) or die("Die Mail konnte nicht versendet werden.");
          echo $mailtext; // nur check für ajax response, später wieder entfernen!!!!
          echo 'Ihre Bestellung wurde an uns versandt.&lt;br&gt;Sie werden telefonisch von uns kontaktiert um die Bestellung zu bestätigen.';
          exit;
      }
  
  //header("Content-type: text/html; charset=utf-8");
  }
  
  add_action( 'wp_ajax_nopriv_product_s', 'resellerMail' );
  add_action( 'wp_ajax_product_s', 'resellerMail' );
  </code></pre>
  
  <p>Screen of the 500 Error:
  ![Screenshot 500 Internal error][2]</p>