0

I have a form on a webpage:

<?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>

It goes to this controller function:

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

I'm wondering is it possible to get rid of this function and simply redirect straight from the form based on what the user has entered?

Alex
  • 34,899
  • 5
  • 77
  • 90
flux
  • 1,518
  • 1
  • 17
  • 31
  • your answer lies in javascript, this may help you: http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery-javascript – Peeyush Kushwaha Jul 23 '12 at 15:27
  • Yes it is possible, Can you please elaborate it more, probably with some example? – FatalError Jul 23 '12 at 15:29
  • Havent had time to try it, but if you need it in javascript, cant you use the onvalidate to alter the action url? As i say, i havent tried it but that was what sprang to mind. If you havent sorted it, ill try that out later this afternoon. – Adrian Brown Jul 23 '12 at 15:31
  • You can't override the controller's method but using `Javascript` you can achieve this. – The Alpha Jul 23 '12 at 15:40

4 Answers4

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>
Alex
  • 34,899
  • 5
  • 77
  • 90
0

If you want to use pure PHP, then:

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;
  }
}
Ben Poulson
  • 3,368
  • 2
  • 17
  • 26
0

Yes, this can be achieved in the following: Where the comments are, include redirect code, or a function call.

if (isset($_POST['query']) {
  $value = $_POST['query'];
  if ($value == 1) {
    //do something
  }
  if ($value == 2) {
    //do something
  }
}
mlishn
  • 1,689
  • 14
  • 19
  • OP is using CodeIgniter framework and don't want to use the controller to be invoked, this answer is not helpful in this case. – The Alpha Jul 23 '12 at 15:33
0

Here is a plain javascript version of changing the form action, it may not be exactly what you want as you have 3 seperate buttons, but it maybe useful.

<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>

Basically if you have the button checked it goes to google.com - else a.html

Adrian Brown
  • 456
  • 3
  • 14