Skip to content
Advertisement

Using isset() inside a foreach doesn’t work?

Basically, I have an indefinite number of buttons that I want to generate from variables held in the mysql database.

$stmt = $conn->prepare('SELECT * FROM array');
$stmt->execute();
$array = $stmt->fetchAll();

foreach ($array as $var) {
echo '<br>Would you like to accept or reject?';
echo '<br><input name="accept' . $var['subject'] . '" type="submit" value="Accept" /><input name="reject' . $var['subject'] . '" type="submit" value="Reject" />';
}

Now when I try to press the buttons nothing happens. This is the code I thought might work for that:

foreach ($array as $var) {
$accept = 'accept' . $var['subject'];
$reject = 'reject' . $var['subject'];
if (isset($_POST[$accept])) {
//script for accept
}
if (isset($_POST[$reject])) {
//script for reject
}
}

There are no errors. I think it’s because I can’t use isset inside a foreach this way. Is there some other efficient way of doing what I am trying to do here?

Advertisement

Answer

The reason I was getting this error was because I forgot to use <form> tags in the html file.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement