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

将输入变量传入URL字符串时出错:Undefined Index问题求助

解决Undefined Index错误
  • 报错原因:页面首次加载时,URL里没有os0这个GET参数,直接调用$_GET["os0"]必然触发Undefined Index错误。而且你这表单用的是POST提交方式,却在action的URL里拼接GET参数,逻辑也绕了——用户输入的os0是POST字段,并非页面初始加载时的GET参数。

  • 修复方案:

    1. 先判断os0参数是否存在再输出,避免报错:
      <form action="purchase-checkout.php<?php echo isset($_GET["os0"]) ? '?MFLHistoryID=' . $_GET["os0"] : ''; ?>" method="POST">
      
    2. 更合理的做法:既然用POST提交,把MFLHistoryID做成隐藏字段传递,这样页面首次加载也不会报错:
      <form action="purchase-checkout.php" method="POST">
          <?php if(isset($_GET["os0"])): ?>
              <input type="hidden" name="MFLHistoryID" value="<?php echo $_GET["os0"]; ?>">
          <?php endif; ?>
          <p>
              <input name="os0" id="os0" maxlength="5" type="text">
          </p>    
          <p>
              <button type="submit" id="checkout-button">Purchase now</button>
          </p>
      </form>
      
    3. 额外提醒:为避免XSS风险,输出参数时建议用htmlspecialchars()做过滤:
      <input type="hidden" name="MFLHistoryID" value="<?php echo htmlspecialchars($_GET["os0"]); ?>">
      

内容的提问来源于stack exchange,提问作者Kevin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 13:06:17