ID:277716
 
Code:
//filename - test.php

<?php
$test=$_POST["test"];

if(!isset["post"]))
{
echo '
<html>
<head>
<title></title>
</head>
<body>

<form method="post" action="test.php">
Check1: <input type="checkbox" name="test[]" value="check1"/>
Check2: <input type="checkbox" name="test[]" value="check2"/>
<input type="submit" value="post"/>
</form>

</body>
</html>';
}
else
{
foreach($test as $t)
{
echo $t . "";
}
}

?>


Problem description:
When I run my script the first time, everything is fine. I check my checkboxes and hit submit.
However, after I hit submit, it tells me I called foreach() wrong.
Heres the exact error -

Warning : invalid argument supplied for foreach() in [blah blah blah] on line [whatever line foreach() is called on].

What am I doing wrong?
Thankyou for your help if I don't thank you for a response.

You really shouldn't be posting this here as it is help for BYOND coding problems. But the problem you've got is simple.
You're missing out a single } after foreach.

There is a good reason people use indentation when they are coding and that is because it makes it a lot easier to notice these sorts of problems.

//filename - test.php

<?php
$test=$_POST["test"];

if(!isset["post"])) {
echo '
<html>
<head>
<title></title>
</head>
<body>

<form method="post" action="test.php">
Check1: <input type="checkbox" name="test[]" value="check1"/>
Check2: <input type="checkbox" name="test[]" value="check2"/>
<input type="submit" value="post"/>
</form>

</body>
</html>';
} else {
foreach($test as $t) {
echo $t . "";
}
} //This was missing.
?>

As you can see, the problem would have been much easier to see had you done that in the first place.
In response to The Magic Man
I checked over my code(I'm working on two consoles), and I forgot to add that second } when copying it over to here, so that isn't the problem.
In response to Dead_Demon
It would help if you copied and shown an accurate code, otherwise how are we suppose to find the exact problem? (And by incorrectly copying some code you could be adding tons of errors to it)

Anyway, after testing your code and a quick look. You were using isset wrong.

<?php
$test=$_POST["test"];

if(!isset($test)) {
echo '
<html>
<head>
<title></title>
</head>
<body>

<form method="post" action="test.php">
Check1: <input type="checkbox" name="test[]" value="check1"/>
Check2: <input type="checkbox" name="test[]" value="check2"/>
<input type="submit" value="post"/>
</form>

</body>
</html>';
} else {
foreach($test as $t) {
echo $t . "";
}
}
?>

That works and is correct because I tested it myself.
This is definitely not the right place to post, though. There are PHP help forums, but this is not one of them.
In response to The Magic Man
The Magic Man wrote:
It would help if you copied and shown an accurate code, otherwise how are we suppose to find the exact problem? (And by incorrectly copying some code you could be adding tons of errors to it)

Anyway, after testing your code and a quick look. You were using isset wrong.

<?php
> $test=$_POST["test"];
>
> if(!isset($test)) {
> echo '
> <html>
> <head>
> <title></title>
> </head>
> <body>
>
> <form method="post" action="test.php">
> Check1: <input type="checkbox" name="test[]" value="check1"/>
> Check2: <input type="checkbox" name="test[]" value="check2"/>
> <input type="submit" value="post"/>
> </form>
>
> </body>
> </html>';
> } else {
> foreach($test as $t) {
> echo $t . "";
> }
> }
> ?>

That works and is correct because I tested it myself.

GEEZUS CHRIST!

<?php
$test = $_POST['test'];

if(!isset($test)) {
include('html.htm'); // Honestly why use echo for that, it looks like a mess!
} else {
foreach($test as $t) {
echo $t . "";
}
}
?>
Soccerguy13 wrote:
While you're method is definitely cleaner, please try to watch your language and attitude here. I am certainly no moderator, but I personally don't appreciate language like that on these forums. I apologize to the moderators for posting where it is your job.

Oh no I'm sorry, I don't honestly go off like that. but the %_POST[] was using double qoutes, whitch I'm not sure if even correct.

$_POST['test'] seems right

$_POST["test"] the hell?

ALSO

EDIT:

if(!isset["post"]))

TO:

if(!isset($_POST['test']))

you could also do

if(!empty($_POST['test'])){
In response to Fusioneko
Using double-quotes for array elements is fully acceptable.
In response to Nadrew
Nadrew wrote:
Using double-quotes for array elements is fully acceptable.

I've never seen it, but the usage of his isset and things seem incorrect., maybe it's not practical for double qoutes to be used.. It just doesn'tl look right either. scars my eyes out.
In response to Fusioneko
Fusioneko wrote:
Nadrew wrote:
Using double-quotes for array elements is fully acceptable.

I've never seen it, but the usage of his isset and things seem incorrect., maybe it's not practical for double qoutes to be used.. It just doesn'tl look right either. scars my eyes out.

Fusioneko: At first I thought it was Nadrew who made your comment, and was really confused. I've seen double-quotes in a lot of PHP code, and pretty much always use them myself.
In response to PirateHead
PirateHead wrote:
Fusioneko wrote:
Nadrew wrote:
Using double-quotes for array elements is fully acceptable.

I've never seen it, but the usage of his isset and things seem incorrect., maybe it's not practical for double qoutes to be used.. It just doesn'tl look right either. scars my eyes out.

Fusioneko: At first I thought it was Nadrew who made your comment, and was really confused. I've seen double-quotes in a lot of PHP code, and pretty much always use them myself.


Yeah but I look through all sorts of open source porjects just 5_POST["dahell"] I've never seen.
In response to Fusioneko
Fusioneko wrote:
Yeah but I look through all sorts of open source porjects just 5_POST["dahell"] I've never seen.

Then you obviously haven't seen much PHP source code. It comes down to personal preference, and technique. I, for one, don't care much for escaping double-quotes in strings with embedded variables, so I tend to use single quotes a lot, unless otherwise needed.

And while using includes is cleaner, it's perfectly acceptable to use echo'd HTML output while developing. It makes it simpler to debug, or add debugging logic to it, and it's one less file that you have to hunt through if bugs do crop up. Once things are all shiny you can move stuff out to external files later.

Lastly, speaking as a moderator, you are still cussing (two instances of 'hell' in your last two posts in this thread alone). Knock it off now. You have all the time in the world to clean up your posts before submitting them. There is no excuse.
In response to digitalmouse
digitalmouse wrote:
You have all the time in the world to clean up your posts before submitting them. There is no excuse.

wrong >_> I don't have all the time in the world to clean up my posts. If I had all the time in the world, I'd be immortal, or 'god'. none the less, I am not. When I was refering to the include I was thinking of something entirely different, I could not explain it to you, untill I gain enough social experience to properly express my thought.
In response to Fusioneko
Just so you know. I was only fixing his code, not trying to improve upon it and make it look and work better. And it is obvious the original poster was still learning PHP, so rather than force things onto him that he might not understand I decided to show him what he did wrong and how to fix it.

And, if we are talking about personal preferances. I would have done it like this.
<?php
$test=$_POST['test'];

if(!$test) {
include 'html.htm';
} else {
foreach($test as $t) {
echo "$t";
}
}
?>

But that is just me! (It works, so why complain about it?)
In response to Fusioneko
Use of double-quotes where you don't need some degree of interpretation (variable interpolation, special character business like \n, stuff of that nature) encounters a small performance hit. By small, I mean so small you'd be better spending your time doing loop optimisation if efficiency is a concern.
In response to Nadrew
Nadrew wrote:
Using double-quotes for array elements is fully acceptable.

Even if this were to be true, I'd disagree using them because it'll encourage you to use them in places where they shouldn't be put.

-- Data
In response to Fusioneko
Fusioneko wrote:
digitalmouse wrote:
You have all the time in the world to clean up your posts before submitting them. There is no excuse.

wrong >_> I don't have all the time in the world to clean up my posts.


Still wrong - your 'enter key' finger is NOT wired directly to your brain in such a way that you can't pause for 10-15 seconds to make sure your post conforms to community standards. This is not up for debate. You either agree to discontinue the usage, not post at all, or a mod removes your ability to post.