0

Webページにフォームがあります:

<?php echo form_open('/search/processing'); ?>

<input type="text" name="query" size="50" />

    <input type="submit" name="1" value="1" /></br>
    <input type="submit" name="2" value="2" /></br>
    <input type="submit" name="3" value="3" /></br>

</form>

このコントローラー関数に移動します。

public function processing()
{
// redirects here based on user input and what submit button was pressed
}

この機能を取り除き、ユーザーが入力した内容に基づいてフォームから直接リダイレクトすることは可能でしょうか?

4

4 に答える 4

1
<script>
window.addEventListener('load', function(){
    document.getElementById('formid').addEventListener('submit', function(){
        for(var i = 0; i < this.length; i++)
            if(this[i].type == 'submit' &&
               this[i].value == 1) window.location.href = 'foo.com';
            else if( ... )
               ....
            else if( ... )
               ....
    }, false);
}, false);
</script>
于 2012-07-23T15:31:20.880 に答える
0

純粋なPHPを使用する場合は、次のようにします。

if(isset($_POST['query']))
{
  switch($_POST['query'])
  {
    case 'hello': header('location: http://www.google.com'); break;
    case 'stack': header('location: http://www.ask.com'); break;
    case 'overflow': header('location: http://www.yahoo.com'); break;
  }
}
于 2012-07-23T15:29:04.640 に答える
0

はい、これは次の方法で実現できます。コメントがある場合は、リダイレクトコード、または関数呼び出しを含めます。

if (isset($_POST['query']) {
  $value = $_POST['query'];
  if ($value == 1) {
    //do something
  }
  if ($value == 2) {
    //do something
  }
}
于 2012-07-23T15:29:20.850 に答える
0

これは、フォーム アクションを変更するプレーンな JavaScript バージョンです。3 つの個別のボタンがあるため、正確に望むものではないかもしれませんが、役に立つかもしれません。

<script type="text/javascript">
function test()
 {
     if ( document.forms.form.test1.checked )
     {
         document.forms.form.action = "http://google.com";
     }

     return true;
 }
</script>
<form name="form" action="a.html" onsubmit="return test()">
    <input type="checkbox" id="test1" name="test1" />
    <input type="submit" value="submit" />
</form>

基本的に、ボタンがチェックされている場合は google.com に移動します - それ以外の場合は a.html

于 2012-07-23T15:48:40.090 に答える