Reputation: 3
beginning programmer here so my apologies if this is a waste of your time.
I've made an HTML form which looks like this:
<form action="post.php" method="post">
<label for="artist">Artist:</label><br>
<input title="" type="text" name="artist" id="artist" value="Muse" required><br>
<label for="album">Album:</label><br>
<input title="" type="text" name="album" id="album" value="Live At Rome Olympic Stadium" required><br>
<label for="genre">Genre:</label><br>
<input title="" type="text" name="genre" id="genre" value="Rock" required><br>
<label for="year">Year:</label><br>
<input title="" type="number" name="year" id="year" value="2013" required><br>
<label for="tracks">Tracks:</label><br>
<input title="" type="number" name="tracks" id="tracks" value="13" required><br><br>
<input title="" type="submit" name="submit" value="Submit">
</form>
The submitted information is sent to post.php, which looks like this:
if ($_POST['artist'] ['album'] ['genre'] ['year'] ['tracks']) {
echo "The artist name is " . $_POST['artist'];
echo "<br>";
echo "The album name is " . $_POST['album'];
echo "<br>";
echo "The genre is " . $_POST['genre'];
echo "<br>";
echo "The year of release is " . $_POST['year'];
echo "<br>";
echo "The number of tracks is " . $_POST['tracks'];
echo "<br>";
} else {
echo "You have not filled out all the forms";
}
(I'm not sure wether or not to use isset - but that's a different matter)
According to my editor, PHPStorm, there's no errors in the formatting of the parameters in $_POST however my browser gives me the following errors:
Warning: Illegal string offset 'album' in C:\xampp\htdocs\php1\week2test\week2music\post.php on line 3
Warning: Illegal string offset 'genre' in C:\xampp\htdocs\php1\week2test\week2music\post.php on line 3
Warning: Illegal string offset 'year' in C:\xampp\htdocs\php1\week2test\week2music\post.php on line 3
Warning: Illegal string offset 'tracks' in C:\xampp\htdocs\php1\week2test\week2music\post.php on line 3
How am I supposed to format my parameters in $_POST to rid these errors? Or if that's not the problem, then I don't know what the problem is, do you have some insight?
Should I be using isset in this scenario?
Thanks in advance.
Upvotes: 0
Views: 3045
Reputation: 15247
if ($_POST['artist'] ['album'] ['genre'] ['year'] ['tracks'])
this doesn't exist in your current context, this will be a 5 depth array.
You meant if ($_POST['artist'] && $_POST['album'] && $_POST['genre'] && $_POST['year'] && $_POST['tracks'])
Or, even better, using isset()
: if (isset($_POST['artist']) && isset($_POST['album']) && isset($_POST['genre']) && isset($_POST['year']) && isset($_POST['tracks']))
But I prefer using a sexier solution, storing in an array the keys of the $_POST
I want to check, and then, loop over that array :
$KeysToCheck = array('artist', 'album', 'genre', 'year', 'tracks');
$AllFieldsAreGood = true;
foreach($KeysToCheck as $KeyToCheck)
if (!isset($_POST[$KeyToCheck]))
$AllFieldsAreGood = false;
if (!$AllFieldsAreGood)
echo "You have not filled out all the forms";
else
{
// success ! Manage it.
}
Take note that using isset()
on a checkbox will be true
only if the box is checked.
Upvotes: 3
Reputation: 3450
isset()
is used to check that variable exists and it's not null.
Hence it's always preferred to use it. In your case it can be used like:
if (isset($_POST['artist']) && isset($_POST['album']) && isset($_POST['genre']) && isset($_POST['year']) && isset($_POST['tracks'])) {
echo "The artist name is " . $_POST['artist'];
echo "<br>";
echo "The album name is " . $_POST['album'];
echo "<br>";
echo "The genre is " . $_POST['genre'];
echo "<br>";
echo "The year of release is " . $_POST['year'];
echo "<br>";
echo "The number of tracks is " . $_POST['tracks'];
echo "<br>";
} else {
echo "You have not filled out all the forms";
}
Upvotes: 0
Reputation: 113
Using isset is better if you want to use non-NULL values received from POST
if (isset($_POST['artist'], $_POST['album'], $_POST['genre'], $_POST['year'], $_POST['tracks']))
Upvotes: 1