简体   繁体   中英

Redirect Form User When Submit Based On Radio Selection Only

I have very low knowledge on coding so picking up the basics as i go along.

During my multiple page form i have 4 "radio" type selections where the end user has to select at least 1 to proceed with the form. What i am trying to do is have the form Redirect the user to the specific page relation to the radio input they selected when they submit the form.

I Have watched you tube videos and read loads of old posts all day and everything i try seems to not work.

Im not sure if javascript, jquery or php would be best using the if or isset statement, Due to my knowledge im not upto date with the right search phrases to use in order to find the correct method so after 14 hours of trying im having to seek your expertise!

Any simple way would be adequate and extremely helpful and for that im hoping someone can help me out on this.

for example the 4 radio selections would go to:

<input class="form-radio" type="radio" id="hbs" name="options" value="hbs" />  ==>  page1.php
<input class="form-radio" type="radio" id="bs"  name="options" value="bs"  />  ==>  page2.php
<input class="form-radio" type="radio" id="pvr" name="options" value="pvr" />  ==>  page3.php
<input class="form-radio" type="radio" id="epc" name="options" value="epc" />  ==>  page4.php 

I need the code to only redirect the user when he has clicked on submit, The reason for this is there are multiple fields within the form on the same page as the radio selections.

Thanks

You can redirect a to a specific page via header('Location: url'); in php.

Here is an example matching your request :

<?php
$value_to_page = array(
    "hbs" => 'page1.php',
    "bs"  => 'page2.php',
    "pvr" => 'page3.php',
    "epc" => 'page4.php',
);
if(isset($_POST['submit'])){

    // process other fields like $_POST['some-other']
    if(isset($_POST['options']) && isset($value_to_page[$_POST['options']])){
        header('Location: '.$value_to_page[$_POST['options']]);
        return;
    }
} ?>

<form method="post">
    <input type="text" name="some-other" />
    <input class="form-radio" type="radio" id="hbs" name="options" value="hbs" />
    <input class="form-radio" type="radio" id="bs"  name="options" value="bs"  />
    <input class="form-radio" type="radio" id="pvr" name="options" value="pvr" />
    <input class="form-radio" type="radio" id="epc" name="options" value="epc" />
    <button type="submit" name="submit">Send</button>
</form>

A simple solution might be to require_once the code of the page you want for the results of the POST as shown (place this at the top of your php file above any HTML):

<?php

switch ($_POST["options"]) {
    case "hbs":
        require_once("page1.php");
        break;
    case "bs":
        require_once("page2.php");
        break;
    case "pvr":
        require_once("page3.php");
        break;
    case "epc":
        require_once("page4.php");
}

die();

?>

Ok, this is a good question, it is not hard though, follow using jQuery, you too can see it working in jsfiddle (will not work properly in jsfiddle for security questions, but try it in your machine ):

<form id="myForm">
    <input class="form-radio" type="radio" id="hbs" checked="true" name="options" value="hbs"  page="page1.php" />  ==>  page1.php
    <input class="form-radio" type="radio" id="bs"  name="options" value="bs" page="page2.php"  />  ==>  page2.php
    <input class="form-radio" type="radio" id="pvr" name="options" value="pvr" page="page3.php" />  ==>  page3.php
    <input class="form-radio" type="radio" id="epc" name="options" value="epc" page="page4.php" />  ==>  page4.php
    <button type="submit">send</button>
</form>

<script>
    $(function(){
       $("#myForm").submit(function(e){
           var page = $(this).find("input[name=options]:checked").attr("page");
           this.action = page;       
       });
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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