title: How can I make a jQuery script aware of a Codeigniter variable?
body: <p>I have been developing a blogging application with CodeIgniter 3.1.8 and Twig. I am currently working on making a <strong>newsletter subscription</strong> system.</p>
  <p>I have created a table named <em>newsletter</em> with 3 columns: <code>id</code>, <code>email</code> and <code>subscription_date</code>.</p>
  <p>The newsletter subscription form:</p>
  <pre><code>&lt;div id=&quot;messags&quot; class=&quot;is-hidden h-text-center&quot;&gt;
    &lt;div class=&quot;success is-hidden alert-box alert-box--success&quot;&gt;You have successfully subscribed to our newsletter&lt;/div&gt;
    &lt;div class=&quot;fail is-hidden alert-box alert-box--error&quot;&gt;Sorry, the newsletter subscription filed&lt;/div&gt;
  &lt;/div&gt;
  &lt;form name=&quot;newsletter&quot; method=&quot;post&quot; action=&quot;{{base_url}}newsletter/subscribe&quot; id=&quot;newsletterForm&quot; class=&quot;group&quot; novalidate&gt;
    &lt;input type=&quot;email&quot; value=&quot;{{set_value('email') | striptags}}&quot; name=&quot;email&quot; class=&quot;email&quot; data-rule-required=&quot;true&quot; placeholder=&quot;Your Email Address&quot;&gt; 
    &lt;input type=&quot;submit&quot; name=&quot;subscribe&quot; value=&quot;subscribe&quot;&gt;
  &lt;/form&gt;
  </code></pre>
  <p>The <strong>Newsletter_model</strong> model:</p>
  <pre><code>class Newsletter_model extends CI_Model {
      public function subscriber_exists() {   
          $query = $this-&gt;db-&gt;get_where('newsletter', ['email' =&gt; $this-&gt;input-&gt;post('email')]);
          return $query-&gt;num_rows() &gt; 0;
      }
  
      public function add_subscriber() {
          $data = [
              'email' =&gt; $this-&gt;input-&gt;post('email'),
              'subscription_date' =&gt; date('Y-m-d H:i:s')
          ];
          return $this-&gt;db-&gt;insert('newsletter', $data);
      }
  }
  </code></pre>
  <p>As you can see above, I use the <code>subscriber_exists()</code> to make sure there are <em>no duplicate emails</em>.</p>
  <p>The <strong>Newsletter</strong> controller is quite simple:</p>
  <pre><code>class Newsletter extends CI_Controller {
    public function __construct(){
          parent::__construct();
      }
  
    public function subscribe(){
      $data['is_new_subscriber'] = true;
      if (!$this-&gt;Newsletter_model-&gt;subscriber_exists()) {
        $this-&gt;Newsletter_model-&gt;add_subscriber();
      } else {
        $data['is_new_subscriber'] = false;
      }
    } 
  }
  </code></pre>
  <h3>The problem</h3>
  <p>I use jQuery AJAX to submit the form and the script is unaware of the <code>is_new_subscriber</code> variable:</p>
  <pre><code>(function($) {
  
      // Add subscriber via AJAX
      $(&quot;#newsletterForm&quot;).validate({
          rules: {
              email: {
                  email: true
              }
          },
  
          submitHandler: function(form) {
              var form = $(&quot;#newsletterForm&quot;),
                  $fields = form.find('input[type=&quot;email&quot;]'),
                  url = form.attr('action'),
                  data = form.serialize();
              $.ajax({
                  type: &quot;POST&quot;,
                  url: url,
                  data: data,
                  success: function() {
                      $('#messags').slideDown(250).delay(2500).slideUp(250);
                      if (is_new_subscriber == true) {
                          $fields.val('');
                          $('#messags .success').show();
                      } else {
                          $('#messags .fail').show();
                      }
                  }
              });
          }
      });
  
  })(jQuery);
  </code></pre>
  <h2>UPDATE</h2>
  <p>Adding echo <code>json_encode($data)</code> to the <code>subscribe()</code> and changing the <code>submitHandler</code> to the below ddi not splve the issue:</p>
  <pre><code>submitHandler: function(form) {
    var form = $(&quot;#newsletterForm&quot;),
    $fields = form.find('input[type=&quot;email&quot;]'),
    url = form.attr('action'),
    data = form.serialize();
    $.ajax({
      dataType: &quot;json&quot;,
      type: &quot;post&quot;,
      url: url,
      data: data,
      success: function() {
        $('#messags').slideDown(250).delay(2500).slideUp(250);
        $fields.val('');
        if (data.is_new_subscriber == true) {
          $('#messags .success').show();
        } else {
          $('#messags .fail').show();
        }
      }
    });
  }
  </code></pre>
  <p>How can I fix this issue?</p>