I'm trying to create an online treasure hunt game where users have to go to specific sites to get the clues and enter it on a website. There are 7 questions whereby upon getting a correct answer, the user will see the next question and clue.
So far this is my PHP code.
<?php
//task 1
if(strtolower($_POST["task1"]) == "hello") {
?>
Here's your next clue!
<p><form action="quiz2.php" method="post">
<input type="text" name="task2" />
<input type="submit" />
</form>
<?php
} else {
?>
Wrong!
<p><form action="quiz.php" method="post">
<input type="text" name="task1" />
<input type="submit" />
</form>
<?php
}
?>
I know how to do it in multiple pages but I would like it if I can do it in one PHP page itself. The reason being, users can just view source the page and just skip to the next question/clue without answering.
How do I achieve this in one PHP page?
There are a lot of ways to handle this. My preference?
Allow users to define a unique id (fun name or something) to track progress.
Have a small db store ids and progress.
Use AJAX to post and retrieve data.
This approach would require one HTML page with AJAX methods, one PHP controller that accepts the unique id and and answer. If the answer is correct, the PHP controller records the status to the DB allowing the controller to return the next clue. If a user logs off the page and comes back, they just have to put in their ID and pick up where they left off. This is a fine solution if security isn't an issue, and it doesn't sound like it is.
Happy coding!
Related
This isn't so much a programming question so much as me just trying to wrap my head around whats going on behind the scenes.
When a user submits content via a php form what exactly happens?
I'm trying to understand how exactly submission data from a form gets saved so that its their for others to see. Take reddit (or even this site) for example, users fill out a form hit submit and their post is there for everyone to see forever. Does this happen no matter what? or is there more code that needs to be added. If it does happen automatically, is the data lost if the server goes offline? how is that data deleted? etc etc
Thanks for the help in advance
EDIT: I guess a better question is how exactly does POST work.
This is a POST example. Please try to find a tutorial to learn PHP.
<form method="POST">
<input type="text" name="yourName">
<input type="submit" value="Submit your name">
</form>
<?php
// This is a PHP comment
// We check if the POST is made
if(isset($_POST['yourName'])){
// We store the POST-variable in a usual variable
$variableWithName = $_POST['yourName'];
// Print out the name
echo "Your name is ".$variableWithName;
}
Okay, I've looked for a solution to this question, but to no avail. It could be the way I worded it, but I've tried almost everything I could think of. And I know this is probably something so simple, I just can't wrap my head around it.
The problem I'm having is this:
I have a form that is prepopulated with data from a MySQL database. This part of the form is strictly for viewing the records (that were previously entered by the user). I can get it to work; I have the database linked, and it shows the data for the row of the particular table that I want. But this is what I'm having trouble with.
I want there to be a button that the user can press that cycles through each row of data in the database, and outputs it in the form accordingly.
It's a little complicated, so I'll use a simple example.
Let's say I have a basic table in a MySQL database with three columns: ID, Name,
and EmailAddress.
Here's the query that grabs the data:
$sql = "SELECT * from tbl_Users ORDER BY ID ASC";
$query = mysql_query($sql);
$result = mysql_fetch_array($query);
Now I know this is deprecated, but I am working in an older version of php, and I'm trying to stay consistent other pages/apps in this particular domain.
Now let's say I have a simple form with two inputs and a submit button.
HTML
<form name="button" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="Name" value="<?php echo $row[Name]; ?>" />
<input type="text" name="emailAddress" value="<?php echo $row[EmailAddress]; ?>" />
<input type="submit" name="submit" value="Next Record" />
</form>
Now I can get it to echo what I need in the form. It can prepopulate the form with a row, as I want it to. The trouble lies with the submit button.
When the submit button is clicked, I want the next row to populate in the form, and so on until the end of the records.
I've tried the if(isset($_POST['submit']{...}, yet I don't know exactly to get this to work. I've tried loops, etc. I know there is a way, I just cannot comprehend one right now. Thanks in advanced.
Okay, so I DID manage to get it to work with PHP. I found a simple PHP Pagination script and changed the limit of the query to 1. Instead of echoing out the results of the query in the WHILE loop, I just called the variables for the form
Unfortunately in your case, PHP is server side, and it will run any queries before the client side has a chance to catch up and output it. I believe you will need to use javascript for this.
You can do one of two things, 1. Pull all table information on load (slower way) and use javascript to show/hide certain elements of that query. Or 2. You can use AJAX to pull the data on command.
I would suggest learning javascript (e.g. use a framework like jQuery) to perform an AJAX call for this.
I am a little frustrated, but trying to work through this in my free time. What I want is to create a blog page for my daughter and I am a bit of a beginner myself when it comes to web design, although I do know css and html like the back of my hand. My question is with php and or mySQL what I want is to have my daughter write her blog/story (she is 9) on one generic page for submitting text that I've written to a spot on her page on our family website, still in the making. So I have learned how to post something, but not to a target space on a specific page. I assume that you use an anchor tag on her page rose.html and getelmentbyID or post? I'm now confused. I can look up the connection sequence for php if someone doesn't want to explain all that to me, but bear with me, while I have some experience with php and mysql I am still a little green behind the ears. Anything would help.
You'd be MUCH better off using something like WordPress - if you know HTML/CSS well you can customize it to be very useful to you.
It's dead simple to install and theirs a million guides out their for you to use.
http://wordpress.org/
Give it a shot, it'll make you life a lot easier.
Theming WordPress:
http://webdesignerwall.com/tutorials/building-custom-wordpress-theme
Say your dynamically posting a p message to a div from submitting words in a text box named "entry". You would just insert the php in the middle of the div where you want to see it. For example
<div>
<!--PHP HERE-->
<?php
$post = $_POST['entry'];
echo '<p>'.$post.'</p>';
?>
<!--END PHP-->
</div>
<!--Area you enter message to post-->
<form method="post" action="<!--PUT NAME OF FILE HERE-->">
<textarea name="entry" cols="40" rows="5">
Enter your comments here...
</textarea><br>
<input type="submit" value="Submit" />
</form>
So right now what I am trying is to randomly get a question from a set of questions in a table on the database and display them on the form for the person to answer. if that is a bad thing to do please let me know how I should go about it instead. I guess it leaves an easy way to abuse the database, perhaps? I'm not sure.
Well, right now I have the form and the form processing on the same page. The problem is that when it is submitted, it of course refreshes and changes the question, which means unless they get lucky, the answer is wrong. I am not sure how to go about setting it up so that I can generate the random question and make sure when they submit the form that it works out correctly without changing the answer. i have form validation with javascript set up, but that won't help against bots.
So basically, what I have is
check stuff, connect to databse, etc
$random = mysql_fetch_row(mysql_query("select * from questions order by rand() limit 1"));
clean submitted data, check, submit, etc.
In the form, I have
General form stuff, name, email, etc
<label for="question"><?php echo $random_row[1]; ?></label>
<input type="text" name="question" id="question" required="required"/>
So you can see what I am wanting to do, if that wasn't clear before.
Your effort seems perfect.
You need to store question id in session because when you submiting form data in next page you require question which was asked.
So you will get question detail by question id store in session and compare answer on submitted page.
Hope this may help.
If you really want to handle the input and the submission with the same page, you're going to end up with a structure like this.
if (!empty($_POST))
{
// there is $_POST data, so there must be a submission
}
else
{
// there is no $_POST data, so the user needs to input
}
To know which question the user is answering in the submission, add a hidden input to the form.
<form ...>
<input type="hidden" name="question_id" value="<?php echo $question_id; ?>" />
<input type="text" name="answer" />
</form>
When receiving the submission, you can use the question_id to lookup the correct answer in the database. Compare the correct answer to the answer the user gave.
Please ask more specifically if you require detail about a particular part.
So I am making a small quiz app that shows one question at a time, in order to move on to the next question you have to answer the current one correctly, and you have as many tries as it takes you...The quiz works perfectly. When you have answered the last question correctly, a form is displayed and it asks you to fill out some information so that the "quizzer" can send you a gift...The submission of this form is done via php, I did plan on implement JQuery/AJAX but right now I am just testing out the DB connectivity and other functions so i kept it simple...the problem I am having is that when I hit submit the page automatically gets refreshed showing the first question again and not outputting anything from my php script..I am not really sure what kind of code I could include to help anyone solve this, as I think it is a more theoretical problem, but let me know if you want me to post more...
Thanks in advance for any help, it is greatly appreciated!
<form action"/index.php" method="POST">
Congratulations! You passed! Please enter in your email address and we will send you something cool!<br />
<input type="text" name="email" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
If the page submits and posts back to itself, the form is going to reset by design. You have to add code that detects the postback and if "True" then repopulate the form with the postback values to give the appearance of data persistance.
Havne't used PHP in a long time, but in .net you would detect a postback by calling:
....If IsPostBack Then....
When you do a postback, the entire page reloads, so that is going to fire your Javascript code again.
To avoid that, you should encapsulate just the form with AJAX to submit just the form so that the rest of the page doesn't post back (including the javascript).
Hope this gives you some direction.
Or... you can change
to post to a different file (like /index2.php) and that way you don't have to worry about postbacks or stuff like that.
Did you check the post data on the same index.php page, like doing:
if(isset($_POST) AND !empty($_POST)) {
var_dump($_POST);
}
This might help you in some way.
I am having the same issue, but with jQuery Mobile. I have implemented this on the form submit but all I need to do refresh the query results:
onClick="window.location.reload(true)"
This is basically a page refresh more than anything but might point you on the direction you need as well.