2013-10-07 67 views
0

我做了一個創建函數,我想將id存儲在一個變量中,然後傳遞這個id來編輯函數,我爲此編寫了一些代碼,但是錯誤存在,我該怎麼做? 這裏是我的代碼:如何從ajax請求獲取id然後將其發送到symfony函數?

var InvoiceId; 
    $("form").submit(function(e) {     
     e.preventDefault(); 
     var url = $(this).attr('action'); 
     var data = $(this).serialize(); 
     $.ajax({ 
      type: "POST", 
      url: url, 
      data: data, 
     }) 
     .success(function(result) { 
       invoiceid=result.id; 
       $("#edit").click(); 
      if(result.success) { 
       $('#result').css({'color':'black','background-color':'#8F8','display':'Block','width':'200px'}); 
       $('#result').html('Invoices Record Inserted'); 
       setTimeout(function(){ 
        $('#result').hide(); 
        },3000); 
        window.location.href = "{{ path('invoicepreview') }}"; 
      } 
     }); 
     this.reset(); 
    }); 
    $("#edit").click(function(){ 
    = "{{ path('invoices_edit', {'id': invoiceid }) }}"; 
}); 

這裏是我的創造功能:

if($request->isXmlHttpRequest()) { 
     $response = new Response(); 
     $output = array('success' => true, 'title' => $entity->gettitle(), 'id' => $entity->getId(), 'notes' => $entity->getnotes(), 'accountid' => $entity->getaccountid(), 'clientid' => $entity->getClientID(), 'status' => $entity->getstatus(), 'totalamount' => $entity->getTotalAmount(), 'paidamount' => $entity->getPaidAmount(), 'balanceamount' => $entity->getBalanceAmount(), 'createdby' => $entity->getcreatedby(), 'updatedby' => $entity->getupdatedby(), 'createddatetime' => $entity->getcreateddatetime(), 'updateddatetime' => $entity->getupdateddatetime()); 
     $response->headers->set('Content-Type', 'application/json'); 
     $response->setContent(json_encode($output)); 

    } 

這裏是我的編輯功能:

public function editAction($id) 
{ 
    $em = $this->getDoctrine()->getManager(); 

    $entity = $em->getRepository('InvoicesInvoicesBundle:Invoices')->find($id); 

    if (!$entity) { 
     throw $this->createNotFoundException('Unable to find Invoices entity.'); 
    } 

    $editForm = $this->createForm(new InvoicesType(), $entity); 
    $deleteForm = $this->createDeleteForm($id); 

    return $this->render('InvoicesInvoicesBundle:Invoices:edit.html.twig', array(
     'entity'  => $entity, 
     'edit_form' => $editForm->createView(), 
     'delete_form' => $deleteForm->createView(), 
    )); 
} 

這裏的錯誤:

Variable "invoiceid" does not exist in InvoicesInvoicesBundle:Invoices:new.html.twig at line 362 

回答

2

如果你想成爲的話生成動態中JS的路徑,特別是如果你正在做的AJAX-Y的東西負載,你幾乎可以肯定要添加的FOS的JS Routing Bundle到項目

正確配置後您的JS的路徑會是這樣的

關於如何配置與使用JSBundle路徑和路線
Routing.generate('invoices_edit', { id: invoiceid }); 

說明可以發現here

+0

我 –

+0

不工作這是一個正確的和工作方案。 *「不工作」*不是錯誤。你是否正確地遵循安裝步驟? – Touki