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.
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.
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.
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!
I am creating a site in PHP while storing my date in a MySQL database. I have already created my sign-up, login, logout portions, but I would like to make things more user friendly and add an area where people can change their user settings. Some of the settings that I would like for the user to be able to modify are as follows:
full name
email
age
gender
etc.
That being said, I would like for them to be able to fill out only the portions of a form that they would like to update. They should be able to leave everything else unchanged and submit all of their changes with a single submit button.
Any suggestions as to the best way to approach this problem are greatly appreciated.
As a side, I would eventually like for this site to contain AJAX (where the user might be able to select individual settings and change them at will), so if your solutions take that into consideration, that would be great.
EDIT:
Sorry, but I should have mentioned that I want to keep the information from being shown to the user (i.e. displayed in the text field) unless they explicitly type in there. As far as I can tell, this would keep me from always posting all of the data every time.
I have a great way of achieving this. Just simply do some if/else coding in php. Like this--
Html Code:
<form action="settings.php" method="POST">
<input type="text" name="full name" />
<input type="text" name="email" />
........ (and more)
</form>
And PHP code ---
<?php
if($_POST)
{
if(isset($_POST['full name'])) { //Update full name of user and redirect to the settings page on success. }
else { //Redirect and show errors! }
if(isset($_POST['email'])) { //Update email of user and redirect to the settings page on success. }
else { //Redirect and show errors! }
}
?>
Or you can use array function of PHP to set the MySql queries in it like ---
<?php
mysql_query("
UPDATE table name SET
//Loading different values from the array using foreach() php function.
");
?>
Just try to do some modifications in it.
Hope this helps you.
Your choices are:
1) multi-stage edit process. 1. pick fields to change. 2. present fields for editing. 3. save updated data. Rather tedious
2) present all the fields in a form, but use some Javascript to keep track of which ones were changed and submit only those
3). present all the fields in a form, and update all the fields in the database. if one or more weren't changed, the update is a null-operation anyways.
3)'s easiest to implement. 2)'s dependent on javascript. 1)'s tedious for you and even more tedious for the user.
As long as you do proper validation on all the fields, I don't see how #3 is anything but the most logical choice.
Create a edit.php or something else. Create textfields which one you want to edit.
All information could be showed thats related with the unique id of a user.
input type='text' value='"Example: Select * from users WHERE id = '$userid'"' name'Example: Update_name'
Do this for all the field you want to edit. After creating this edit.php. Use a script to update the $_POST or $_GET user details. Based on this easy to use script you've got a edit function for a user.