Ajax从服务器接收响应并将其保存在变量中以供以后的PhP使用

I have an issue, that I cannot solve. I am rather new to Ajax and PhP so I will appreciate any help that I can get.

I have a database on my localhost. What I try to achieve is to get a response from server with information from database. I want to store that information in a variable, so I can later "echo" in certain place.

The whole idea behind this is, that:

You enter product number in an input field. Request is being sent to database. Once you receive response from database, another input field below product number is automatically filled up with a product name. You click on the next step and once again you see the input fields, that are filled up with rest of information based on what product number you entered earlier.

So far the issue is that I can get immediate response once everything is sent, but if I want to see only one piece of information and later "echo" everything else out, but I can't figure how to do that.

I hope that I did manage to explain what is my problem, if not I will try to add more information about it.

This is my Ajax code. I am don't know how to store information that I get from database, so I can use it later on.

<script>
function showPump(str) {
    if (str == "0") {
        document.getElementById("pumpInfo").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("pumpInfo").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","getpump.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>

Part of HTML:

<form>
    Product number:<input type="text" name="productNR" onfocusout="showPump(this.value)">
</form>
<br>
<div id="pumpInfo"><b>Pump info will be below</b></div>

PHP

mysqli_select_db($con,"pumps");
$sql="SELECT * FROM pumps WHERE product_nr = '".$q."'";
$result = mysqli_query($con,$sql);

while($row = mysqli_fetch_array($result)) {
    echo "Product type <input value=" . $row['type'] . "></br>";
}
mysqli_close($con);
?>

End result is that once I fill up the product number field, "result"(Product type) is being displayed right under it, but I have 4 more database entries that I wish to use on next step.

Here's one way to store values that you receive via AJAX.

//results will be an array
var results;

if (this.readyState == 4 && this.status == 200) {
  document.getElementById("pumpInfo").innerHTML = this.responseText;
  results.push(this.responseText); //push the value of this.reponseText to the array.
}

//this pops the value of the first element of the results array
alert(results[0]);