2009-08-12 59 views
0

在我的應用程序正在使用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 

從控制器端返回保存在變量getformid正確的,但它並沒有反映在表單ID在savefield AJAX post..And它被保存爲0只,甚至它會提醒作爲返回表格id..Please建議我..

注::這個問題是

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); 
} 

我的模式保存表格就像

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 
} 

而且我save_form.ctp在我的意見的文件夾像

<?php echo $formId;?> 

回答

4

您在爲AJAX調用的success處理程序內設置getformid,但代碼爲使用getformid在該功能之外。

不要忘記,當你打電話給$.ajax之類的東西時,函數立即返回 - 在AJAX調用完成之前,當然還有success函數已經運行之前。這就是異步。因此,你想要在AJAX請求完成後執行的任何代碼都必須進入success函數(或當然,在success之內調用的另一個函數中)。

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 
)}; 

然後,您可以訪問已通過調用的getValue()的formId變量

方法設置的值