IT-info > AJAX

AJAX

AJAX GET (PHP)

text.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Text</title>
<script type="text/javascript">
function saveTxt(){
var str = document.getElementById("inputbox").value;

    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("demo").innerHTML = this.responseText;
        }
    };

    xhttp.open("GET", "processtext.php?sometxt=" + str, true);
    xhttp.send();
}
</script>
</head>
<body>
<form> First name: <input id="inputbox" type="text"> </form> <button onclick="saveTxt();">Run script</button>
<div id="demo"></div>
</body>
</html>

processtext.php:

<?php
if($_REQUEST["sometxt"]){
    $exampletext = $_REQUEST["sometxt"];
    echo "The text: ".$exampletext;
}
?>