You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

同页面AJAX发送的POST请求无法被PHP捕获的问题求助

Fixing Your PHP & AJAX Code Issues

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:

  1. Undefined $i Variable
    Your button's onclick calls Data(<?=$i?>), but there's no definition for $i in your code. This will either throw a PHP error or pass an undefined value to your JS function, breaking the AJAX call. Make sure $i is properly initialized before using it, e.g.:

    <?php $i = 123; // Replace with your actual value ?>
    <input type="button" class="button" onclick="Data(<?=$i?>)"/>
    
  2. Missing AJAX url Parameter
    Your $.ajax call doesn't specify where to send the request—jQuery has no idea which PHP file to hit. Add a url parameter, 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 page
    
  3. Incorrect cache Value
    The cache option in jQuery AJAX expects a boolean (true/false), not a string "false". Using the string will be treated as true, which isn't what you want. Fix it to:

    cache: false,
    
  4. 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);
    }
    
  5. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:16:17