If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Hybrid View

[RESOLVED] Return data based on input field...

I'm not sure if this is something JS should do, or PHP... I just know I can't do it with an HTML form...

Here's what I'm trying to do...

I want my user to enter a number. Then, I suppose the JS would validate that number to be between two other numbers. If it's between any two given numbers, then it would return certain information.

For example, if the user enters '2101', then an output script would read, "Based on the score you entered (2101), you will receive approximately $600 as a scholarship, toward your degree program."

I'm just looking for a good starting point. Designing the input form in HTML is simple... but I need help with the JS. I'm just looking for a good starting point, as I'm learning more about JS. I don't want any handouts and I'm not looking for someone to write this for me. Just some guidance!

Basically, the code in the form's onsubmit attribute calls the showScholarship function and then stops the submission of the form by returning false. this.score.value returns the value of the score input in the form. What happens in the showScholarship function is probably pretty self-explanatory.

You could also do something similar with PHP through form submission (which wouldn't require users to have JavaScript enabled), or with a combination of JavaScript and PHP using Ajax techniques to fetch and show data from the server without loading a new page. But the latter might be overkill for what you're trying to do. :)

If a score of zero is entered, then I want the scholarship amount of $5550 to display. If the score is between two integers, then another value will display and so on. But when I try this with any value, I get a scholarship display of $0 every time...

= is not a comparison operator - it's an assignment operator. score = 0 will set score to 0, and in JavaScript that whole assignment expression will also return the value (which in this case is 0). So what happens when you say

if (score = 0)

is that score is set to 0, then the if statement checks if 0 is true. It's not, so the next statement will not execute. The following if statements will also fail because we never check if score is 0. Ultimately, the statement after the last else is executed - setting scholarship to 0.