1
votes

I have a form with select input. I want to auto submit the form when the dropdown list was selected.

My code:

<?php echo $this->Form->create('Product', array('controller'=>'products', 'action'=>'shipping_area'));
    echo $this->Form->input('area', array('options' => array('1' => 'Within Malaysia', '2' => 'International'), 'empty' => 'choose area',
    'label' => 'Choose shipping area', 'onChange'=>'javascript:this.form.submit()'));
    //echo $this->Form->end('Save');
?>

I put 'onChange'=>'javascript:this.form.submit()', but it goes to http://localhost/cake/cake/products/shipping_area ( supposely http://localhost/cake/products/shipping_area )

I also tried 'onChange'=>'this.form.submit()', but got same error.

can anyone please help.

2
If you look at the generated html, what is the path on the form element ?Dimitar Dimitrov
I put this line: return $this->redirect($this->request->here); i want it to redirect to the same page after submitting the form.dancingAngel
Try to use document.referrer: w3schools.com/jsref/prop_doc_referrer.aspkicaj

2 Answers

1
votes

You can add an "id" attribute to the form and then every time that you get an "onchange" event in the "select" element, you have to obtain the "id" value and pass it to the javascript function "document.getElement('idValueHere')" and call to the fuction submit. More clearly, the following code:

<?php 
# step 1: add an id attribute ('id' => 'anyFormName') to the array options

# step 2: add an onchange envent to the dropdown ('onChange'=>'document.getElementById("anyFormName").submit();')

echo $this->Form->create('Product', array('controller'=>'products', 'action'=>'shipping_area', 'id' => 'anyFormName'));
echo $this->Form->input('area', array('options' => array('1' => 'Within Malaysia', '2' => 'International'), 'empty' => 'choose area',
    'label' => 'Choose shipping area', 'onChange'=>'document.getElementById("anyFormName").submit();'));
//echo $this->Form->end('Save');?>
0
votes

Hope it helps you.

echo $this->Form->create('Product', array(
    'url' => array(
        'controller'=>'products', 'action'=>'shipping_area'
    )
));