Alexander Vasilchuk
Alexander Vasilchuk

Reputation: 155

RadioButtons in php

I've got a form:

    <form  action= "catrand.php" method="POST" name="form">
    <h4>How mach text need to print?</h4>
    <input type="text" onkeyup="validate(this)" name="inp"> <script> function validate(inp) { inp.value = inp.value.replace(/[^\d,.]*/g, '') .replace(/([,.])[,.]+/g, '$1') .replace(/^[^\d]*(\d+([.,]\d{0,5})?).*$/g, '$1'); } </script>
    <h4>Select how print text</h4>
<input type="radio" name="b001" value="First"><font>text</font>
<input type="radio" name="b001" value="Second"><font>check</font>
<input type="radio" name="b001" value="Last"><font>radio</font>
    <h4>Input text</h4>
    <input type="text" name="inp1">
    <input type="text" name="inp2">
    <input type="text" name="inp3">
    <p><input type="submit"></p>    
    </form>

and php code:

for($i=1;$i<=$number;$i++)
    {
        switch($_POST['b001']) {
            case "Last":
            {
                echo "<input type=\"radio\" onclick=\"uncheck_radio(this)\" value=\"First\" \"name=\"h001\">" .$numberone; 
                echo "<input type=\"radio\" onclick=\"uncheck_radio(this)\" value=\"Second\" \"name=\"h001\">".$numbertwo; 
                echo "<input type=\"radio\" onclick=\"uncheck_radio(this)\" value=\"Third\" \"name=\"h001\">".$numberthree."</br>";
                break;
            }


}           
    }

But despite the fact that the names of radiobuttons are same, i can mark two and more radiobuttons. Why? What is wrong in my code?

Upvotes: 0

Views: 38

Answers (3)

Taj Uddin
Taj Uddin

Reputation: 523

Problem is in your php code

change your code as below

for($i=1;$i<=$number;$i++)
{
    switch($_POST['b001']) {
        case "Last":
        {
            echo "<input type=\"radio\" onclick=\"uncheck_radio(this)\" value=\"First\" name=\"h001\">" .$numberone; 
            echo "<input type=\"radio\" onclick=\"uncheck_radio(this)\" value=\"Second\" name=\"h001\">".$numbertwo; 
            echo "<input type=\"radio\" onclick=\"uncheck_radio(this)\" value=\"Third\" name=\"h001\">".$numberthree."</br>";
            break;
        }}}

don't use \" before name.

Upvotes: 0

Dalton D
Dalton D

Reputation: 111

Take a look at this page Only allowing selection of one radio button in a form with PHP

Try adding the 'for' attribute as described in the answer on that page.

Upvotes: 0

grateful
grateful

Reputation: 1128

You dont need the escaped quotes here: \"name= try with just name=

Upvotes: 1

Related Questions