I am using xmlhttp request to send data to my php file and then use it to get data out of a database and then show that response back at my original page. Following is the code:
<script>
function func2(str) {
if (str == "") {
document.getElementById("replaceContent").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("replaceContent").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","projectdata.php?q="+str,true);
xmlhttp.send();
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<div id="replaceContent">
This should be replaced.
<button onclick="func2(10)"> Click ME </button>
</div>
</body>
</html>
and the following is the projectData.php:
<?php
include 'connEst.php';
// get the q parameter from URL
$q = $_GET['id'];
echo $q;
$sql="SELECT * FROM projects WHERE ID = '".$q."'";
$result = mysqli_query($conn,$sql);
echo "<table>
<tr>
<th> $q </th>
</tr>
<tr>
<th>Project Name</th>
<th>Project Location</th>
<th>Project Value</th>
<th>Client Name</th>
<th>Work Done in Project</th>
<th>Project Summary</th>
<th>Start Date</th>
<th>End Date</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['p_name'] . "</td>";
echo "<td>" . $row['p_loc'] . "</td>";
echo "<td>" . $row['p_val'] . "</td>";
echo "<td>" . $row['client_name'] . "</td>";
echo "<td>" . $row['p_wdone'] . "</td>";
echo "<td>" . $row['p_summary'] . "</td>";
echo "<td>" . $row['start_date'] . "</td>";
echo "<td>" . $row['end_date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
I am running it on my mac using an apache server installation. I am testing this in Chrome. The $q variable I have setup is returning a blank value when I run the file.
please help!
</div>