设置单选按钮单击两个目的地(提交表单和重定向)

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

本文关键字:提交 表单 重定向 目的地 两个 单选按钮 设置 单击      更新时间:2023-09-26

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

<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。