Form Redirect.. How to?

Discussion in 'PHP' started by brianj, May 16, 2009.

  1. #1
    Hi, i have a form that returns the value as /?Field=TextText

    How can i mod_rewrite redirect that value to Domain.com/TextText?

    Thanks
     
    brianj, May 16, 2009 IP
  2. dannet

    dannet Well-Known Member

    Messages:
    864
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    153
    #2
    You can use:

    RewriteRule ^(.*)/$ index.php?Field=$1
    PHP:
    and access directly to domain.com/TextText/

    Regards
     
    dannet, May 16, 2009 IP
  3. brianj

    brianj Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Thanks, but i already have this rule.. the problem is, the form directs to /?Field=TextText

    so i need to redirect the url from the form action?
     
    brianj, May 16, 2009 IP
  4. dannet

    dannet Well-Known Member

    Messages:
    864
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    153
    #4
    Could you paste the portion of code here to check it?

    I think you have to use domain.com/TextText/ directly on the form action.
     
    dannet, May 16, 2009 IP
  5. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #5
    How about;
    <?php
    if($_GET['Field']){
    	header("Location: /".$_GET['Field']."/");
    }
    ?>
    PHP:
     
    Sky AK47, May 16, 2009 IP
  6. brianj

    brianj Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    Hmm, simply said: i just don't want /?Search=TextText as search result, instead i want /TextText after the form action..

    Does this only work with JavaScript?
     
    brianj, May 16, 2009 IP
  7. aaron d.

    aaron d. Peon

    Messages:
    37
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yes, any solution would be JS only. You could fake it by having the form post to a php script which then uses header() to redirect to the domain.com/TextText/ url, but other than that, JS only.
     
    aaron d., May 16, 2009 IP
  8. brianj

    brianj Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #8
    Thanks for help, redirect sounds good.. but i didn't get Sky's code to work.. any ideas?

    <?php
    if($_GET['Field']){
        header("Location: /".$_GET['Field']."/");
    }
    ?>
    PHP:
     
    brianj, May 16, 2009 IP
  9. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #9
    I see, sorry for posting rubbish then.
    Yes, the only way doing this would be javascript I guess, like this;
    <script type="text/javascript">
    	function my_url(){
    		window.location = "/"+document.getElementById("search").value+"/";
    	}
    </script>
    <form method="get" action="index.php">
    	<input type="text" name="search" id="search" />
    </form>
    <input type="submit" onclick="javascript: my_url()" />
    Code (markup):
    EDIT: What do you mean, did you get any errors?
     
    Sky AK47, May 16, 2009 IP
  10. brianj

    brianj Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #10
    Hm, the problem with onClick is that Enter doesn't work for the form.. so how to fix that redirect?
     
    brianj, May 16, 2009 IP
  11. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #11
    Onsubmit then :)
    <script type="text/javascript">
    	function my_url(){
    		window.location = "/"+document.getElementById("search").value+"/";
    		return false;
    	}
    </script>
    <form method="get" action="index.php" onsubmit="javascript: return my_url()">
    	<input type="text" name="search" id="search" />
    	<input type="submit" />
    </form>
    Code (markup):
     
    Sky AK47, May 16, 2009 IP
  12. brianj

    brianj Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #12
    Hrm.. OnSubmit does the same as action, pointing to /?Field=TextText in this case-_-

    Any more ideas? What code can i put into redirect.php?
     
    brianj, May 16, 2009 IP
  13. aaron d.

    aaron d. Peon

    Messages:
    37
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Did you change the name of the $_GET['Field'] variable to the actual input name IE $_GET['search']?
     
    aaron d., May 16, 2009 IP
  14. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #14
    Can we see the real code then? As it's kinda hard to code something while you don't know if it would work.
    Also why, would onsubmit not work in this case, it works perfectly here.

    And, did you do what aaron adviced? Did you change the get variable with the name of your form?
     
    Sky AK47, May 16, 2009 IP
  15. brianj

    brianj Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #15
    edit: Heck! Now it works! Thanks everyone!!!
     
    brianj, May 16, 2009 IP
  16. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #16
    ht*p://www.imged.net/test.php
    What differs from yours?
    EDIT: :)
     
    Sky AK47, May 16, 2009 IP