Reputation: 11
I'm trying to get a submit button working with some JavaScript functionality but cannot seem to get the right results.
On the page heating-pull.php I have a schedule button which when clicked prompts the user to write a day of the week. When clicked it opens up a prompt box.
function show_prompt()
{
var day=prompt("For Which Day?");
var startTime=prompt("The Start Time?");
var endTime=prompt("The End Time?");
if (day=="Monday" || "Tuesday"
|| "Wednesday"
|| "Thursday"
|| "Friday"
|| "Saturday"
|| "Sunday")
{
alert("Your Schedule Has Been Set");
} else
alert("Scheduling Error, Try Again.")
window.location ="heating-pull.php";
}
If they write a correct day of the week I want them to go to the page where I'm sending them (schedule.php), if they do not enter a correct day I want to reload the page and not let them past.
New to JavaScript and I don't know whats wrong even though I've looked for the answers in this place. Any help would be great. Cheers.
Upvotes: 1
Views: 55
Reputation: 13600
Some explanations first.
When using a if
statement, the ||
doesn't mean something like this:
x is equal to 5 or 6 or 7 or 8 or 9
Where x would be one of them. The ||
is used to separates conditions:
condition1 or condititon2 or contition3
condition1 || condition2 || condititon3
In your code you have the first condition right:
day=="Monday" = condition1
"Tuesday" = condition2
...
"Sunday" = conditionN
In javascripts Strings
only empty strings evaluate to false. So we can rewrite your condition like this:
day=="Monday" || true || true || true || true ...
If you want to use &&
and ||
, you'll have to have conditions between them. But sometimes it's worth using data structures to find something.
In your cases you could have something like
// Make this as global as possible but not in window obviously
DaysOfTheWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
// Freeze the object so nothing can modify it later... you wouldn't want to end up
// with "Caturday" instead of "Sunday"
Object.freeze(DaysOfTheWeek);
function show_prompt()
{
var day=prompt("For Which Day?");
var startTime=prompt("The Start Time?");
var endTime=prompt("The End Time?");
if(DaysOfTheWeek.indexOf(day) >= 0) {
// more cod goes here
window.location = "schedule.php";
} else {
window.location.href = window.location.href;
}
}
Upvotes: 0
Reputation: 133403
Your condition in the if statement is not correct. Use this instead
Use
if (day == "Monday" || day == "Tuesday" || day == "Wednesday" || day == "Thursday" || day == "Friday" || day == "Saturday" || day == "Sunday") {
//Do something
};
OR
if (["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"].indexOf(day) > -1) {
//Do something
};
As per comment I want the redirect to refresh the page so the user has to fill out the form again.
You should use
window.location.href = window.location.href;
OR
document.location.reload(true);
The Location.reload() method Reloads the resource from the current URL.
Complete Code
function show_prompt() {
var day = prompt("For Which Day?");
var startTime = prompt("The Start Time?");
var endTime = prompt("The End Time?");
if (day == "Monday" ||
day == "Tuesday" ||
day == "Wednesday" ||
day == "Thursday" ||
day == "Friday" ||
day == "Saturday" ||
day == "Sunday"
) {
alert("Your Schedule Has Been Set");
window.location.href = window.location.href;
} else {
alert("Scheduling Error, Try Again.")
window.location = "heating-pull.php";
}
}
Upvotes: 2