what is wrong with these scripts?
I have two files, wish.php and day.php
wish.php collects the data (day of the week) and day.php should kick a response depending on what you entered. I set it to respond "Get back to work, you lazy bum!", if you enter "Sunday" and, supposedly "You need a vacation..." if you enter something else.
I am getting a responce "Get back to work, you lazy bum!" no matter what I enter.
Can someone point an error out?
here is wish.php
PHP Code:
<html>
<head>
<title>Pick-a-Day</title>
</head>
<body>
<form action="day.php" method=post>
I wish today was:
<input type="text" name="day">
[enter day of the week]
<input type="submit" name="submit" value="Click!">
</form>
</body>
</html>
here is day.php
PHP Code:
<?php
$day = "Sunday";
if ($day == "Sunday") {
echo ("Get back to work, you lazy bum!");
}
else {
echo ("You need a vacation...");
}
?>
Same thing happens if I use wish2 file, which has checkboxes rather then text input field.
maybe I'm reading this wrong, but it looks like you're defining $day in day.php
$day = "Sunday";
if ($day == "Sunday") {
which would meen that $day will be equal to Sunday no matter what the form sends day=Sunday
try removing the line:
$day = "Sunday";
and see if it works right
you also might want to make it ignore case, since a lot of people would type sunday rather than Sunday, however this looks like its just a learning activity, in which case that wouldn't matter
Your suggestion worked on the wish.php (the textfield input), but didn't work on wish2.php (checkbox). Interestingly enough, now it's showing the other message, "You need a vacation".
Edited: just for giggles, I also tried not removing it, but putting
$day = ""; instead, and I got identical bad results with "you need a vacation". The link above has day.php without that line altogether.
I'm trying to understand why it works (or should I say, doesn't) this way?...
Last edited by Daria; 02-05-2004 at 06:23 PM.
Absence of a result is a result, unless defined otherwise.
granted I've never used checkboxes in a form, but this is how I would imagine they work
EDIT:
when learning php, its sometimes better to use GET rather than POST, since you can see what arguments the php script is recieving
as a side note, if both of those checkboxes were checked, it would show as sunday i believe, since its the last thing in your code chronologically, i would suggest using radio buttons for something like that in the future, since you hypothetically wouldn't want someone to wish it was both monday & sunday
With use the GET statement he meant that you should replace POST with GET if you're still learning PHP.
It makes the variables your sending appear in the address bar.
For example the user types "Sunday"
PHP Code:
<form action="day.php" method=POST>
I wish today was:
<input type="text" name="day">
[enter day of the week]
<input type="submit" name="submit" value="Click!">
</form>
shows "http://blablabla/day.php" in the Address Bar.
PHP Code:
<form action="day.php" method=GET>
I wish today was:
<input type="text" name="day">
[enter day of the week]
<input type="submit" name="submit" value="Click!">
</form>
Would give "http://blablabla/day.php?day=Sunday"
This way, you can see the variable and would notice that with the checkboxes it gave day=checkbox instead of day=Sunday. Success with php
one more thing that you should look out for:
you code responds with "Get back to work, you lazy bum!"
If day == "Sunday"
but if the user types "sUnday" "sunday" "SUNDAY" "sUNDAY" " Sunday" "Sunday " etc.. they will stil get the "You need a vacation..." response. PHP is case- and space-sensitive when it comes to strings and variables.
To make the variable case-insensitive use this before the if.
This changes the input " sUNDaY " (with spaces) in three steps.
First step: lowercase everything": " sunday "
Second step: uppercase 1st letter of every word: " Sunday "
Third step: remove leading and trailing spaces: "Sunday"
Tada, your code is now much more friendly (unless the user types Snuday etc.. )
Bookmarks