Note that there are some explanatory texts on larger screens.

plurals
  1. POSweating and screaming because of a cascading drop down
    primarykey
    data
    text
    <p>After searching for an answer for about five days for a solution to my problem, I figured I might as well ask for some help. I am working on a cascading drop down that I can´t seem to get to work.</p> <p>The Mysql data are saved in one table where we have country (varchar) and regions (varchar), names of document (varchar), links to documents (varchar) as well as date added (timestamp). </p> <p>I want the first dropdown to lead to the second drop down that contains the regions. I looked at many tutorials but I can´t seem to find one that uses either PDO or Mysqli, that works. When the form is submitted I want the name of the document to appear as hyperlink and the date added.</p> <p>I got started using: <a href="http://www.electrictoolbox.com/json-data-jquery-php-mysql/" rel="nofollow">http://www.electrictoolbox.com/json-data-jquery-php-mysql/</a></p> <p>This is my code:</p> <p>"Drop down site"</p> <pre><code> &lt;form&gt; &lt;select name="country" id="countryname"&gt; &lt;option&gt;Sverige&lt;/option&gt; &lt;option&gt;Norge&lt;/option&gt; &lt;option&gt;Danmark&lt;/option&gt; &lt;option&gt;Finland&lt;/option&gt; &lt;/select&gt; &lt;select name="region" id="regionname"&gt; &lt;/select&gt; &lt;/form&gt; </code></pre> <p>Javascript (jQuery)</p> <pre><code>function popregion() { $.getJSON('/blanketter_func2.php', {countryname:$('#countryname').val()}, function(data) { var select = $('#regionname'); var options = select.prop('options'); $('option', select).remove(); $.each(data, function(index, array) { options[options.length] = new Option(array['region']); }); }); } $(document).ready(function() { popregion(); $('#countryname').change(function() { popregion(); }); }); </code></pre> <p>The function file (blanketed_func2.php).</p> <pre><code>&lt;?php $dsn = "mysql:host=localhost;dbname=blanketter"; $username = "root"; $password = "root"; $pdo = new PDO($dsn, $username, $password); $rows = array(); if(isset($_GET['countryname'])) { $stmt = $pdo-&gt;prepare("SELECT region FROM blanketter WHERE country = ? ORDER BY region"); $stmt-&gt;execute(array($_GET['countryname'])); $rows = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC); } echo json_encode($rows); ?&gt; </code></pre> <p>I am using jQuery version 1.9.1 and any help would be appreciated!</p> <p>/Sten</p>
    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