I have three questions about some code :
- How does
isset($_POST['submit'])knows when the submit button has been clicked. I dont get the the mechanics behind this ? - Why is each input element using a name with a value of
artist[]with the square brackets like that ? Are we defining and array inside HTML ? - I was told that the value of name given to an input element has to match the name given to the
$_POSTvariable. But in this case$_POSTis usingartistwith without the square brackets. Whats going on here ?
.
<html>
<head></head>
<body>
<?php
// check for submit
if (!isset($_POST['submit'])) {
// and display form
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="checkbox" name="artist[]" value="Bon Jovi">Bon Jovi
<input type="checkbox" name="artist[]" value="N'Sync">N'Sync
<input type="checkbox" name="artist" value="Boyzone">Boyzone
<input type="checkbox" name="artist" value="Britney Spears">Britney Spears
<input type="checkbox" name="artist" value="Jethro Tull">Jethro Tull
<input type="checkbox" name="artist" value="Crosby, Stills & Nash">Crosby, Stills & Nash
<input type="submit" name="submit" value="Select">
</form>
<?php
} else {
// or display the selected artists
// use a foreach loop to read and display array elements
if (is_array($_POST['artist'])) {
echo 'You selected: <br />';
foreach ($_POST['artist'] as $a) {
echo "<i>$a</i><br />";
}
} else {
echo 'Nothing selected';
}
}
?>
</body>
</html>
3 Answers 3
1.How does isset($_POST['submit']) know when the submit button has been clicked.
Clicking the submit button causes the browser to submit the form (and include the name/value of the clicked submit button in the data).
Submitting the form creates an HTTP request.
The PHP won't run except as a reaction to the server receiving an HTTP request.
The PHP examines the data to see if the submit buttons name/value in in it.
- Why is each input element using a name with a value of "artist[]" with the square brackets like that?
Most form processing libraries will provide a way to access multiple elements with the same name as an array. PHP's requires that the name ends in [] before allowing that.
I was told that the value of name given to an input element has to match the name given to the $_POST variable. But in this case $_POST is using "artist" with without the square brackets. Whats going on here?
PHP's form data parser treats [] as a message to create an array and not as part of the name. It is odd.
It does however, allow you to define such things as:
<input type="checkbox" name="foo[bar][]" value="1">
<input type="checkbox" name="foo[bar][]" value="1">
<input type="checkbox" name="foo[bar][]" value="1">
<input type="checkbox" name="foo[oat][]" value="1">
<input type="checkbox" name="foo[oat][]" value="2">
<input type="checkbox" name="foo[oat][]" value="3">
And then loop over $_POST['foo'] to get an array of two items which can be looped over in turn.
1 Comment
$_POST['submit']is set when the form is submitted and a input field with name "submit" exists. In your case, the<input type='submit'>has a name of "submit". (You can also get the value of the submit button using $_POST['submit'] which holds the value).The names have artist[] because it declares an array to be passed as the form is processed. You can use it to group values together. Usually, it's done by
artist[name],artist[album]etc. which can the be referenced in PHP using_POST['artist']['name'].Sort of follows from 2. Using empty square brackets makes an array without associations. You'd reference the first field as
$_POST['artist'][0]and next$_POST['artist'][1].
When all fields have been given the same name, an array is built and you can reference it in PHP like 3.
Comments
The submit button actually submits the form and it's a POST value on it's own. So if it's set it means that the form was submitted. Though, it would be better to check if
$_SERVER['REQUEST_METHOD']is equal to 'POST'.Yes, it's defining an "array", but it's wrong because in the last 4 checkboxes it doesn't have the square brackets.
$_POST['artist']would contain an array with all the checkboxes values (true or false, depending on wheteher they were checked or not respectively).