在Ajax帖子中使用变量

时间:2009-08-12 10:28:28

标签: jquery cakephp

在我的应用程序中,我使用的是Ajax帖子:

$('#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 + "&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(/<span.*/, '');

      if ($("#label" + id + "> span.req").length > 0)
      {
        checked = "true";
      }

      $.ajax({
        type: "POST",
        url: "http://localhost/FormBuilder/index.php/forms/saveField",
        async: false,
        data: "sequence_no=" + id + "&name=" + fname + "&type=" + ftype + "&size=" + fsize + "&instr=" + finstr + "&formid=" + getformid + "&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

从Controller端返回的Form id正确保存在变量getformid中,但它没有反映在savefield ajax post中。它只保存为0,即使它作为返回的Form id提醒..请建议我..

注意::问题是对

的引用

https://stackoverflow.com/questions/1264343/varaibles-inside-success-function-of-ajax-post

另外我在这里给我的控制器中的代码为saveForm

function saveForm()
{
  $this->data['Form']['name']=$this->params['form']['formname'];
  $this->data['Form']['created_by']=$this->Session->read('userId');
  $this->data['Form']['status']=$this->params['form']['status'];
  $this->data['Form']['access']="Private";
  $formId=$this->Form->saveForms($this->data);

  $this->set('formId',$formId);
}

我的模型saveForms就像

function saveForms($data)//design saveForm
{
  $this->data['Form']['name']=$data['Form']['name'];
  $this->data['Form']['created_by']=$data['Form']['created_by'];
  $this->data['Form']['status']=$data['Form']['status'];
  $this->data['Form']['access']=$data['Form']['access'];
  $this->save($this->data);
  return $this->id;//returns the saved Form id to the controller
}

我的views文件夹中的save_form.ctp就像

<?php echo $formId;?>

2 个答案:

答案 0 :(得分:4)

您在AJAX调用的getformid处理程序中设置success,但使用 getformid的代码在该函数之外。

永远不要忘记,当你调用$.ajax之类的东西时,函数会在AJAX调用完成之前立即返回,当然也就是在你的success函数运行之前。这就是异步的原因。

因此,在AJAX请求完成后你想要执行的任何代码必须进入success函数(或者在success内部调用的另一个函数中)。

答案 1 :(得分:1)

您可以创建一个函数并像参数一样传递它。例如:

function param() {
    this.array = new Array(1);
    this.setValue = function(v) { this.array[0] = v; }
    this.getValue = function() { return this.array[0]; }
}

然后在你的ajax请求中。做类似下面的事情:

var formId = new param;
$.ajax({
    type: "POST",
    url: "http://localhost/FormBuilder/index.php/forms/saveForm/",
    async: false,
    data: "formname="+formname+"&status="+status,
    success: function(msg){
          formId.setValue(msg);
    }//success
)};

然后,您可以通过调用 formId 变量的 getValue()方法来访问设置的值

相关问题