Set value of an input field, after using AJAX

Dear Sawtooth-Team,

I am creating a questionnaire where the participant has to select an answer out of about 1000 entries. As the entries might also change after some time, I thought that it is a good idea to not solve that problem with several lists, which take time to update, but the following way...

First I created a Free-Format-Question with an input field and a div where the future answers can be shown.
Next there is a script running which queries a database and returns the suggestions, which the user should be able to click on.
So far the script works- the suggestions are shown.

What is not working is the function enterCity. If I assign a predefined value, which should be set into the input field after clicking a suggestion, the function works fine- but somehow I cant pass a variable as a value.

I tried to pass the variable as a parameter from the <p>-tag, I tried to create the variable inside the val()-method and I tried to set the variable before setting it into the input field- nothing worked.

Do you have any ideas, why?

Here is my html- code:


<input type="text" name="Stadt_suche" id="Stadt_suche">
<div class="dropdown-content" id="dropdown-content">

</div>


There you find the scripts:

$('#Stadt_suche').keyup(showCity);

function showCity() {
var str = SSI_GetValue('Stadt_suche');

if (str.length <= 2) {
document.getElementById("dropdown-content").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("dropdown-content").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "getcity.php?q=" + str + "%", true);
xmlhttp.send();
}
}

function enterCity(){
var city = $(this).text();
$("#Stadt_suche").val(city);
SSI_SubmitMe();
}



And finaly, in case its needed, the database query:

<?php

$mysqli = new mysqli("host", "user", "password", "database");
if($mysqli->connect_error) {
exit('Could not connect');
}

$sql = "SELECT DISTINCT `Ort` FROM `HaendlernetzGesamt` WHERE `Ort` Like ?";

$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $_GET['q']);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($ort);

if($stmt->num_rows > 0){
while ($stmt->fetch()){
echo "<p id='" . $ort . "' onclick='enterCity();' >". $ort ."</p>";
}
}
?>


Best regards!

Resolved
1 reply