我为我的客户构建的web表单已经运行了几周,但是最近他告诉我,他希望我在表单中引入第四个控件,以进一步细化结果。
我遇到的问题是我的url呈现的不正确,这扰乱了稍后在我的页面上运行的查询。
旧URL
http://mysite.com/product?s=INTERMITTENT&p=1300&g=1200上面的URL搜索所有产品的id为间歇,然后过滤他们的总吞吐量宝洁,这很好,直到他想让我引入第四个控制,这将允许在公制和帝国之间的转换,新的URL看起来有点像这样
http://mysite.com/product?s=INTERMITTENT&m=IMPERIAL&p=1000&g=350然而,每次表单提交时,我都会收到一些类似于
http://mysite.com/product?s=IMPERIAL&m=IMPERIAL&p=1000&g=350我不知道为什么输出是这样的
HTML标记
<form id="pumpSlider" action="" method="GET" align="center">
<input id="bS" type="hidden" name="s" value="<?php echo $pType ?>" />
<div class="btn-group" data-toggle="buttons-radio">
<button type="submit" class="<?php if( $pType == 'INTERMITTENT' ){ echo 'active '; } ?>btn btn-primary waitingUiBut" id="but1">INTERMITTENT</button>
<button type="submit" class="<?php if( $pType == 'CONTINUOUS' ){ echo 'active '; } ?>btn btn-primary waitingUiBut" id="but4">CONTINUOUS</button>
</div>
<input type="hidden" name="m" value="<?php echo $_GET['m'] ?>" />
<div class="btn-group" data-toggle="buttons-radio" style="display:block; padding-top: 20px;">
<button type="submit" class="<?php if( $_GET['m'] == 'METRIC' ){ echo 'active '; } ?>btn btn-primary waitingUiBut" id="but5">METRIC</button>
<button type="submit" class="<?php if( $_GET['m'] == 'IMPERIAL' ){ echo 'active '; } ?>btn btn-primary waitingUiBut" id="but6">IMPERIAL</button>
</div>
<div align="center" class="productSlider">
<p class="inlineLabel">PSI</p><div class="filterSlider" id="psiSlider"></div>
<p class="inlineLabel">GPM</p><div class="filterSlider" id="gpmSlider"></div>
<input id="pS" type="hidden" name="p" value="<?php echo $pVal ?> " />
<input id="gS" type="hidden" name="g" value="<?php echo $gVal ?>" />
</div>
</form>隐藏的输入接受它们被赋予的提交字段的值。
我想问题可能在我的PHP代码中,但我不知道在哪里
# Check if each variable is set, if not delegate default values.
if(!isset($_GET['m']) || !isset($_GET['p']) || !isset($_GET['g']) || !isset($_GET['s'])){
$_GET['m'] = "IMPERIAL";
$_GET['g'] = "0";
$_GET['p'] = "0";
$_GET['s'] = "INTERMITTENT";
$cVal = "classic";
}
# Validate the input is correct (check for tampered URL)
if(isset($_GET['s'])){
if(preg_match('/^[INTERMITTENT | CONTINUOUS ]+$/', stripslashes(trim($_GET['s']))))
{
$pType = $_GET['s'];
$cVal = "classic";
} else {
$pType = "INTERMITTENT";
$cVal = "classic";
}
}
if(isset($_GET['p'])){
if(preg_match('%^[0-9]{1,6}$%', stripslashes(trim($_GET['p']))))
{
$pVal = $_GET['p'];
} else {
$pVal = "0";
}
}
if(isset($_GET['g'])){
if(preg_match('%^[0-9]{1,6}$%', stripslashes(trim($_GET['g']))))
{
$gVal = $_GET['g'];
} else {
$gVal = "0";
}
}
if(isset($_GET['m'])){
if(preg_match('/^[IMPERIAL | METRIC ]+$/', stripslashes(trim($_GET['m']))))
{
$m = $_GET['m'];
} else {
$_GET['m'] = "IMPERIAL";
}
}发布于 2013-09-06 14:54:57
您可能应该使用http_build_query,而不是自己连接这个值。只需构建一个带有值的数组,然后“交换”那些需要更新的数组(然后调用http_build_query将它们放在一起)。例如:
// establish initial data:
$params = array(
's' => $_GET['s'], // INTERMITTENT
'm' => $_GET['m'], // IMPERIAL
'p' => (int)$_GET['p'], // 1300
'g' => (int)$_GET['g'] // 1200
);
/* $params validation logic, and maybe fallback values on failures */
// propose a change:
$params['m'] = 'METRIC';
// rebuild:
$query = http_build_query($params); // s=INTERMITTENT&m=METRIC&p=1300&g=1200然后,只要您的表单是从$params填充的,就不应该看到任何重复的值。
https://stackoverflow.com/questions/18660562
复制相似问题