Friday, 13 September 2013

how to add button for dynamic form creation and then save to database?

how to add button for dynamic form creation and then save to database?

i make a form of sections i want to add a button on form when i click
button the complete form build again and then we add values to store
database,i add more then 4 forms through submit button how i do this?
here is my form:
{% extends '::base.html.twig' %}
{% block body -%}
<title>{% block title %}Proposals>Create Sections{% endblock %}</title>
{% block stylesheets %}
<link href="{{ asset('styles/bootstrap1.css') }}" rel="stylesheet" />
{% endblock %}
<div id="fuelux-wizard" class="wizard row">
<ul class="wizard-steps">
<li data-target="#step1">
<span class="step">1</span>
<span class="title">Create <br> Proposals</span>
</li>
<li data-target="#step2" class="active">
<span class="step">2</span>
<span class="title">Sections</span>
</li>
<li data-target="#step3">
<span class="step">3</span>
<span class="title">Proposal <br> Fees</span>
</li>
</ul>
</div>
<h3>Sections</h3>
<form action="{{ path('sections_create') }}" method="post" {{
form_enctype(form) }}>
{{ form_widget(form) }}
<p>
<button id="edit" type="button" class="btn btn-primary"><i
class="icon-chevron-left"></i>Previous</button>
<button type="submit" class="btn btn-success"> Next <i
class="icon-chevron-right"></i></button>
</p>
</form>
<div id="result"></div>
<ul class="record_actions">
<li>
<a href="{{ path('sections') }}">
Back to the list
</a>
</li>
</ul>
{% endblock %}
{% block javascripts %}
<script src="{{ asset('js/jquery-1.10.2.js') }}"
type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("form").submit(function(e) {
e.preventDefault();
var url = $(this).attr('action');
var data = $(this).serialize();
$.ajax({
type: "POST",
url: url,
data: data,
}).done(function( result ) {
if(result.success) {
$('#result').css({'color':'black','background-color':'#8F8','display':'Block','width':'200px'});
$('#result').html('Sections Record Inserted');
setTimeout(function(){
$('#result').hide();
},3000);
window.location.href = "{{ path('propsalfees_new') }}";
}
});
this.reset();
});
$("#edit").click(function(){
window.location.href= "{{ path('proposals_edit', {'id':
proposalid }) }}";
});
});
</script>
{% endblock %}
and here is my controller to save data:
<?php
namespace Proposals\ProposalsBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Proposals\ProposalsBundle\Entity\Proposals;
use Proposals\ProposalsBundle\Form\ProposalsType;
use \DateTime;
/**
* Proposals controller.
*
*/
class ProposalsController extends Controller
{
/**
* Lists all Proposals entities.
*
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities =
$em->getRepository('ProposalsProposalsBundle:Proposals')->findAll();
return
$this->render('ProposalsProposalsBundle:Proposals:index.html.twig',
array(
'entities' => $entities,
));
}
/**
* Creates a new Proposals entity.
*
*/
public function createAction(Request $request)
{
$user = $this->get('security.context')->getToken()->getUser();
$userId = $user->getId();
$entity = new Proposals();
$form = $this->createForm(new ProposalsType(), $entity);
$form->bind($request);
$postData =
$request->request->get('proposals_proposalsbundle_proposalstype');
$name_value = $postData['firstname'];
$entity->setClientID($name_value);
$entity->setCreatedBy($userId);
$entity->setUpdatedBy($userId);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
$date=date('Y-m-d H:i:s');
$_SESSION['id'] =$entity->getId();
if($request->isXmlHttpRequest()) {
$response = new Response();
$output = array('success' => true, 'description' =>
$entity->getdescription(), 'id' => $entity->getId(),
'proposalname' => $entity->getproposalname(), 'templateid' =>
$entity->gettemplateid(), 'clientid' => $entity->getClientID(),
'status' => $entity->getstatus(), 'createdby'
=>$entity->getCreatedBy(), 'updatedby' =>
$entity->getUpdatedBy(), 'createddatetime' => $date,
'updateddatetime' => $date);
$response->headers->set('Content-Type', 'application/json');
$response->setContent(json_encode($output));
return $response;
}
return $this->redirect($this->generateUrl('proposals_show',
array('id' => $entity->getId())));
}
return
$this->render('ProposalsProposalsBundle:Proposals:new.html.twig',
array(
'entity' => $entity,
'form' => $form->createView(),
));
}

No comments:

Post a Comment