Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update a <div> with jQuery and CakePHP?
    text
    copied!<p>I'm using CakePHP for a small web app and on one form page there's a dropdown to select a job number. I'd like to update two text fields based on the job number selected in the dropdown using jQuery (I'm also open to using the default ajax helper but I didn't have a lot of success with it). </p> <p>Here's my jQuery snippet:</p> <pre><code>&lt;script&gt; $(document).ready(function() { $('#job_id').change(function() { $.post('/surveys/jobdetails', {id: $(this).attr('id')}); }) .change(); }); &lt;/script&gt; </code></pre> <p><code>jobdetails</code> is a method in my controller that gets the current job based on the job id passed in. However, it doesn't get called when the dropdown changes value. I tried substituting an alert function in place of .post and that worked, so onchange is being called correctly.</p> <p>Here's the <code>&lt;div&gt;</code> I'm trying to update:</p> <pre><code>echo "&lt;div id='job_details'&gt;"; echo $form-&gt;label('jobtitle', 'Job Title'); echo "&lt;input type='text' name='jobtitle' id='jobtitle'&gt;"; echo $form-&gt;label('department', 'Department'); echo "&lt;input type='text' name='department' id='department'&gt;"; echo "&lt;/div&gt;"; </code></pre> <p>I want to set the value of each text field to be the corresponding value for the job returned from the ajax call. There's a lot of really good jQuery and CakePHP documentation but I haven't found anything that quite covers what I'm trying to do. Can anyone see what I'm doing wrong? Is there a better way to use ajax to update a div with CakePHP?</p>
 

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