Page 1 of 1

Rookie problem in learning PHP

Posted: Tue Sep 28, 2010 11:37 am
by Wickings
Hi guys!

Short intro: I study at a university in denmark, and this semester includes PHP coding. So i've only been working with php for a couple of weeks, and I still run in to basic, stupid problems, as u will probably see, hehe.

Anyway, i come to you asking what mistake i made in the following code:

<?php
$day = range(1, 31);
foreach ($day as $value) {
echo "<option value=\"$value\"";
if ($_POST['day'] == $value) {
echo "selected=\"selected\"";
}
echo ">$value</option>\n";
}

?>

Now, what i'm trying to do: I'm currently working on a reference form for people to fill out on a web page. In this form they should enter their date of birth. And with the code above, as you might see, i'm trying to make the array sticky. However, nomatter what i try, it gives me an error when i test the site (using WAMP server).

Can anyone of you correct my code and shed light over what i did wrong? tried multiple things, nothing worked. :?

Also, since our teacher is not really any good at teaching, could someone explain me what "selected=\"selected\"" does? She sent out the command in a mail with the comment "just copy paste my code"...

Many thanks!

Re: Rookie problem in learning PHP

Posted: Tue Sep 28, 2010 11:43 am
by Jonah Bron

Code: Select all

<?php
$day = range(1, 31);
foreach ($day as $value) {
    echo '<option value="' . $value . '"';
    if ($_POST['day'] == $value) {
        echo ' selected="selected"';
    }
    echo '>' . $value . '</option>\n';
}
?> 
It looked okay, but try the above. 'selected="selected"' sets that item in the menu as the default option.

Re: Rookie problem in learning PHP

Posted: Tue Sep 28, 2010 11:57 am
by Wickings
Hmm unfortunately that doesn't work either. I copypasted it to replace my current code, that was what you meant, right? :P

Could the error in my original code be that i at some point mix up the variables that i tell it to display?

Re: Rookie problem in learning PHP

Posted: Tue Sep 28, 2010 11:59 am
by Jonah Bron
Oh, you have an error? What is it? Errors have a reason for being there, and the always provide useful information. If you are asking for help with a problem, post errors.

Re: Rookie problem in learning PHP

Posted: Tue Sep 28, 2010 12:16 pm
by Wickings
well, its my browser that displays the error:
Parse error: parse error in C:\wamp\www\test\MP3_case_2010\make_references.php on line 247

and since i end the </html> in line 247, i guess its a syntax error somewhere. the script itself is in line 166 of the document

Re: Rookie problem in learning PHP

Posted: Tue Sep 28, 2010 12:22 pm
by twinedev
When you get an error that reports on the last line, especially when that line isn't a PHP line, the first thing to look for is mismatched curly braces { }, or possibly forgotten closing php tag ( ?> ) before going back to HTML.

(ie. you opened one to start an if or some type of loop, and forgot to close it).

Also, the selected="selected" code will tell the select box which option to have selected by default when the page loads.

-Greg

Re: Rookie problem in learning PHP

Posted: Tue Sep 28, 2010 12:30 pm
by Wickings
Thanks guys:P i actually found the syntax error ( expendable { in line 79, d'oh)

however, now it gives me this when i select the menu:

Image

any suggestions?

also thanks for all ur patience =)

Re: Rookie problem in learning PHP

Posted: Tue Sep 28, 2010 12:49 pm
by Jonah Bron
$_POST['day'] should be getting the input element "day" from the previous page. Are you sure you submitted form data to the page?

Re: Rookie problem in learning PHP

Posted: Tue Sep 28, 2010 2:00 pm
by twinedev
Try this:

Code: Select all

if (isset($_POST['day']) && $_POST['day'] == $value) {

Re: Rookie problem in learning PHP

Posted: Tue Sep 28, 2010 3:30 pm
by McInfo
It's unnecessary to check 31 times whether the variable is set. Move isset() out of the loop. Initialize another variable outside the loop and use that to store the posted value if there happens to be one; then use the new variable in the comparison within the loop. Otherwise, keep $_POST['day'] inside the loop, but outside the loop, if $_POST['day'] is not set, initialize it to something that won't be matched with any of the values within the loop.