同页面AJAX发送的POST请求无法被PHP捕获的问题求助
Hey there! Let's break down your code and fix the issues that are probably causing it to fail. First, let's look at your original code formatted for clarity:
<?php if(isset($_POST['sInput'])){ $sInput=($_POST['sInput']); echo "$sInput"; }else{ echo '1'; } ?> <input type="button" class="button" onclick="Data(<?=$i?>)"/> <script type="text/javascript"> function Data(sInput){ $.ajax({ type:"POST", data:{sInput:sInput}, cache:"false", success: function() { alert('Done'); } }); } </script>
Key Issues & Fixes
Let's go through the problems one by one:
Undefined
$iVariable
Your button'sonclickcallsData(<?=$i?>), but there's no definition for$iin your code. This will either throw a PHP error or pass an undefined value to your JS function, breaking the AJAX call. Make sure$iis properly initialized before using it, e.g.:<?php $i = 123; // Replace with your actual value ?> <input type="button" class="button" onclick="Data(<?=$i?>)"/>Missing AJAX
urlParameter
Your$.ajaxcall doesn't specify where to send the request—jQuery has no idea which PHP file to hit. Add aurlparameter, pointing to the file that handles your POST request (this can be the same page if you're using inline PHP):url: window.location.href, // Sends request to the current pageIncorrect
cacheValue
Thecacheoption in jQuery AJAX expects a boolean (true/false), not a string"false". Using the string will be treated astrue, which isn't what you want. Fix it to:cache: false,No Error Handling & Poor Response Feedback
Your success callback only alerts "Done" but doesn't check what the server returned. Add an error callback to catch failures, and use the server response in your success alert to debug:success: function(response) { alert('Done! Server responded with: ' + response); }, error: function(xhr, status, error) { alert('Oops, an error occurred: ' + error); }XSS Vulnerability & Unclean Response
Directly echoing$_POST['sInput']without escaping exposes you to cross-site scripting (XSS) attacks. Also, your PHP code will output the HTML button and script along with the response unless you exit after echoing. Fix the PHP section:<?php if(isset($_POST['sInput'])){ // Escape output to prevent XSS $sInput = htmlspecialchars($_POST['sInput'], ENT_QUOTES); echo $sInput; exit; // Stop execution here to avoid sending extra HTML } // Only show the button if it's not an AJAX request ?>
Full Fixed Code
Here's the complete, working version of your code with all fixes applied:
<?php if(isset($_POST['sInput'])){ $sInput = htmlspecialchars($_POST['sInput'], ENT_QUOTES); echo $sInput; exit; } ?> <?php $i = 123; // Replace with your actual value ?> <input type="button" class="button" onclick="Data(<?=$i?>)"/> <script type="text/javascript"> function Data(sInput){ $.ajax({ type: "POST", url: window.location.href, data: {sInput: sInput}, cache: false, success: function(response) { alert('Done! Server responded with: ' + response); }, error: function(xhr, status, error) { alert('Oops, an error occurred: ' + error); } }); } </script>
内容的提问来源于stack exchange,提问作者LadyJava

