Info about PHP Forms - php

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;
}

Related

Victim of Muse, new to programming, don't even know how to describe what I am trying to do in computer or programming terms

thanks in advance...
I am brand spanking new to programming, trying to get my first, very simple, website up off the ground. I understand how to do all the HTML associated with what I am doing (I think anyway), but I have a specific need and I don't even know how to describe it without several sentences...here goes...
I need something similar to what online petition sites use. I need something that lets users input their name, which will then be added to a list of names they see above. Ideally, I would like to have some names already entered in the box before the first user ever comes across the page. I don't care about e-mail confirmation or collecting their e-mail, I just simply want the user to be able to input a name and have it show up on the site as soon as they hit the send button.
I am a fairly quick learner, so if anybody could point me in the right direction, any help would be so greatly appreciated.
<?php
if( isset( $_POST['name'] ) && $_POST['name'] != '' );
{
echo $_POST['name'] . '<br>';
}
?>
<form method="post">
<input name="name" type="text" placeholder="type name" />
<input type="submit">
</form>
This code has zero validation, so a malicious user could input anything. Read up on https://phpro.org/tutorials/Filtering-Data-with-PHP.html

Onclick of link POST data to PHP file

I know this type of question is asked a lot, I've seen quite a few, but none of them seem to answer what I am trying to do, even enough to get me started. I have very little understanding of jQuery, I haven't quite picked it all up (got PHP, though). I want to onclick() of my element as shown below:
<form name="cleartask1" method="POST">
<input type="hidden" name="taskid" value="1">
<input type="checkbox" class='icheck-me' name="check1" data-skin="square" data-color="blue">
</form>
Submit data to a file, we'll say, for this example post.php, but I need it to send the value of all of the form elements when checked (the default for a checkbox) to the post.php file. The post.php file will then pull the data from what was posted from the form. Would I call it in a way as below?
<?php
$value = $_POST['taskid'];
?>
How would I post that to the post.php file? If this doesn't make sense please tell me and I will definitely explain in greater detail. Pretty much I want to post form data on the click of a check box for that specific form.
Notice: The form name and checkbox name are the same to group them together, these tasks will be listed from a MySQL database so grouping them ensures that I submit to the correct one.
Please let me know if you can help, or if you need further information.
UPDATE: I do need to submit this without reloading as this is a live task list, so submitting this without reloading would be best. If it's possible could someone provide an example of jQuery's Ajax to use in order to complete this?
Thanks,
Scott
do you mean something like this? you can use jQuery.submit()
$(".icheck-me").click(function(){
$("form[name='cleartask1']").submit();
});
http://api.jquery.com/submit/
How do I submit the form to another page without reloading, though?
That probably would of made more sense to ask. :) Sorry
as you want to make a post without reloading the page you need to use AJAX
http://en.wikipedia.org/wiki/Ajax_(programming)
and Jquery has a simple way for doing AJAX Request
it would be something like this
$(".icheck-me").click(function(){
$.ajax({
type: "POST",
url: "<address to your page where you want to send your data>",
data: { taskid: $("input[type='taskid']").val()
}
success:function(rsp){
//do anything you want after a successful ajax request
}
});
});
http://api.jquery.com/jQuery.ajax/
Well we want to help you that much but please have a time to study something about ajax first and the the ajax things in jquery.. then if you have find some difficulties of understanding it then ask here again and post what you have done so far =)

Random challenge in a 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.

jquery page reset on php form submission

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.

Using PHP IF ELSE for online treasure hunt

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!

Categories