You are on page 1of 6

09.

Seminar AJAX
Ushtrim 1

Jepet file ajax_info.txt me tekstin “Pershendetje, kjo eshte nje prove ne AJAX”.
Te ndertohet programi duke perdorur Ajax qe ne klikim te nje butoni afishon tekstin qe gjen tek file
ajax_info.txt.

Ajax_example1.php
<html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Kliko per te ndryshuar tekstin</h2></div>


<button type="button" onclick="loadXMLDoc()">Testo njohuri AJAX</button>

</body>
</html>
Ushtrim 2
Te ndertohet programi duke perdorur Ajax qe jep sugjerime emri nderkohe qe shkruhet ne nje fushe
tekst, ku sugjerimet e emrave te merren nga nje vector ne file getsuggest.php.

Kodi qe ben kerkimin e sugjerimeve ne baze te fjaleve te shkruajtura ne file getsuggest.php jepet me
poshte:

<?php
// Array with names
$a=array("Ana","Belina","Denisa","Diana","Eva","Fiona","Greta","Helena","Ina","Johana","Kleo","Lin
da","Nina","Orjon","Petrit","Amanda");

// get the q parameter from URL


$q = $_REQUEST["q"];

$hint = "";

// lookup all hints from array if $q is different from ""


if ($q !== "") {
$q = strtolower($q);
$len=strlen($q);
foreach($a as $name) {
if (stristr($q, substr($name, 0, $len))) {
if ($hint === "") {
$hint = $name;
} else {
$hint .= ", $name";
}
}
}
}
// Output "no suggestion" if no hint was found or output correct values
echo $hint === "" ? "no suggestion" : $hint;
?>
Ajax_example2.php

<html>
<head>
<script>
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "getsuggest.php?q=" + str, true);
xmlhttp.send();
}
}
</script>
</head>
<body>

<p><b>Shkruani nje emer ne fushen tekst:</b></p>


<form>
Emri: <input type="text" onkeyup="showHint(this.value)">
</form>
<p>Sugjerime: <span id="txtHint"></span></p>
</body>
</html>

Ushtrim 3

Te ndertohet programi duke perdorur Ajax qe afishon te dhena per nje person ne nje tabele. Te
dhenat te merret nga tabela personel_tb ne database ajax_db.
Te zgjidhet emri nga nje liste dhe me pas te afishohen te dhenat.

Query te meposhtme ndertojne dhe mbushin tabelen me vlera:

CREATE TABLE `personel_tb` (


`id` tinyint(4) NOT NULL auto_increment,
`emri` varchar(14) NOT NULL default '',
`mbiemri` varchar(15) NOT NULL default '',
`mosha` tinyint(4) NOT NULL default '0',
`vendlindja` varchar(20) NOT NULL default '',
`profesioni` varchar(30) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=5 ;

INSERT INTO `personel_tb` (`id`, `emri`, `mbiemri`, `mosha`, `vendlindja`, `profesioni`) VALUES (1, 'Anila',
'Hysa', 26, 'Tirane', 'mesuese');
INSERT INTO `personel_tb` (`id`, `emri`, `mbiemri`, `mosha`, `vendlindja`, `profesioni`) VALUES (2, 'Enea',
'Godea', 35, 'Lushnje', 'Inxhinier');
INSERT INTO `personel_tb` (`id`, `emri`, `mbiemri`, `mosha`, `vendlindja`, `profesioni`) VALUES (3,
'Artemisa', 'Koleka', 42, 'Permet', 'Mjeke');
INSERT INTO `personel_tb` (`id`, `emri`, `mbiemri`, `mosha`, `vendlindja`, `profesioni`) VALUES (4, 'Luan',
'Mano', 51, 'Shkoder', 'Poet');

Ajax_example3.php
<html>
<head>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Zgjidhni nje nga emrat:</option>
<option value="1">Anila Hysa</option>
<option value="2">Enea Godea</option>
<option value="3">Artemisa Koleka</option>
<option value="4">Luan Mano</option>
</select>
</form>
<br>
<div id="txtHint"><b>Te dhenat per personin jane si me poshte...</b></div>

</body>
</html>

Getuser.php

<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}

table, td, th {
border: 1px solid black;
padding: 5px;
}

th {text-align: left;}
</style>
</head>
<body>

<?php
$q = intval($_GET['q']);
$db_host = 'localhost';
$db_name = 'ajax_db';
$db_user = 'root';
$db_pass = 'Paw2020';

if(!$con = mysqli_connect($db_host, $db_user, $db_pass))

die( "Lidhja me bazen e te dhenave nuk mund te kryhet." .mysql_connect_error().


"</body></html>" );
if ( !mysqli_select_db( $con,$db_name ) )

die( "Baza e te dhenave nuk mund te hapet:".mysql_error(). "</body></html>");

$sql="SELECT * FROM personel_tb WHERE id = '".$q."'";


$result = mysqli_query($con, $sql);

echo "<table>
<tr>
<th>Emri</th>
<th>Mbiemri</th>
<th>Mosha</th>
<th>Vendlindja</th>
<th>Profesioni</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['emri'] . "</td>";
echo "<td>" . $row['mbiemri'] . "</td>";
echo "<td>" . $row['mosha'] . "</td>";
echo "<td>" . $row['vendlindja'] . "</td>";
echo "<td>" . $row['profesioni'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>

You might also like