I am trying to fill in a form field with a certain value from my DB whenever a related dropdown changes and I don't understand why:
- I can successfully use the js()->val() of my dropdown to set the val() of the field I'm trying to populate ($price)
- but whenever I use the js()->val() in my query or in the alert I added for troubleshooting purposes, it outputs the field name reference string with '.val()' appended or something similar...
I know it's a noob question but I've looked at this several ways and tried different ways and I can't get it to work. If I hardcode a value for 'id' in the query below it does work as expected, I just need to be able to pass the query the appropriate value...
$form = $this->add('Form');
$product = $form->addField('dropdown', 'name');
$product->setModel('Product');
$price = $form->addField('line', 'price_per');
$product->js('change', $price->js()
->val($product->js()->val())
->univ()
->alert('ID is : ' . $product->js()->val()));
// $product->js('change', $price->js()->val(
// $this->api->db->dsql() //->expr(2+2)
// ->debug()
// ->table('product')
// ->field('price_per')
// ->where('id',$product->js()->val())
// ->getOne()
// ));
Your problem is here:
you must remember that js() produces a PHP chain which can then be converted into javascript. If you concatenate it with string, then it becomes a string and hence the alert. Try this:
it should work fine.
You can add method in the UNIV chain with necessary concatenation. You can also use js(null,' 2+4') to inject any javascript code, but be careful as it is not escaped.
AJAX
This is a technique when javascript relies on server-side algorithm. JavaScript sends request to the backend. Agile Toolkit uses a technique called AJAXEC (Ajax + Exec). It will send request to the server, but server will respond with chunk of javascript for it to execute (hence the ->execute() method)
There are several ways to wrap this functionality with Agile Toolkit but you should probably look at the basics first: