如何根据所选的无线电值从表单提交重定向到不同的URL

时间:2017-06-20 09:29:03

标签: javascript php forms

我正在尝试处理一个html表单,捕获我已经工作的典型名称和电子邮件。典型的表单提交到php页面。

我现在要做的是捕获单选按钮的值,具体取决于检查哪一个(有10个)。当用户提交表单时,我想根据选择的无线电值将用户重定向到十个页面中的一个。

因此,例如,如果选中了收音机1 - 请转到page1.html,如果选中第2页,则转到第2.html页等。

所以我认为我需要在我的php文件中使用IF语句,这可能会检查switch类的值,然后根据该值重定向到相关页面。我没有做太多的PHP,所以我不能完全确定正确的流程。

非常感谢任何帮助。最好的问候尼克

HTML

        <form id="rate-our-page" method="post" action="feedbackForm.php" role="form">
            <div class="form-group switch">
                <input name="switch" id="one" value="1" type="radio" role="radio"/>
                <label for="one" class="switch__label">1</label>
                <input name="switch" id="two" value="2" type="radio" role="radio"/>
                <label for="two" class="switch__label">2</label>
                <input name="switch" id="three" value="3" type="radio" role="radio"/>
                <label for="three" class="switch__label" >3</label>
                <input name="switch" id="four" value="4" type="radio" role="radio"/>
                <label for="four" class="switch__label" >4</label>
                <input name="switch" id="five" value="5" type="radio" role="radio" checked/>
                <label for="five" class="switch__label" >5</label>
                <input name="switch" id="six" value="6" type="radio" role="radio"/>
                <label for="six" class="switch__label" >6</label>
                <input name="switch" id="seven" value="7" type="radio" role="radio"/>
                <label for="seven" class="switch__label" >7</label>
                <input name="switch" id="eight" value="8" type="radio" role="radio"/>
                <label for="eight" class="switch__label" >8</label>
                <input name="switch" id="nine" value="9" type="radio" role="radio"/>
                <label for="nine" class="switch__label" >9</label>
                <input name="switch" id="ten"  value="10" type="radio" role="radio"/>
                <label for="ten" class="switch__label" >10</label>
                <div class="switch__indicator" />
            </div>
            <div class="messages"></div>
            <div class ="form-controls row">
                <div class="form-group col-sm-12 col-md-6">
                    <input id="form_name" type="text" name="name" class="form-control input-lg" placeholder="YOUR NAME" required="required" data-error="Name is required" role="textbox" aria-required="true">
                    <div class="help-block with-errors"></div>
                </div>
                <div class="form-group col-sm-12 col-md-6">
                    <input id="form_email" type="email" name="email" class="form-control input-lg" placeholder="EMAIL ADDRESS" required="required" data-error="Valid email is required" role="textbox" aria-required="true">
                    <div class="help-block with-errors"></div>
                </div>
                <button type="submit" class="btn btn-lp-primary" value="Send message" role="button">Submit Rating</button>
            </div>
        </form>

PHP

<?php
// configure
$from = 'Landing Page Feedback Form <nick@test.co.uk>'; 
$sendTo = 'Nick <nick@test.co.uk>';
$subject = 'You Have Some New Feedback On Your Landing Page!';
$fields = array('name' => 'Name', 'email' => 'Email'); // array variable name => Text to appear in email
$okMessage = 'Thank, We will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again';



// let's do the sending

try
{
    $emailText = "A new lead has been generated via the landing page, details captured follow. \n \n";

    foreach ($_POST as $key => $value) {

        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    }

    mail($sendTo, $subject, $emailText, "From: " . $from);

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}
else {
    echo $responseArray['message'];
}

1 个答案:

答案 0 :(得分:0)

问题似乎缺少某些细节,但您可能会考虑以下问题。无论如何,这是一种方法。

if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['switch'] ) ){


    function geturl( $i ){
        switch( $i ){
            case 1: return 'http://www.example.com';
            case 2: return 'http://www.foo.com';
            case 3: return 'http://bar.com';
            /* etc */
        }   
    }


    $redirect = geturl( intval( $_POST['switch'] ) );
    header("Location: {$redirect}");

}