0

I have tried solve this code. But unfortunately ajax could not work. I have not figured out the errors. Please help me.

index.html

<form>
<select name="languages" onchange="showLanguage(this.value)">
    <option value="">Select Programming Language</option>
    <option value="1">Python</option>
    <option value="2">Java</option>
    <option value="3">C++</option>
 </select>
</form>
<br>
<div id="languageDetails">Programming Related Info</div>

<script>
function showLanguage(string) 
{
    if (string == "") 
    {
        document.getElementById("languageDetails").innerHTML = "";
        return;
    } 
    else 
    {
        var request = new XMLHttpRequest();
        request.onreadystatechange = function() 
        {
            if (request.readyState == 4 && request.status == 200)
            {
               document.getElementById("languageDetails").innerHTML = 
               request.responseText;
            }
        };
         request.open("GET","languageDetails.php?i="+string,true); 
         request.send();
    }
}
</script>

languageDetails.php

<?php
$i=$_GET["i"];

$conn = mysqli_connect('localhost','root',' ','db');

if (!$conn) 
{
    die('Not connect ' . mysqli_error($conn));
}

$sql="SELECT * FROM language WHERE id = '".$i."'";
$result = mysqli_query($conn,$sql);

echo "<table>
<tr>
<th>Id</th>
<th>Programming Name</th>
<th>Born</th>
</tr>";
while($row = mysqli_fetch_array($result)) 
{
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['born'] . "</td>";
    echo "</tr>";
}
echo "</table>";

mysqli_close($conn);
?>

code.sql

CREATE DATABASE db;

CREATE TABLE IF NOT EXISTS language 
(
  id int(3) NOT NULL AUTO_INCREMENT,
  name varchar(50) NOT NULL,
  born int(4) NOT NULL,
  PRIMARY KEY (id)
)ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;

INSERT INTO language(id, name, born) VALUES
(1, 'Python', 2005),
(2, 'Java', 1995),
(3, 'C++', 1986);
rashedcs
  • 3,588
  • 2
  • 39
  • 40
  • _ajax could not work_ What? How? _I have not figured out the errors_ What errors? – AbraCadaver Jul 18 '19 at 19:13
  • If you could share what error you are getting and where, that would be a starting point. Use the browser console to check for any javascript errors, and the network panel to check what is happening at the network level. – sal Jul 18 '19 at 19:14
  • @sal I have tried at least 3 hours. But code could not work. How can I do sir? I am very wondering if you could help me – rashedcs Jul 18 '19 at 19:16
  • @AbraCadaver , Sir the could not work. – rashedcs Jul 18 '19 at 19:18
  • You should check for [mysqli errors](https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-information-in-different-environments-mysqli-fetch-as) but is your password really a space - `'root',' ','db'` – Nigel Ren Jul 18 '19 at 19:20
  • I deployed your code on a Xampp instance on my Windows computer. The error I got was regarding mysql connection error, due to password. I changed the password used in `languageDetails.php` from *space* `' '` to *empty string* like `''` and it all works as-is. – sal Jul 18 '19 at 23:50

0 Answers0