title: ajax form submission with second form submission if successful
body: <p>I'm currently building a system using ExpressionEngine that allows users to answer questions in exchange for points, they can then use these points to claim prizes.</p>
  
  <p>I've been writing the functionality to claim a prize, it needs to do the following:</p>
  
  <ul>
  <li>Check the prize is in stock</li>
  <li>Check the user has enough points</li>
  <li>If in stock and enough points submit a form which lets the admin know to send the prize out</li>
  </ul>
  
  <p>I have the following code which I think is nearly there however I'm struggling with the last bit, the actual success/failure parts. I've used jQuery Ajax:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  $(function() {
      $("#prizeClaim").submit(function() {
          var data = "entry_id={entry_id}&amp;member_id={logged_in_member_id}&amp;prize_title={title}&amp;prize_points={prize_points}";
          $.ajax({
              url: "/prizes/prize_validation/",
              data: data, 
              success: function(html) {
                  alert(html); // alert the output from the PHP Script
              }
          });
          return false;
      });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>This code currently just outputs the html of prize_validation in an alert, this is the code used on the validation page so far:</p>
  
  <pre><code>&lt;?php
  
    // All data required made into vars
    $entry_id = ($_GET['entry_id']);
    $member_id = ($_GET['member_id']);
    $prize_title = ($_GET['prize_title']);
    $prize_points = ($_GET['prize_points']);
  
    // Select the stock column 
    $query = ee()-&gt;db-&gt;query("SELECT field_id_6 FROM exp_channel_data WHERE entry_id = $entry_id");
  
    if ($query-&gt;num_rows() &gt; 0)
    {
        foreach($query-&gt;result_array() as $row)
        {
            // define stock_total
            $stock_total = $row['field_id_6'];
  
            // If stock more than 0 go ahead
            if($stock_total &gt; 0) {
              //remove 1 stock item
              ee()-&gt;db-&gt;query("UPDATE exp_channel_data SET field_id_6 = field_id_6 - 1 WHERE entry_id = $entry_id");
  
              //update users points
              $data = array('member_id' =&gt; $member_id, 'prize_points' =&gt; $prize_points, 'prize_id' =&gt; $entry_id);
              $sql = ee()-&gt;db-&gt;insert_string('exp_rmdy_member_prize_data', $data);
              ee()-&gt;db-&gt;query($sql);
            }
        }
    }
  ?&gt;
  
  {exp:freeform:form form_id="1" return="thanks"}
      &lt;input type="hidden" name="name" value="{username}" id="freeform_name" maxlength="150"&gt;
      &lt;input type="hidden" name="email" value="{email}" id="freeform_email" maxlength="150"&gt;
      &lt;input type="hidden" name="company" value="{exp:channel:entries channel='company' dynamic='no'}{if {member_code} == {company_code}}{title}: {company_address}{/if}{/exp:channel:entries}" id="freeform_company" maxlength="200"&gt;
      &lt;input type="hidden" name="prize" value="&lt;?php echo $prize_title ?&gt;" id="freeform_prize" maxlength="150"&gt;
      &lt;p&gt;&lt;input type='submit' name='submit' value='Process order'&gt;&lt;/p&gt;
  {/exp:freeform:form}
  </code></pre>
  
  <p>This code checks the stock and if the item is in stock it then removes the amount of points from the users total points, this works. However once this has happened I want to submit the Freeform, I'm not 100% sure if this should be within the prize_validation file or in a third location. But after lots of experimentation I'm still not sure how to go about either!</p>
  
  <p>Any hints/tips much appreciated!</p>