I have a button on a page that when a user pushes it, it creates another “radio element” on the page. The problem though is that I am having a hard time retrieving the value with PHP after the form is submitted.
Here is my HTML code:
<input type="radio" checked="checked" id="rad-3" title="member-section" name="use_name[0]" value="Test!" />
Here is my PHP code:
if ($_POST['use_name[0]'] == 'Test!') {
echo 'It Worked!' ;
} else {
echo 'Nope!' ;
}
Whenever my form is submitted, Nope! is always echoed out. Any help is appreciated!
PHP will receive
$_POST['use_name']as an array, with the[0]key you specified in the HTML. So you want to test:Debug with
print_r($_POST)to see the structure of the array.Note that if you don’t have a need to send specific array keys in the HTML, you can just name the inputs like
name='use_name[]'and PHP will receive them as a numeric-indexed array. You can be more specific if needed, with string keys too:name='use_name[first_value]'and PHP will receive the string keys as in$_POST['use_name']['first_value']Note however, that this is not a typical way to use radio buttons. Since only one is usually selected, unless you are listing multiple radio buttons (we don’t see your HTML) all with the name
name='use_name[0]the buttons will not be bound as a radio group. They must all have the same name, and different array keys will turn them into different radio groups. This is more commonly used for checkboxes.