title: Use of variable in the Ajax post
body: <p>In my application I am using Ajax post like:</p>
  
  <pre><code>$('#fb_contentarea_col3 #fb_contentarea_col1down2 #saveForm').live("click", function() {
    if (!$("#formName").val() == "") {
      if (saveDraft == 'on')
        status = "Incompleted";
      else
        status = "Completed";
  
      var formname = $("#formName").val();
      $.ajax({
        type: "POST",
        url: "http://localhost/FormBuilder/index.php/forms/saveForm/",
        async: false,
        data: "formname=" + formname + "&amp;status=" + status,
        success: function(msg) {
          getformid = msg;
          //returned FOrm id is saved in the JQuery variable
        }
        //success
      });
      //ajax
  
      $("#fb_contentarea_col1down21 div").each(function() {
        alert("Form id " + getformid);
        //alerts me the Form id retrieved
        var checked = "false";
        var id = $(this).attr("id");
        var fsize = $("#input" + id + "").width();
        var ftype = $("#input" + id + "").attr('data-attr');
        var finstr = $("#instr" + id + "").text();
        var fname = $("#label" + id + "").clone().html().replace(/&lt;span.*/, '');
  
        if ($("#label" + id + "&gt; span.req").length &gt; 0)
        {
          checked = "true";
        }
  
        $.ajax({
          type: "POST",
          url: "http://localhost/FormBuilder/index.php/forms/saveField",
          async: false,
          data: "sequence_no=" + id + "&amp;name=" + fname + "&amp;type=" + ftype + "&amp;size=" + fsize + "&amp;instr=" + finstr + "&amp;formid=" + getformid + "&amp;required=" + checked,
  
          success: function(msg) {
              //alert( "Data Saved: " + msg);
          }
          //success
        });
        //ajax
      }
      //for each DIv in mu design page i am saving them as Field
    }
    //if formname exists
  }
  //saveForm
  </code></pre>
  
  <p>The Form id as returned from the Controller side is saved in the variable getformid correctly, But it is not reflected in the savefield ajax post..And it is saving as 0 only even it alerts as the returned Form id..Please suggest me..</p>
  
  <p>Note ::the question is a reference to</p>
  
  <p><a href="https://stackoverflow.com/questions/1264343/varaibles-inside-success-function-of-ajax-post">https://stackoverflow.com/questions/1264343/varaibles-inside-success-function-of-ajax-post</a></p>
  
  <p>In addition I am giving here the Code in my Controller for saveForm</p>
  
  <pre><code>function saveForm()
  {
    $this-&gt;data['Form']['name']=$this-&gt;params['form']['formname'];
    $this-&gt;data['Form']['created_by']=$this-&gt;Session-&gt;read('userId');
    $this-&gt;data['Form']['status']=$this-&gt;params['form']['status'];
    $this-&gt;data['Form']['access']="Private";
    $formId=$this-&gt;Form-&gt;saveForms($this-&gt;data);
  
    $this-&gt;set('formId',$formId);
  }
  </code></pre>
  
  <p>And my Model saveForms is like</p>
  
  <pre><code>function saveForms($data)//design saveForm
  {
    $this-&gt;data['Form']['name']=$data['Form']['name'];
    $this-&gt;data['Form']['created_by']=$data['Form']['created_by'];
    $this-&gt;data['Form']['status']=$data['Form']['status'];
    $this-&gt;data['Form']['access']=$data['Form']['access'];
    $this-&gt;save($this-&gt;data);
    return $this-&gt;id;//returns the saved Form id to the controller
  }
  </code></pre>
  
  <p>And my save_form.ctp in my views folder is like</p>
  
  <pre><code>&lt;?php echo $formId;?&gt;
  </code></pre>