Jump to content

PHP - AJAX and MySQL


silviu60
 Share

Recommended Posts

Good evening , I have a problem that gives me headaches ... I saw in your School Site (W3SCHOOLS) section PHP AJAX example that people take from the database . I tried to put the same id " Peter " two other people like an array and don't work.
Ex: when select Peter Griffin appear let's say his family or wife .. any ideea or clue ?
Thanks alot. Have a great day.
HTML
<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="">Select a person:</option>
  <option value="1">Peter Griffin</option>
  <option value="2">Lois Griffin</option>
  <option value="3">Joseph Swanson</option>
  <option value="4">Glenn Quagmire</option>
  </select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>

</body>
</html>

PHP FILE

<!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','peter','abc123','my_db');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

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

echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['FirstName'] . "</td>";
    echo "<td>" . $row['LastName'] . "</td>";
    echo "<td>" . $row['Age'] . "</td>";
    echo "<td>" . $row['Hometown'] . "</td>";
    echo "<td>" . $row['Job'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
Edited by silviu60
Link to comment
Share on other sites

hello again,

 

I wish that when I select for exemple the 1st person Peter Griffin to appear details about other people who might be in his family

 

1st select:

id Fist Name Last Name AGE Hometown Job

 

1 Peter Griffin 41 Quahog Brewery

1 Gina Griffin 20 Quahog Manager

1 Alex Griffin 1 Quahog -

 

 

 

 

2nd select:

id Fist Name Last Name AGE Hometown Job

 

2 Joseph Swanson 20 London Driver

2 Alexandra Swanson 18 London Maid

 

 

somthing like this

 

 

ex: Select: Peter Griffin - Gina Griffin

-Alex Griffin

 

Select: Joseph Swanson: -Alexandra Swanson

-Gigel Swanson

 

Etc.

 

Thanks for replay.

Edited by silviu60
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...