Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP setting up dependent AJAX dropdown
    primarykey
    data
    text
    <p>I have done a lot of research, tried to apply a few different examples but it seems that nothing really works.</p> <p>So I have the following 3 models: Customers, Projects and Events. Customers have many Projects and Projects have many Events.</p> <p>While creating an Event, I would like a user to select a Customer from a dropdown list and then the user should be provided with a list of Projects that belong to the selected Customer. The closest I have got to is the following. I do not have experience with AJAX, so that really is a hard nut to brake.</p> <p>Action in the Porject's controller:</p> <pre><code> public function getbycustomer(){ $customer_id = $this-&gt;request-&gt;data['Event']['customer_id']; $projects = $this-&gt;Project-&gt;find('list', array('conditions'=&gt;array('Project.customer_id' =&gt; $customer_id), 'recursive' =&gt; -1)); $this-&gt;set('projects', $projects); $this-&gt;layout = 'ajax'; } </code></pre> <p>View for this action is the following:</p> <pre><code> &lt;?php foreach ($projects as $key =&gt; $value): ?&gt; &lt;option value="&lt;?php echo $key; ?&gt;"&gt;&lt;?php echo $value; ?&gt;&lt;/option&gt; &lt;?php endforeach; ?&gt; </code></pre> <p>And here is the snippets from the view for adding an event:</p> <pre><code> echo $this-&gt;Form-&gt;input('customer_id'); echo $this-&gt;Form-&gt;input('project_id'); //form continues and at the end of a page there is the AJAX call $this-&gt;Js-&gt;get('#EventCustomerId')-&gt;event('change', $this-&gt;Js-&gt;request(array( 'controller'=&gt;'projects', 'action'=&gt;'getbycustomer' ), array( 'update'=&gt;'#EventProjectId', 'async' =&gt; true, 'method' =&gt; 'post', 'dataExpression'=&gt;true, 'data'=&gt; $this-&gt;Js-&gt;serializeForm(array( 'isForm' =&gt; true, 'inline' =&gt; true )) )) ); </code></pre> <p>Any help is much much appreciated as I do not even know the proper way for debugging it, so I could provide more valuable information.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload