admin管理员组文章数量:1401176
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();
}
本文标签: phpHow to submit form using JavaScript and redirect to a URLStack Overflow
版权声明:本文标题:php - How to submit form using JavaScript and redirect to a URL? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744271059a2598185.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论