I'm trying to make my code, test.php, execute some Javascript code if my PHP code, conditional.php, sees that something was inputted then submitted. Instead what my code does is output "Not empty" instead of "Do Something in Javascript". One strange thing I noticed is that lines 26-32 (inside the doSomething() function) in test.php are ignored and yet it prints out "Empty" and "Not empty" in the textbox nonetheless.
The reason I'm doing it this way is because the code in my actual website will need to either use a Javascript API or just PHP depending on the input to generate an output.
test.php
<!DOCTYPE html>
<html>
<body>
<!-- Input -->
<div class="form">
<form action="test.php" method="post">
<input type="text" id="inputText" name="inputText">
<input type="submit">
</form>
</div>
<br>
<!-- Output -->
<div class="txtBox">
<textarea id="txtBox">
<?php require_once "conditional.php";?>
</textarea>
</div>
<script>
function makeRequest() {
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = doSomething() {
// get echoed json from conditional.php
document.getElementById("txtBox").innerHTML = httpRequest.responseText;
/if (httpRequest.responseText == "Not Empty") {
// do my Javascript code
document.getElementById("txtBox").innerHTML = "Do something in Javascript";
}
};
httpRequest.open("GET", conditional.php);
httpRequest.send();
}
</script>
</body>
</html>
conditional.php
<?php
$input = $_POST["inputText"];
if ($input != "") {
echo json_encode("Not empty");
} else {
echo json_encode("Empty");
}
?>
GET, but are accessing the form information viaPOST."GET"method in AJAX and usingPOSTvariable to retrieve it inconditional.php.