【问题标题】:PHP: redirect to blog pages by selectbox on HTML formPHP:通过 HTML 表单上的选择框重定向到博客页面
【发布时间】:2015-11-07 15:35:27
【问题描述】:

我是 PHP 编程的新手,但我必须将它用于我博客中的一个页面,该页面允许用户使用单选框和选择框在博客内进行搜索。我已经阅读了一些关于它的讨论,但我在我的代码中找不到错误。

这是我的情况:我允许在我的博客中按品牌、型号或 cc 搜索有关摩托车的内容,因此我有一个带有三个单选框的表单,可以启用/禁用下面的三个选择框。上述每个类别一个。单击一个按钮,用户将被重定向到相应的链接 /brand/ 或 /model/ 或 /cc/。

这是我的 HTML 表单:

<form name="admin" method="post" action="/search.php">
<input type="radio" name="rdo_brand" value="brand" id="r_brand" onclick="document.getElementById('r_model').checked=false;document.getElementById('r_cc').checked=false;document.getElementById('box_brand').disabled=false;document.getElementById('box_model').disabled=true;document.getElementById('box_cc').disabled=true;"> Search by brand
<input type="radio" name="rdo_model" value="model" id="r_model" onclick="document.getElementById('r_brand').checked=false;document.getElementById('r_cc').checked=false;document.getElementById('box_brand').disabled=true;document.getElementById('box_model').disabled=false;document.getElementById('box_cc').disabled=true;"> Search by model
<input type="radio" name="rdo_cc" value="cc" id="r_cc" onclick="document.getElementById('r_brand').checked=false;document.getElementById('r_model').checked=false;document.getElementById('box_brand').disabled=true;document.getElementById('box_model').disabled=true;document.getElementById('box_cc').disabled=false;"> Search by cc

Brand
<select name="brand" id="box_brand" />
<option value="-">-</option>
<option value="bmw">Bmw</option>
<option value="ducati">Ducati</option>
<option value="honda">Honda</option>
</select>

Model
<select name="model" id="box_model" />
<option value="-">-</option>
<option value="k1200gt">K1200GT</option>
<option value="monster">Monster</option>
<option value="silverwing>Silver Wing</option>
</select>

CC
<select name="cc" id="box_cc" />
<option value="-">-</option>
<option value="600">600</option>
<option value="900">900</option>
<option value="1200">1200</option>
</select>

<input type="submit" value="Search" id="btn_search" />
>/form>

这是我的search.php

// Data recovery process
// I know it's not the better way but is the most simply for a newbie like me!
if (empty($_POST['brand']))
{
    $brand = 'nothing';
}
else
{
    $brand = $_POST['brand'];
}
if (empty($_POST['model']))
{
    $model = 'nothing';
}
else
{
    $model = $_POST['model'];
}
if (empty($_POST['cc']))
{
    $cc = 'nothing';
}
else
{
    $cc = $_POST['cc'];
}

// Replacing spaces in acquired variables
$brand = str_replace(" ", "-", $brand);
$model = str_replace(" ", "-", $model);
$cc = str_replace(" ", "-", $cc);

// Control process
if ($brand <> 'nothing')
{
    header("location: mysite.com/categories/brands/$brand/");
    exit;
}
else
{
    if ($model <> 'nothing')
    {
        header("location: mysite.com/categories/models/$model/");
        exit;
    }
    else
    {
        header("location: mysite.com/categories/cc/$cc/");
        exit;
    }
}

我认为代码是正确的,但显然不是,因为它不能完全工作。如果我检查 rdo_brand 并选择 brand 内的选项之一(例如 bmw),则重定向对 工作正常mysite.com/categories/brands/bmw/...但如果我检查 rdo_model 并选择 model 中的选项之一,重定向错误地将我带到 mysite.com/categories/cc/nothing/

我无法理解!为什么会这样?

【问题讨论】:

  • 尝试将您的条件 if ($brand &lt;&gt; 'nothing') 更改为 if ($brand != 'nothing')
  • 你不能只使用链接吗?
  • @TismonVarghese 条件&lt;&gt;!= 不一样?不幸的是,结果 - 错误地 - 相同。 @JamesFenwick 我不明白。您建议以其他方式编写链接?我必须使用完整的写作(mysite.com/categories/brands/SELECTEDBRAND),因为我的博客帖子托管在一个单独的域中)

标签: php html forms redirect header


【解决方案1】:

尝试修复 html 代码中的拼写错误(见下文)

<form name="admin" method="post" action="/search.php">
<input type="radio" name="rdo_brand" value="brand" id="r_brand" onclick="document.getElementById('r_model').checked=false;document.getElementById('r_cc').checked=false;document.getElementById('box_brand').disabled=false;document.getElementById('box_model').disabled=true;document.getElementById('box_cc').disabled=true;"> Search by brand
<input type="radio" name="rdo_model" value="model" id="r_model" onclick="document.getElementById('r_brand').checked=false;document.getElementById('r_cc').checked=false;document.getElementById('box_brand').disabled=true;document.getElementById('box_model').disabled=false;document.getElementById('box_cc').disabled=true;"> Search by model
<input type="radio" name="rdo_cc" value="cc" id="r_cc" onclick="document.getElementById('r_brand').checked=false;document.getElementById('r_model').checked=false;document.getElementById('box_brand').disabled=true;document.getElementById('box_model').disabled=true;document.getElementById('box_cc').disabled=false;"> Search by cc

Brand
<select name="brand" id="box_brand" />
<option value="-">-</option>
<option value="bmw">Bmw</option>
<option value="ducati">Ducati</option>
<option value="honda">Honda</option>
</select>

Model
<select name="model" id="box_model" />
<option value="-">-</option>
<option value="k1200gt">K1200GT</option>
<option value="monster">Monster</option>
<option value="silverwing">Silver Wing</option>
</select>

CC
<select name="cc" id="box_cc" />
<option value="-">-</option>
<option value="600">600</option>
<option value="900">900</option>
<option value="1200">1200</option>
</select>

<input type="submit" value="Search" id="btn_search" />
</form>

【讨论】:

  • 如果拼写错误是&gt;/form,我向您保证原始 HTML 已经正确。错别字是通过复制和改编此处的代码而造成的。所以,如果其余的代码没有错误,我真的无法解释为什么不工作。
  • 该死,我在&lt;option value="silverwing&gt; 行中发现了另一个错别字(正如您已经注意到的那样!):修复它,代码可以完美运行。多么愚蠢的错误;我很尴尬!所以谢谢大家。我希望我的代码对网民有用!
猜你喜欢
  • 2020-09-10
  • 2015-12-16
  • 1970-01-01
  • 1970-01-01
  • 2016-06-13
  • 2018-12-23
  • 2018-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多