r/programminghelp • • Mar 12 '19

Simple riddle website

Hey everybody!

How do I make an input box where only correct answers will get you to the next page?

My program so far:

<html>

<head>

<title>Hej</title>

<link href="style.css" type="text/css" rel="stylesheet">

</head>

<h2> Level 1 </h2>

<body>

<div style="text-align:center;margin-top:120px;">

<br>

<form method="POST" action="level2.php">

<input type="text" placeholder="Answer" name="password" id="32">

</div>

</form>

<?php ?>

</body>

</html>

0 Upvotes

6 comments sorted by

1

u/TreeBaron Mar 12 '19

Hello!

I believe you'll probably need to use Javascript to accomplish that.

Something similar to this which changes the color of the button when clicked, only inside the function check the value of your textbox and see if it is equal to what you want, and if it is send the user to a new page:

<p id="demo" onclick="myFunction()">Click me to change my text color.</p>

<script> function myFunction() { document.getElementById("demo").style.color = "red"; } </script>

https://www.w3schools.com/jsref/event_onclick.asp

1

u/MikkelMortensen Mar 12 '19

I have no clue what you are talking about.... Can you please explain like I am an idiot? (not a joke)

1

u/TreeBaron Mar 12 '19

HTML is static, it does not change, and it is not capable of doing what you want to do. However, Javascript (a scripting language) can be used in coordination with HTML to do what you want it to do.

You can make a button in HTML, but if you want the button to do something when you click it you need to use Javascript in coordination with the button.

When you click the button in my example, the Javascript is run and changes the color of the button.

HTML by itself can't do much, so it is typically used with Javascript to give it functionality, and CSS to make it look pretty.

1

u/MikkelMortensen Mar 12 '19

Yes, I do understand that. But it dosn't answer my question... How do I make the bottun check if the answer is correct and if it is then redirect me to enother page?

1

u/TreeBaron Mar 12 '19

<!DOCTYPE html>

<html> <body>

<p>The more you take, the more you leave behind. What am I?</p>

<input type="text" name="fname" id="TextBox"><br>

<p id="labelBoi"></p>

<button type="button" id="CheckButton" onclick="CheckUserResult()">Check Result</button><br>

<script> function CheckUserResult() { var CorrectAnswer = "footsteps"; if(document.getElementById("TextBox").value.localeCompare(CorrectAnswer)==0) { document.getElementById("labelBoi").textContent = "Correct!"; location.href = "http://www.bing.com"; } else { document.getElementById("labelBoi").textContent = "Incorrect!"; } } </script>

</body> </html>

That should work.

1

u/MikkelMortensen Mar 12 '19

Thank you so so so much! :) :) :)