sâmbătă, 27 februarie 2016

PHP cu mySQL si Javascript - 2

index.php

<!DOCTYPE html>
<html>
<head>
<script>
function showUser(str) {
  if (str=="") {
    document.getElementById("txtHint").innerHTML="";
    return;
  }
  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="nume" onchange="showUser(this.value)">
<option value="">Selectati persoana:</option>
<option value="1">Popescu</option>
<option value="2">Florescu</option>
<option value="3">Ionescu</option>

<option value="4">nimereala</option>
<option value="5">altul</option></select>
</form>
<br>
<div id="txtHint"><b>Persoanele restante la intretinere vor fi afisate mai jos.</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']);

$con = mysqli_connect('localhost','central5_vali','parola123','central5_vali');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM bloc WHERE ap = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Nume</th>
<th>Prenume</th>
<th>Apa</th>
<th>Pers in loc</th>
<th>AP</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['nume'] . "</td>";
    echo "<td>" . $row['prenume'] . "</td>";
    echo "<td>" . $row['apa'] . "</td>";
    echo "<td>" . $row['pers'] . "</td>";
    echo "<td>" . $row['ap'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>

Niciun comentariu:

Trimiteți un comentariu