简体   繁体   中英

how to get multiple radio button values in php

I have a webpage that contains two set of radio buttons. I want to do; if user select a OS in first fieldset and a language from second fieldset, then user must directed to relevant pages.

Please guide me to complete this page. I am unable to get two radio button values at a time. If I rename the radio buttons like below,

<fieldset id="group1">
    <input type="radio" class="radto" name="android"/>
</fieldset>
<fieldset id="group1">
    <input type="radio" class="radto" name="english"/>
</fieldset>

It's working, but the problem is users can select more than one OS and language. I need to prevent this. How can I overcome this problem.

<?php
if (isset($_POST['submit'])) {
    if (isset($_POST['android']) && isset($_POST['english'])) {

        header("location: andro_eng.php");
        exit();
}

    if (isset($_POST['android']) && isset($_POST['french'])) {

        header("location: andro_fre.php");
        exit();
    }
}

?>

<html>
<head>
</head>

<body>
<form action="" method="post">
<fieldset id="group1">
    <li><input type="radio" class="radto" name="a"/>&nbsp;   &nbsp;android</li>
    <li><input type="radio" class="radto" name="a"/>&nbsp;   &nbsp;ios</li>
    <li><input type="radio" class="radto" name="a"/>&nbsp;   &nbsp;symbian</li>
</fieldset>

<fieldset id="group2">
    <li><input type="radio" class="radto" name="b">&nbsp; &nbsp;english</li>   
    <li><input type="radio" class="radto" name="b" >&nbsp; &nbsp;french</li>
    <li><input type="radio" class="radto" name="b">&nbsp; &nbsp;spanish</li>
</fieldset>
<input type="submit" value="next" name="submit">
</form>
</body>
</html>

The name attribute has to be the same for the different options:

 $( document ).ready(function() { $('#button').click(function() { alert($('input[name=os]:checked').val()); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <fieldset id="group1"> <input type="radio" class="radio" name="os" value="android" id="android" /> <label for="a">Android</label> </fieldset> <fieldset id="group1"> <input type="radio" class="radio" name="os" value="iOS" id="iOS" /> <label for="b">iOS</label> </fieldset> <button id="button" type="button">Get value</button> 

And you need to add a value attribute to the radio inputs, otherwise you won't get any value.

Then you can get the values from PHP: $_POST['os'] will return android , iOS or may be empty if no value is selected.

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