0

How to submit form using JavaScript and redirect to a URL ?

<form name="f1" method="get" action="aaa.php">
    name : <input type="text" name="value_1" value=""/><br>
    age: <input type="text" name="value_2" value=""/>
    <input type="submit" name="submit" value="send"/>
</form>

please see image in this link:

http://image.ohozaa.com/i/4d0/zCzQIH.jpg

after press submit button , i want to redirect page to www.example.com/aaa/robert/21

How can i do that ?

1
  • 3
    Have you tried anything yourself? Maybe googled something? I bet this question has been asked a lot before. Commented Sep 23, 2014 at 7:38

4 Answers 4

2

Try this with html code

<form name="f1" method="get" action="aaa.php">
name : <input type="text" id="value_1" name="value_1" value=""/><br>
age: <input type="text" id="value_2" name="value_2" value=""/>
<input type="button" name="submit" onclick="checkredirect()" value="send"/>

And javascript

<script type="text/javascript">
function checkredirect(){
    var value_1 = $('#value_1').val();
    var value_2 =  $('#value_2').val();
    window.location = 'http://www.example.com/'+value_1+'/'+value_2;
    return false;
}
</script>

Don't forget to add jQuery library

Sign up to request clarification or add additional context in comments.

4 Comments

Add this to above javascrip and change button submit -> type="button"<script src="ajax.googleapis.com/ajax/libs/jquery/1.7/…>
! not work, www.example.com/aaa.php?value_1=robert&value_2=21&submit=send
change to <input type="button" name="submit" onclick="checkredirect()" value="send"/>
Don't use onclick in the HTML, please, it is evil ;-) Also, why change the window.location if the submit does it natively ? Just changing the action attribute value should be enough.
0

Maybe something like this:

HTML:

<form id="f1" class="js-redirect" method="get" action="aaa.php">
    name : <input type="text" name="value_1" id="value_1" value=""/><br>
    age: <input type="text" name="value_2" id="value_2" value=""/>
    <input type="submit" name="submit" value="send"/>
</form>

N.B.: name attribute in form tags is deprecated, use id attribute. Input, select and textarea tags should have ids as well.

JS:

$('.js-redirect').on('submit', function() {
    var newUrl = this.action.replace('.php', '/' + $('#value_1').val() + '/' + $('#value_2').val());
    this.action = newUrl;
})

Comments

0

set header in php file

$dynamic = $_GET['value_1'];
$id =$_GET['value_2'];
header('location:www.example.com/aaa/'.$dynamic.'/'.$id);

3 Comments

but robet and 21 is dynamic data.
set variable instead of robert and 21,check this edit
i mean robert and 21 are from form input.
0

Try this function on click button

function myFunction() {
    document.getElementById("myForm").submit();
}

2 Comments

This literally does nothing.
Set action action="www.example.com/aaa/robert/21" this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.