繁体   English   中英

将单选按钮设置为onclick两个目标(提交表单和重定向)

[英]Set radio button onclick two destinations (submit form and redirect)

这怎么可能? 我有这段代码可以满足我一半的需求。 单击单选按钮时,它将重定向用户,但表单没有保存或通过电子邮件提交给我。 是否可以使其一次执行2个命令? 这是代码

<input type="radio" id="display_al" name="display_al" value="display_al" onClick="this.form.action='book-now-2';this.form.submit;"  onMouseOver="style.cursor='hand'">

我在这里想念什么? 我将它用于联系表格,这样人们就会有想法。 每当他们选择其他付款方式时,我都会对其进行重定向。 我想通过信用卡付款将其重定向到更安全的页面。

这里有两个选项:1)在表单中添加一些隐藏的信息,告诉您的表单提交脚本保存信息后需要重定向到另一个页面:

首先向表单添加一个隐藏字段:

<input type="hidden" name="redirect" id="redirect" />

然后更改onclick

onclick="document.getElementById('redirect').value='altpayment';this.form.submit;"

并更新您的表单处理程序

<?php
//your normal form submission code, and then...

if(isset($_POST['redirect']) && $_POST['redirect'] == "altpayment"){
    header("location: http://www.yoursite.com/book-now-2");
}else{
    //whatever you normally do after submitting the form
}

2)使用AJAX提交表单,然后重定向:

创建一个JavaScript函数

<script>
function submitForm(){
 $.ajax({
        url: 'some-url',
        type: 'post',
        dataType: 'json',
        data: $('form#myForm').serialize(),
        success: function(data) {
            window.location.replace("http://www.yoursite.com/book-now-2");
        }
    });
}
</script>

更改onclick

onclick="submitForm();"

如果选择第二种方法,请确保在页面上包括JQuery框架,并用#myForm ID替换#myForm

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM