I am having trouble getting this to work. I have been able to call data from a dropdown menu and place it into a table and have it actively update without reloading the page. I am now trying to get the database information that is called to appear within a text input field or another drop down menu.
So basically I have a drop down menu that will call up user information, I am trying to get that information that is called to appear within another form so I can update it. Here is the code I am working with;
table7.php
<html>
<head>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("txtDisp").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("txtDisp").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser2.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form method="post" action="localhost/table7.php">
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<?php
$con=mysqli_connect("localhost","user","password","database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM users");
while($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['id'] . "'>" . $row['uname'] . "</option>";
mysqli_close($con);
}
?>
</select>
</form>
<br>
<div id="txtDisp"><b>Person info will be listed here.</b></div>
</body>
</html>
getuser2.php
<?php
$q = intval($_GET['q']);
$con=mysqli_connect("localhost","user","password","database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT * FROM users WHERE id = '".$q."'";
$result1 = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Username</th>
<th>E-Mail</th>
<th>Info 1</th>
</tr>";
echo "<form action="getuser2.php" method="post">";
while($row1 = mysqli_fetch_array($result1)) {
echo "<tr>";
echo "<td><input type="text" name="info1" value=" . $row1['uname'] . "></td>";
echo "<td>" . $row1['email'] . "</td>";
echo "<td>" . $row1['info1'] . "</td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
mysqli_close($con);
?>
Within the getuser2.php code if you strip out the form input section and replace it with a call for uname using the format directly below for email and info it will display the data called from the database in standard text format.
However, I am encountering this error:
Parse error: syntax error, unexpected 'text' (T_STRING), expecting ',' or ';' in C:\wamp\www\getuser2.php on line 25