I've seen this many times before on Stack Overflow and other websites, but none of the previous posts seem to be helping.
Assume the following HTML code:
<input maxlength="50" size="50" id="searchTxt" />
And assume the following AJAX call:
$.ajax({
type: "POST",
url: "tdat.php",
data: {userresponse: $('#searchTxt').val()},
success: function(){
$('#searchTxt').val("")
}
})
What should happen is that the data "userresponse" gets posted to the server, having the value of whatever is inside the input box, and then the input in the box is emptied.
Later, I have the following PHP code in tdat.php:
<?php
if(isset($_POST['userresponse'])){
$userResponsePHP = $_POST['userresponse'];
switch($userResponsePHP){
case "yes":
echo "alert(\"hi\")";
break;
case "no":
echo "alert(\"negative!\")";
break;
default:
echo "alert(\"nope.\")";
break;
}
}
?>
The PHP does not respond, even if the AJAX call works perfectly. PHP does not even receive the value of "userresponse". I have checked the file paths, localhost connection, adding quotes to "userresponse", and practically all other minor issues stated in other threads, but the problem persists. The AJAX call clears the value of searchTxt, thus it is a successful send. No errors are shown in the console. Why is this so?
isset($_POST['userresponse'])use!empty($_POST['userresponse'])