【问题标题】:Check input if start with then submit to the right action Php html检查输入是否开始,然后提交到正确的操作 Php html
【发布时间】:2018-05-05 05:32:20
【问题描述】:

我需要帮助 我有一个带有输入文本的表单

<?php 
if($input == ‘aab’ or ‘aac’){
    $action = " http://mywebsite.com /women/search=$input"; 
}else if($input == ‘aad’ or ‘aaf’ or ‘aat’){
    $action =" http://mywebsite.com /men/search=$input”;
}
?>
<form action= "<?php echo $action; ?>" method="post">
<input id="Input" name="Input" class="input-block-level "></p>


                    <p class="pull-right" ><button id="Search" class="btn" >                    Search

                </form>

它有两个不同的动作,例如如果输入文本以 aab 或 aac 开头,那么它将提交给http://mywebsite.com /women/search=$input 如果输入文本值以 aad 或 aaf 或 aat 开头,则动作将是 到http://mywebsite.com /men/search=$input 所以首先我们将检查输入是否以 aab 或 aac 或 aad 或 aat 开头...然后 subit 到正确的 url

【问题讨论】:

  • ‘’ - 巧妙的引语,美丽而致命。
  • 你有双重$$..请告诉我上面是伪代码
  • 您的else if 毫无意义且无效。你的整个帖子都站不住脚。
  • '' 和 $$, == == in else if 无效,没有意义
  • 我一个,不会重写你的代码;我出去了。

标签: php jquery html forms validation


【解决方案1】:

使用 PhP 是不可能的。你可以在 Javascript 的帮助下完成,我建议你使用 jQuery。如下所示。

<script type="text/javascript">
$(function(){
    $('#Input').change(function(){
        var input = $('#Input').val(),
            status = input.substring(0,3);
        if(status === "aad" || status === "aaf" || status === "aat"){
            $('form').attr('action', 'http://mywebsite.com/men/search='+input);
        }else if(status === "aab" || status === "aac"){
            $('form').attr('action', 'http://mywebsite.com/women/search='+input);
        }
    });
});
</script>

别忘了包含 Jquery。

【讨论】:

  • if/else if 使用$.inArray(input, ['aab', 'aab', 'aac']) 会更干净
  • @MrMarlow 是的,你是对的,但这有点典型,因为他还处于起步阶段。注意这里我们将使用 $.inArray(status, ['aab', 'aab', 'aac']) ,我们还必须注意索引 0
  • @santosh 非常感谢您希望编辑代码以将状态设为数组,因为可能性超过 22 个
  • @faucot,我在对此答案的评论中提到了这一点。将此函数返回值与 -1 进行比较
【解决方案2】:

您需要将 $variable 替换为您实际检查的内容,但这应该可以满足您的需求。

在cmets中添加了一些注释,稍微解释一下。

<?php
// Default action for if none is matched below in if/elseif clause
// by default, this will search the location you are not.
$action = '';

// Replace $variable with the variable you are checking against.
// The variable has to exist AND contain at least 3 characters
$input  = isset($variable) && strlen($variable) > 3 ? substr($variable, 0, 3) : false;

// Check the $input variable is not false, and is either 'aab' or 'aac'
if ($input && in_array($input, array('aab', 'aac'))) {
    $action = 'http://mywebsite.com/women/search=' . $input;

// Otherwise check if $input is set and is either: aad, aaf or aat
} else if ($input && in_array($input, array('aad', 'aaf', 'aat'))) {
    $action = 'http://mywebsite.com/men/search=' . $input;
}

if (strlen($action)) {
    echo <<<JS
        <script>
        document.location.href='$action';
        </script>
JS;
    exit;
}

// Output form. You were missing closing elements etc.
echo <<<FORM
    <form action="" method="post">
        <input id="Input" name="Input" class="input-block-level">
            <p class="pull-right">
                <button id="Search" class="btn" >Search</button>
            </p>
    </form>
FORM;

【讨论】:

  • 非常感谢您的回复和此解决方案,我正在检查的是输入文本值 STARTSWITH 是否可以是 aab5264 或 aab4715
  • 是的,我明白了,但代码从何而来?它是在 URL 中,还是在其他地方生成的?
  • $variable 应该是输入文本的值
  • 我在中间添加了额外的块,它调用 javascript 进行重定向。 PHP 可以重定向,但前提是不发送标头 - 我无法确认,因此恢复为原生 javascript。忽略上面的need jQuery废话-jQuery IS NOT NEEDED
猜你喜欢
  • 1970-01-01
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
  • 2010-11-10
  • 1970-01-01
  • 1970-01-01
  • 2011-08-18
  • 2017-07-24
相关资源
最近更新 更多