将输入变量传入URL字符串时出错:Undefined Index问题求助
解决Undefined Index错误
报错原因:页面首次加载时,URL里没有
os0这个GET参数,直接调用$_GET["os0"]必然触发Undefined Index错误。而且你这表单用的是POST提交方式,却在action的URL里拼接GET参数,逻辑也绕了——用户输入的os0是POST字段,并非页面初始加载时的GET参数。修复方案:
- 先判断
os0参数是否存在再输出,避免报错:<form action="purchase-checkout.php<?php echo isset($_GET["os0"]) ? '?MFLHistoryID=' . $_GET["os0"] : ''; ?>" method="POST"> - 更合理的做法:既然用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> - 额外提醒:为避免XSS风险,输出参数时建议用
htmlspecialchars()做过滤:<input type="hidden" name="MFLHistoryID" value="<?php echo htmlspecialchars($_GET["os0"]); ?>">
- 先判断
内容的提问来源于stack exchange,提问作者Kevin
相关产品推荐
相关产品推荐

