繁体   English   中英

如何基于表单数据重定向到页面?

[英]How do I redirect to a page based on form data?

我已经进行了大约2天的研究,但是它要么无法正常工作,要么不知道脚本如何工作。

我有一个HTML表单,其中的action属性张贴到我的电子邮件营销服务的url中。 问题是,它被重定向到我在他们那一侧的表单设置中输入的URL( www.theirsite.com/myid/submissions/是要向其发布action位置)。

我希望它根据选择哪个单选按钮重定向到不同的URL,但只允许我为所有提交都放置一个URL。

任何帮助,将不胜感激。 我是编码新手,但了解基本规则。

先感谢您!

编辑:这是我的表单代码。

<form action="https://www.getdrip.com/forms/19579582/submissions" method="get" data-drip-embedded-form="19579582" data-drip-id="19579582">

单选按钮:

<input name="fields[situation]" required="" type="radio" value="employed full time"> <input name="fields[situation]" required="" type="radio" value="employed part time"> <input name="fields[situation]" required="" type="radio" value="unemployed"> <input name="fields[situation]" required="" type="radio" value="retired"> <input name="fields[situation]" required="" type="radio" value="self employed">

按键:

<button type="submit" name="submit" data-drip-attribute="sign-up-button">My Button Text</button>

因此,您的问题很容易解决。

当用户选择一个单选框时,您希望<form action="www.theirsite.com/myid/submissions/">进行更新。

这是您可以在普通JS中执行的操作:

<!-- So these inputs when clicked, will update your 'action' URL -->
<input name="myRadioButton" required="" type="radio" value="employed-full-time" data-marketing="employed full time" onclick="updateFormAction(this)">
<input name="myRadioButton" required="" type="radio" value="employed-part-time" data-marketing="employed part time" onclick="updateFormAction(this)">
<input name="myRadioButton" required="" type="radio" value="unemployed" data-marketing="unemployed" onclick="updateFormAction(this)">
<input name="myRadioButton" required="" type="radio" value="retired" data-marketing="retired" onclick="updateFormAction(this)">
<input name="myRadioButton" required="" type="radio" value="self-employed" data-marketing="self employed" onclick="updateFormAction(this)">

<!-- This is the bit that lets you decide what type it is -->
<input id="marketingValue" name="marketingValue" type="hidden" value="none" />

<script>
    function updateFormAction(radioButton) {
        // These 2 lines will update your forms ACTION URL
        // If there is only one form on the page this will work
        document.forms[0].action = radioButton.value

        // Otherwise select the form you want to target using ID
        document.getElementById('form-id').action = radioButton.value;

        // This will update your hidden input value
        document.getElementById('marketingValue').value = radioButton.getAttribute('data-marketing');
    }
</script>

上面的代码将完成您想要的。 每次用户单击单选按钮时,表单的action = {value}都会更新,因为它调用updateFormAction()函数。

暂无
暂无

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

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