-1

two problem i have:

first:

i want users see my form with no prechecked in radio buttons but it does when they see it at first time(as you see)(explain that i use checked to show user which radio he/she selected after pushing the button)

second:

why when i name submit button "select sex" and push it in the form, it doesn't echo "it's done" but when i name it "select" it works?! i want my submit name has two words.

and the codes:

<html>
<body>
    <?php
    if(isset($_POST['select sex']))
        echo "it's done";
    ?>
    <form name="input" action="" method="post">
    <input type="radio" name="sex" value="male" checked="
    <?php if(isset($_POST['select sex']) and $_POST['sex']=='male') echo 'checked'; else echo '';?>
    "> Male<br />
    <input type="radio" name="sex" value="female" checked="
    <?php if(isset($_POST['select sex']) and $_POST['sex']=='female') echo 'checked'; else echo '';?>
    "> Female<br />
    <input type="submit" name="select sex" value="Submit" />
    </form> 
</body>

Massih
  • 23
  • 4
  • Remove the `checked` attribute completely, you use it in a wrong way. Please read the documentation! – arkascha Feb 05 '14 at 15:59
  • 1
    names should not have a space – mamdouh alramadan Feb 05 '14 at 15:59
  • 1
    `'sselect sex' !== "select sex"` – Quentin Feb 05 '14 at 16:00
  • @mamdouhalramadan — Why not? – Quentin Feb 05 '14 at 16:00
  • @Quentin check this http://stackoverflow.com/questions/17396037/can-a-html-input-name-contain-spaces-commas-etc – mamdouh alramadan Feb 05 '14 at 16:01
  • @mamdouhalramadan — Only one of the answers there says it is a problem, and that answer is wrong (see my comment on that answer). – Quentin Feb 05 '14 at 16:02
  • @Quentin - thanks for the note, but we can say at least it is a bad practice, because if you are sending a post array with spaced indexes which will not let you turn these indexes to variables in any case. – mamdouh alramadan Feb 05 '14 at 16:06
  • @mamdouhalramadan — Bad practise is letting user input determine variable names. Use an (since we are talking PHP) associative array. – Quentin Feb 05 '14 at 16:09
  • @Quentin, he isn't talking about user input determining variable names--he's talking about developer-determined field names determining variable names. That said, mamdouh, it would be trivial to write code that handles the space before converting it to a variable, and the concern about the spaces is not relevant. – Brian Warshaw Feb 05 '14 at 16:37

1 Answers1

0

For checkbox, not mentioned checked attribute for any of the check box option.

For submit button, normally we are not using the space in field name and this is the best practice. Though you have used then you have written the wrong code. Please check below updated code line for if statement.

 if(isset($_POST['select sex']))
Jignesh Patel
  • 1,028
  • 6
  • 10