Forum Moderators: coopster
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
...
echo "
<form>
<select name='users' onchange='showUser(this.value)'>
<option value=''>Select a person:</option>
";
$sql = "select *
from companies
order by company_name";
$result = query($sql,$conn) or die(mysql_error());
while($newArray = fetch_array($result)){
$company_id = $newArray['company_id'];
$company_name = stripslashes($newArray['company_name']);
echo "<option value='$company_id'>$company_name</option>";
}
echo "
</select>
</form>
";
$sql="SELECT * FROM companies WHERE company_id = '".$q."'";
$result = query($sql,$conn) or die(mysql_error());
echo "<table>
<tr>
<th>Company</th>
<th>State</th>
</tr>";
// while($newArray = fetch_array($result)){
while($row = fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['company_name'] . "</td>";
echo "<td>" . $row['company_state'] . "</td>";
echo "</tr>";
}
echo "</table>";
$new_name = "aaaa_". $company_name;
$sql="UPDATE pbs_companies SET company_name = '$new_name' WHERE company_id = '".$q."'";
echo "SQL: $sql<br/>";
$result = query($sql,$conn) or die(mysql_error());
I have a php file which I now have passing a variable to an Ajax script
const mySelect = document.querySelector('#mySelect');
mySelect.addEventListener('change', function(event) {
const selection = event.target.value;
const user = document.querySelector('#someDiv').getAttribute('data-user');
const other = document.querySelector('#otherDiv').id;
const formData = new FormData;
formData.append('selection', selection);
formData.append('user', user);
formData.append('otherInfo', other);
// AJAX POST request here...
// note: I use es6 const syntax, this can be replaced with var.
}