Form Submit URL Formatting

2019-01-28 19:43发布

I have a form which when submitted goes to the url "signup.php?username=xx" where xx is an input field. Instead of this I was wondering if I could get it to go to the following url "signup/xx" where xx is taken from the input field.

标签: php html forms get
3条回答
Root(大扎)
2楼-- · 2019-01-28 20:07

Try this:

RewriteRule ^signup/([^/]*)$ /signup.php?username=$1 [L]
查看更多
欢心
3楼-- · 2019-01-28 20:16

You need to use Javascript and some type of .htaccess for that. But why don't you use POST instead of GET?

I don't want singup?username=sawny&password=1234 in my history.

EDIT:

I had done something like this:

$(document).ready(function() {
    $("submit[name='foo']").live('submit', function() {

        var usr = $("form input[name='user']").val();
        $("form input[name='user']").remove(); //Else it will be singup/usr?user=

        $("form[name='bar']").attr("action", "singup/"+ usr);
    });
});

and then the regexp @zero posted in a .htaccess file.

查看更多
趁早两清
4楼-- · 2019-01-28 20:33

Include jQuery if not already included

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>

and then add this script

<script>
$(function(){
    $("form").submit(function(e){
        e.preventDefault();
        var action = $(this).attr("action");
        var username = $(":input[name='username']").val();
        action = action + "/" + username;
        location.href = action;
    })
})
</script>
查看更多
登录 后发表回答