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:
.jpg
after press submit button , i want to redirect page to www.example/aaa/robert/21
How can i do that ?
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./i/4d0/zCzQIH.jpg
after press submit button , i want to redirect page to www.example./aaa/robert/21
How can i do that ?
Share Improve this question edited Sep 23, 2014 at 8:06 Kevin 41.9k12 gold badges56 silver badges72 bronze badges asked Sep 23, 2014 at 7:37 pap mooopap mooo 771 gold badge2 silver badges8 bronze badges 1- 3 Have you tried anything yourself? Maybe googled something? I bet this question has been asked a lot before. – Déjà vu Commented Sep 23, 2014 at 7:38
4 Answers
Reset to default 2Try 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./'+value_1+'/'+value_2;
return false;
}
</script>
Don't forget to add jQuery library
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 id
s as well.
JS:
$('.js-redirect').on('submit', function() {
var newUrl = this.action.replace('.php', '/' + $('#value_1').val() + '/' + $('#value_2').val());
this.action = newUrl;
})
set header in php file
$dynamic = $_GET['value_1'];
$id =$_GET['value_2'];
header('location:www.example./aaa/'.$dynamic.'/'.$id);
Try this function on click button
function myFunction() {
document.getElementById("myForm").submit();
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744271059a4566103.html
评论列表(0条)