help symfony 1.4
In a form, when I am selecting the name of the client from a drop down list, next line开发者_如何学C 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,
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.
精彩评论