Ajax请求持续返回false:简单验证程序技术求助
Hey there, let's walk through the common issues that might be causing your Ajax request to keep returning false, and fix them one by one.
First, let's recap your code for clarity:
Frontend Code
$("#codeSbmt").click(function(){ var $input = $("#fourDigit"); codeCheck("codeTest.php?q=" + $input); }) function codeCheck(url) { var xhttp; xhttp=new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("message").innerHTML = this.responseText } }; xhttp.open("GET", url, true); xhttp.send(); }
Backend Code Snippet
$q = $_REQUEST['q'...
1. Critical Frontend Issue: Passing the jQuery Object Instead of Input Value
The biggest red flag here is this line:
codeCheck("codeTest.php?q=" + $input);
$input is a jQuery object ($("#fourDigit")), not the actual value of the input field. When you concatenate an object to a string, it becomes [object Object]—so your backend is receiving q=[object Object] instead of the four-digit code the user entered. That's almost certainly why your validation is failing.
Fix this by using .val() to get the input's value:
codeCheck("codeTest.php?q=" + $input.val());
2. Verify HTML Element IDs Match
Double-check that your HTML has elements with exactly these IDs:
codeSbmt(the submit button)fourDigit(the input field for the code)message(the element where you display the response)
A tiny typo here (likefourdigitinstead offourDigit) would break the whole flow.
3. Backend Troubleshooting
Your backend code is incomplete, but let's cover common issues here:
- Complete the parameter fetch: Make sure you're correctly getting the
qparameter. Fix the syntax first:$q = $_REQUEST['q']; // Don't forget the closing bracket and semicolon - Check validation logic: If your backend returns
falsewhen the code is invalid, confirm that:- The input
$qis not empty - It matches your expected format (e.g., exactly 4 digits—use
ctype_digit($q) && strlen($q) === 4to validate)
- The input
- Enable PHP error reporting: Add these lines at the top of
codeTest.phpto catch syntax or runtime errors that might be silently failing:error_reporting(E_ALL); ini_set('display_errors', 1); - Check URL path: Ensure
codeTest.phpis in the same directory as your frontend page, or use an absolute path (e.g.,/your-folder/codeTest.php) if it's in a different location. A wrong path would cause a 404 error, which your current code doesn't handle.
4. Optional: Improve Error Handling
Right now your code only handles successful 200 responses. Add a check for errors to debug easier:
xhttp.onreadystatechange = function() { if (this.readyState == 4) { if (this.status == 200) { document.getElementById("message").innerHTML = this.responseText; } else { document.getElementById("message").innerHTML = "Error: Request failed with status " + this.status; } } };
Try these fixes one by one—most likely the input value issue is the main culprit. Let me know if you're still having trouble after checking these!
内容的提问来源于stack exchange,提问作者Hamza Shakil

