如何在提交GET表单时排除submit参数(HTML+PHP场景)
解决GET表单提交时URL携带submit参数的问题
核心问题是你依赖submit参数判断表单是否提交,但GET表单会自动把带name属性的按钮参数拼到URL里。要去掉这个参数同时保留提交检测逻辑,按以下两步修改:
1. 修改HTML提交按钮
删掉提交按钮的name="submit"属性,这样表单提交时就不会把submit参数带入URL:
<input type="submit" value="Submit" style="margin-top: 20px; justify-content: center" />
2. 调整PHP的提交检测逻辑
原本靠isset($_GET['submit'])判断提交,现在改成检测必填字段reference和zipcode是否存在(因为这两个字段设置了required,用户提交时必然携带):
<?php if (isset($_GET['reference']) && isset($_GET['zipcode'])) { $reference = sanitize_text_field($_GET['reference']); $zipcode = intval($_GET['zipcode']); $curl = curl_init(); $link = "https://example.net/customer-portal/tracking?reference=$reference&postalCode=$zipcode"; curl_setopt_array($curl, [ CURLOPT_URL => $link, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", // 注意:GET请求不需要POST数据,删掉下面这行多余的CURLOPT_POSTFIELDS // CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"Reference\"\r\n\r\n2889615\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"postalCode\"\r\n\r\n35956\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", CURLOPT_HTTPHEADER => [ "AccountId: si", "Authorization: Basic d2Vic-----", "Postman-Token: 0fdfa047-----", "cache-control: no-cache" ], ]); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); $response = json_decode($response); if ($response->Data === null) { $message = '<p style="text-align: center; margin-top: -50px; margin-bottom: 60px;">The provided Customer Load Number and <br/>Pickup Zipcode combination could not be validated.</p>'; echo $message; } else { $data = $response; } } ?>
额外优化提示
你当前的CURL请求中添加了CURLOPT_POSTFIELDS,但这是GET请求,POST数据完全多余,甚至可能导致API解析异常,建议直接删除该参数。
修改完成后,用户提交表单的URL就只会包含reference和zipcode两个参数,PHP也能正确检测到表单提交并执行后续逻辑。
内容的提问来源于stack exchange,提问作者Map
相关产品推荐
相关产品推荐

