Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are trying to initialize the jQuery plugin on an element that does not exist yet. I think it is important to understand what is going wrong, so you can avoid this problem in the future, since this is a very common pattern you're using.</p> <p>We'll look at your code, starting with this bit...</p> <pre><code> $(function() { $( "#accordion" ).accordion(); }); </code></pre> <p>...which is executed immediately, as the page loads. Since there is no element with the id <code>accordion</code> at that moment, nothing happens (check your console -- probably have an error).</p> <p>Then, you have the <code>onchange</code> code on the input element, which uses non-jQuery ajax to send a request on to your php file. The php file generates some html, wrapped in a div with the id <code>accordion</code>, and adds it to the page. That element has never had the accordion-related javascript applied to it, and thus sits there with no functionality.</p> <p>You've got some of the concepts right, but your timing is wrong. First of all, let's use the jQuery ajax method to simply that code. Also, since you're already paying a performance/load time cost to include jQuery in your project, you should take full advantage of the functionality and use the utility element selector as well:</p> <p><strong>The new javascript</strong></p> <pre><code>(function ($) { // used to keep a reference to the ajax request object var searchQuery = false; var showUser = function (str) { if (searchQuery != false) // if there is a pending request, cancel it searchQuery.abort(); var searchTerm = $('#matr').val(); if (searchTerm .trim().length &lt; 1) { $("#txtHint").html(''); return; } // clean up the old accordion if ($("#accordion").length &gt; 0) $("#accordion").accordion('destroy'); $("#txtHint").html('seaching...'); searchQuery = $.ajax({ 'url':'http://IP/file_name/test.php?q='+searchTerm, 'success':function (response) { searchQuery = false; $('#txtHint').html(response); $('#accordion').accordion(); }, 'error': function () { searchQuery = false; $('#txtHint').html('Sorry, there was an error'); } }); }; // add the change event to the text field, once the page loads $(document).ready(function () { $('#matr').change(showUser); }); })(jQuery); </code></pre> <p><strong>The HTML</strong></p> <p>The only change required to the HTML is that you remove the inline <code>onClick</code> event from the input. That said, you are using <code>center</code> to center text, which is not a good practice. Use css <code>text-align:center;</code> instead -- the less tags, the better (generally speaking).</p> <p><strong>The PHP</strong></p> <p>Nothing needs to change here, but be certain you are correctly treating the user input that you're getting from <code>$_GET</code>. Using <code>mysqli_*</code> alone does not protect against SQL injection, if you don't use it correctly. We don't see that code here, so consider this a standard disclaimer: <strong>never trust user input</strong>!</p> <p><strong>Documentation</strong></p> <ul> <li><code>jQuery.ajax</code> - <a href="http://api.jquery.com/jQuery.ajax/" rel="nofollow">http://api.jquery.com/jQuery.ajax/</a></li> <li>jQuery UI Accordion widget - <a href="http://jqueryui.com/accordion/" rel="nofollow">http://jqueryui.com/accordion/</a></li> </ul>
    singulars
    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