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

时间:2017-08-01 04:06:04

标签: javascript php forms redirect

我现在已经做了大约2天的研究,但要么它不起作用,要么我不知道脚本是如何工作的。

我有一个带有action属性的HTML表单发布到我的电子邮件营销服务网址。问题是,它重定向到我在其旁边的表单设置中输入的URL(www.theirsite.com/myid/submissions/action正在POST的地方)。

我希望它根据选择的单选按钮重定向到不同的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>

1 个答案:

答案 0 :(得分:0)

所以你的问题很容易解决。

当用户选择radiobox时,您希望<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()函数。