title: Can't save data in WordPress via AJAX (plugin)
body: <p>I'm working in a new plugin and I want the users use a form to send his/her email. This email would be saved in a custom table in the database.</p>
  <p>This is the html form:</p>
  <pre><code>&lt;form class=&quot;form-inline singup subscribe-database&quot; action=&quot;#&quot; method=&quot;post&quot; data-url=&quot;&lt;?php echo verde_product_info('extend_url') ?&gt;&quot;&gt;
  &lt;input type=&quot;hidden&quot; name=&quot;language&quot; value=&quot;&lt;?php echo verde_top('language'); ?&gt;&quot;&gt;
  &lt;input type=&quot;hidden&quot; name=&quot;database&quot; value=&quot;&lt;?php echo $wpdb-&gt;prefix; ?&gt;&quot;&gt;
  &lt;input type=&quot;email&quot; name=&quot;email&quot; placeholder=&quot;Email&quot; required&gt;
  &lt;button type=&quot;submit&quot; class=&quot;btn&quot;&gt;Send&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  <p>When it is sending, the js code is the next:</p>
  <pre><code>var urlplugin = jQuery('.subscribe-database').attr('data-url');
      
      jQuery('.subscribe form').submit(function() {
          var postdata = jQuery('.subscribe form').serialize();
          jQuery.ajax({
              type: 'POST',
              url: urlplugin + '/themes/php/sendmail.php',
              data: postdata, 
              dataType: 'json',
              success: function(json) {
                  console.log(json);
              },
              error: function (xhr, ajaxOptions, thrownError) {
                  console.log(xhr.status);
                  console.log(thrownError);
              }
          });
          return false;
      });
  </code></pre>
  <p>And the file <strong>sendmail.php</strong> when the error happens is the next one:</p>
  <pre><code>global $wpdb;
  
  
  if(!isset($wpdb))
  {
      require_once('../../../../../wp-includes/wp-db.php');
  }
  
  switch ($_POST['language']) {
      case 'english':
          $success1 = 'Thanks for your subscription!';
          $error1 = 'Insert a valid email address';
          break;
      case 'spanish':
          $success1 = '&amp;#161;Gracias por tu suscripci&amp;oacute;n!';
          $error1 = 'Escribe un email v&amp;aacute;lido';
          break;
      case 'french':
          $success1 = 'Merci pour votre abonnement!';
          $error1 = 'Ins&amp;eacute;rez une adresse e-mail valide';
          break;
      case 'german':
          $success1 = 'Vielen Dank f&amp;#252;r Ihr Abonnement!';
          $error1 = 'Legen Sie eine g&amp;#252;ltige E-Mail-Adresse';
          break;
      case 'italian':
          $success1 = 'Grazie per il vostro abbonamento!';
          $error1 = 'Inserire un indirizzo email valido';
          break;
      case 'portuguese':
          $success1 = 'Obrigado pela sua inscri&amp;#231;&amp;#227;o!';
          $error1 = 'Insira um email v&amp;aacute;lido';
          break;
  }
  
  function isEmail($email) {
      return(preg_match(&quot;/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i&quot;, $email));
  }
  
  if($_POST['email']) {
      $table = $_POST['database'] . 'verde_emails';
      $subscriber_email = ($_POST['email']);
  
      if(!isEmail($subscriber_email)) {
          $array = array();
          $array['valid'] = 0;
          $array['message'] = $error1;
          echo json_encode($array); 
      }
      else {
          $array = array();
          $array['valid'] = 1;
          $array['message'] = $success1;
          echo json_encode($array);
          $wpdb-&gt;insert( 
              $table, 
              array( 
                  'option_email' =&gt; $subscriber_email, 
              )
          )
      }
  }
  </code></pre>
  <p>When I use the form I get the next error in the browser console:</p>
  <pre><code>200
  scripts-new.js:43 SyntaxError: Unexpected token &lt; in JSON at position 0
      at JSON.parse (&lt;anonymous&gt;)
      at parseJSON (jquery-1.9.1.min.js:2:4322)
      at Fn (jquery-1.9.1.min.js:4:15175)
      at k (jquery-1.9.1.min.js:4:13895)
      at XMLHttpRequest.r (jquery-1.9.1.min.js:4:18226)
  </code></pre>
  <p>But it works fine if I comment the next code in the file <strong>sendmail.php</strong>:</p>
  <pre><code>$wpdb-&gt;insert( 
  $table, 
      array( 
          'option_email' =&gt; $subscriber_email, 
      )
  )
  </code></pre>
  <p>but obviously, the email can't be saved in the database.</p>
  <p>I'm lost with this error and I will be very grateful if someone can help me.</p>
  <p>Sorry for my English and thank you very much.</p>