title: Returning php variable with ajax form
body: <p>What I need help with is to get the success to set the PHP variable, so that the #result div gets a value (not a JavaScript .html thing, I need it to set the PHP variable).</p>
  
  <p>Is this possible without setting a new URL with the PHP variable?</p>
  
  <p>In other words, can I send a JavaScript variable to PHP variable without doing this:<br>
  <a href="https://stackoverflow.com/questions/8191124/send-javascript-variable-to-php-variable">send javaScript variable to php variable</a></p>
  
  <p>HTML</p>
  
  <pre><code>&lt;div id="hidden_slidersize"&gt;&lt;/div&gt;
  &lt;script type="text/javascript"&gt;
      $(function(){
          if ($('body').width() &gt;= 960) {
              var sizeOfSlider = 500;
              } else {
              var sizeOfSlider = ($('body').width())/2;
              }
          $('#hidden_slidersize').html('&lt;form id="dataform" method="post" name="hiddentrick_form" action="data.php"&gt;&lt;fieldset&gt;&lt;input id="hiddentrick" name="hiddentrick" type="hidden" value="' + sizeOfSlider + '" /&gt;&lt;input class="datasubmit" type="submit" value="senddata" /&gt;&lt;/fieldset&gt;&lt;/form&gt;');  
  
      });
  &lt;/script&gt;
  
  &lt;div id="result"&gt;&lt;?php echo $sizeofslider ?&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Form Javascript</p>
  
  <pre><code>$(document).ready(function() {
  
  $(document).on('submit', 'form#dataform', function () { 
  
  $.ajax({  
    type: 'POST',  
    url: 'data.php',  
    data: $('#dataform').serialize(),
    dataType:'html',  
    success: function(data) {alert(data);},
    error: function(data) {
    //AJAX request not completed
    alert('error');
    }
  });  
  return false;  
  
    });
  });
  </code></pre>
  
  <p>Form php</p>
  
  <pre><code>&lt;?php
  if( isset($_POST) ){
  
      $sizeofslider = $_POST['hiddentrick'];
      echo $sizeofslider;
  }
  ?&gt;
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>It seems like I didn't explain clearly enough.
  What I need to do is to make a javascript variable into a php variable and then calculate with it.</p>
  
  <p>Here's the calculation that comes after:</p>
  
  <pre><code>&lt;?php 
  
  $czes = array(
                      'sort_order' =&gt; 'desc',
                      'sort_column' =&gt; 'post_date',
                      'hierarchical' =&gt; 1,
                      'exclude' =&gt; '',
                      'include' =&gt; '',
                      'meta_key' =&gt; '',
                      'meta_value' =&gt; '',
                      'authors' =&gt; '',
                      'child_of' =&gt; 4,
                      'exclude_tree' =&gt; '',
                      'number' =&gt; '',
                      'offset' =&gt; 0,
                      'post_type' =&gt; 'page',
                      'post_status' =&gt; 'publish'
                      ); 
                      $pages = get_pages($czes); 
  
                      foreach ($pages as $page) { ?&gt;
  
          &lt;?php $postmetapic1 = get_post_meta($page-&gt;ID, 'pic1', true); ?&gt;
          &lt;?php $postmetapic2 = get_post_meta($page-&gt;ID, 'pic2', true); ?&gt;
          &lt;?php $postmetapic3 = get_post_meta($page-&gt;ID, 'pic3', true); ?&gt;
  
          &lt;?php
          $holder_width_first=0;
          if (!empty($postmetapic1)) {$holder_width_first++;}
          if (!empty($postmetapic2)) {$holder_width_first++;}
          if (!empty($postmetapic3)) {$holder_width_first++;}
          $holder_width= $holder_width_first*$sizeofslider;
          ?&gt;
  
          &lt;div id="s_sli_hol_&lt;?php echo $page-&gt;ID ?&gt;" class='slider_holder small_slider_holder' style="width: &lt;?php echo $holder_width ?&gt;px"&gt;Here's the img-tags&lt;/div&gt;
                      &lt;?php } ?&gt;
  </code></pre>