Note that there are some explanatory texts on larger screens.

plurals
  1. POOne dropdownlist depending on the other
    text
    copied!<p>I develop a php web page that contains two dropdownlists (select tags) as one of them used to display the car types like Toyota, Nissan, Chevrolet and so on.</p> <pre><code>Toyota Nissan Chevrolet </code></pre> <p>The other should be used to display the car models like Toyota Camry, Toyota Corrolla, Toyota Cressida, Toyota Eco and son on depeding on the Car Type selected from the first dropdownlist.</p> <p>I use PHP as a development language plus Sybase as database and I connect to it using ODBC. I use Windows-1256 as Character Encoding as my displayed data is Arabic.</p> <p>I try to use Ajax to implement that but I do not know how as I used Ajax before to return one value only but in this case I need to reply with some database records in the following format:-</p> <pre><code>15 "Toyota Camry" 16 "Toyota Corrolla" 17 "Toyota Cressida" 18 "Toyota Eco" </code></pre> <p>plus that the data is sent in arabic not in English as listed above so the list may be as the following:-</p> <pre><code>15 "تويوتا كامري" 16 "تويوتا كرولا" 17 "تويوتا كرسيدا" 18 "تويوتا إيكو" </code></pre> <p>how I can do that using Ajax and make sure that the Arabic text will be displayed well?</p> <p>I wait your answer and help and Thanks in advance .....</p> <p>My Code is written in the following to make things more clear and be useful for others and to get the right answer:</p> <p><em><strong>The first file</em></strong></p> <pre><code>showCarData.php File (Saved as ANSI PHP File) &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=windows-1256" /&gt; &lt;script src="js/jquery-1.3.2.min.js" type="text/javascript" /&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $('#selectCarType').change(function () { $('#selectCarModel option').remove(); $.ajax( { url: "getCarModels.php", dataType: 'json', data: { CarType: $('#selectCarType').val() }, async: true, success: function(result) { //var x = eval(result); $.each(result, function(key, value) { $('#selectCarModel').append('&lt;option value="' + key + '"&gt;' + value + '&lt;/option&gt;'); } ); } }); $('#selectCarModel').show(); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;select id="selectCarType"&gt; &lt;option value="0" selected="selected"&gt;select car type&lt;/option&gt; &lt;?php //Connecting To The Database and getting $conn Variable $conn = odbc_connect("database","username","password"); //Connection Check if (!$conn) { echo "Database Connection Error: " . $conn; } $sqlCarTypes = "SELECT DISTINCT CarTypeID AS CarTypeCode, CarTypeDesc AS CarTypeName FROM tableCarTypes ORDER BY CarTypeDesc ASC"; $rsCarTypes = odbc_exec($conn,$sqlCarTypes); if (!$rsCarTypes) { echo "&lt;option class='CarType' value='n' &gt;Not Available&lt;/option&gt;"; } else { while ( odbc_fetch_row($rsCarTypes) ) { //Assigning The Database Result Set Rows To User-Defined Varaiables $CarTypeCode = odbc_result($rsCarTypes,"CarTypeCode"); $CarTypeName = odbc_result($rsCarTypes,"CarTypeName"); //Creating the options echo '&lt;option class="CarType" style="color:#060;" value="'. $CarTypeCode . '"'; echo '&gt;' . $CarTypeName . '&lt;/option&gt;' . "\n"; } } odbc_close($conn); ?&gt; &lt;/select&gt; &lt;select id="selectCarModel"&gt; &lt;option value="0" selected="selected"&gt;select car model&lt;/option&gt; &lt;/select&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><em><strong>and the other file is</em></strong> </p> <pre><code>getCarModels.php File (Saved as ANSI PHP File) &lt;?PHP //determine the Characterset header('Content-Type: text/html; charset=windows-1256'); //Recieve CarType variable $CarType = $_GET['CarType']; // initialize an array that will hold the rows $result = array(); if ($CarType != Null) { //Connecting To The Database and getting $conn Variable $conn = odbc_connect("database","username","password"); //Connection Check if (!$conn) { echo "Database Connection Error: " . $conn; } $sqlCarModels = "SELECT DISTINCT CarModelID AS CarModelCode, CarModelDesc AS CarModelName FROM tableCarTypes WHERE CarTypeID='$CarType' ORDER BY CarModelDesc ASC "; $rsCarModels = odbc_exec($conn,$sqlCarModels); if ( $rsCarModels ) { while ( odbc_fetch_row($rsCarModels) ) { $CarModelCode = odbc_result($rsCarModels,"CarModelCode"); $CarModelName = odbc_result($rsCarModels,"CarModelName"); $CarModelName = utf8_encode($CarModelName); $result [$CarModelCode] = $CarModelName; } } else { echo "No Data"; } odbc_close($conn); } //send the result in json encoding echo json_encode($result); ?&gt; </code></pre> <p>I hope this clear what I asked about and that any one could help me finding where is the error or the thing I miss to get the output in a proper format instead of the strange symbols and characters that could not be read as it shows in the second dropdown list.</p> <p><em><strong>Thanks in advance</em></strong></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