我创建了一个创建函数,我想将 id 存储在一个变量中,然后将此 id 传递给编辑函数,我为此编写了一些代码,但存在错误,我该怎么做?这是我的代码:
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