0

In a form, when I am selecting the name of the client from a drop down list, next line is to select name of the product from drop down list, and it gives all the products, but only need those items manufacturing by this selected client. (in the database file of client primary key is client_id and the production file primary key is item_id and foreign key is client_id).

I am new to symfony, can anybody please help me?

Thanks,

greg0ire
  • 22,714
  • 16
  • 72
  • 101
  • How do you plan to do this? With javascript only? With AJAX? With php only? – greg0ire Apr 13 '11 at 09:59
  • better option to do this is to use Ajax, because it will be a better user experience than going for the PHP(only when javascript is disabled) way... – Harish Kurup Jun 15 '11 at 13:04

1 Answers1

0

In Symfony Form,

class ClientForm extends sfForm
{
   public function configure() 
   {
      $clients_data = ClientsData::getAllClients(); //get from database
      $clients = array_merge(array("--Select Clients--", $clients_data);
      $this->setWidgets(array(
          "clients" => new sfWidgetFormChoice(array("choices" =>$clients)),
          "products" =>new sfWidgetFormChoice(array("choices" =>array("--Select Product--")))
      ));

      $this->setValidators(array(
           "clients" => new sfValidatorChoice(array("choices" =>array_keys($clients_data))),
            "products" => new sfValidatorString()
      ));
   } 
}

in View

<script type="text/javascript">
    $(document).ready(function(){
         $("#clients").change(function(){
        var client_id=$("#clients").val();
        $('#products > option').remove();
        $.ajax({
                    type: 'POST',
                    url: 'products/load',
                    async: true,
                    cache: false,
                    dataType : 'json',
                    data: 'cid='+client_id,
                    success: function(jsonData){
                        $(jsonData.products).each(function()
                        {
                            $("#products").append($('<option></option>').val(this.id).html(this.item));
                        });                            
                    }
         });
      });
    });
<script>
<form action="<?php url_for('submit/form'); ?>" id="form" method="post" >
   <?php echo $form["clients"]->render(array('id' => 'clients')); ?>
   <?php echo $form["clients"]->renderError(); ?>
   <br/>
   <?php echo $form["products"]->render(array('id' => 'products')); ?>
   <?php echo $form["products"]->renderError(); ?>
   <button type="submit">submit</button>
</form>

The above code sends an Ajax request to the products module with client id, and based on client id does a query and returns the product data for the given client id.

NOTE: Javascript should be enabled.

Hope this helped you. you can also do this the symfony way, please check the symfony docs.

Harish Kurup
  • 7,257
  • 19
  • 65
  • 94