Beginners PHP 2:PHP POST Part 1/2

3 posts Page 1 of 1
Contributors
User avatar
Alex
Member
Member
Posts: 40
Joined: Mon Feb 01, 2010 8:17 pm

Beginners PHP 2:PHP POST Part 1/2
Alex
In part 1 of this series, we learned about the $_GET[] function. (if you did not read it, access it here: viewtopic.php?f=86&t=3747) If you did not already know what echo does, it just displays a string onto the webpage. Anyways, today we're going to learn about the 2nd method used in the <form /> tag, POST!

Quick Explaination of what POST is:
Well, POST is a global variable array that handles server based information transactions. Complex words, simple function. Basically what this nonsense I'm speaking here is that POST cannot be seen in the URL box on your webbrowser, instead, the information is submitted through PHP between files (<form action="" />). It is commonly used for login/register forms. It is more geared for security rather than being openly visible in the URL box like GET. Get is used mainly for search engines. You can use POST for search engines, but it's not recommended because a user can't link you to "google.com/search" and expect that it will search for "cat" because you did not POST it in a forum.

One thing I did not teach you in GET is that what appears in a URL is based on the name of the <input /> tag. Remember in the previous tutorial, we made it say "?num1=&num2=" ? Well, the <input /> tag's name for those were "<input name='num1' />" and "<input name='num2' />".

Onward,

What is the syntax for POST?
The syntax used for this is
Code: Select all
$_POST['fieldname']; 
It's just like GET, only change GET to POST.

Example, Yayy!
We are going to create a simple index.php file that will contain an HTML form, just like the one used in GET, only we'll change the fields around a bit.
index.php:
Code: Select all
<?php
//index.php
?>
<html>
<head>
<TITLE>PHP Post Example</TITLE>
</head>
<body>
<h1>Question yourself!</h1>
<form name="form1" action="form_submit.php" method="POST">
Insert a Question: <br />
<input type="text" name="question" value="" />
<br />
Answer to your question:<br />
<input type="text" name="answer" value="" />
<br />
<input type="submit" name="submit" value="POST it!" />
</form>
</body>
</html>
In this code, we create a form with 3 fields. Question, Answer, and Submit. We also change the method parameter of the <form> tag to "POST" meaning that it will POST this to "form_submit.php"
Here are the three fields:
  • Question: A Text field with the value of nothing. Ask a simple question in here like "What is my favorite color?"
  • Answer: A Text field with the value of nothing. Answer your simple question. e.g: "Green"
  • Submit: Submit the form.
After the first tutorial, I shouldn't have to go into much further explanation on how this forum works.

After this, we need to create our "form_submit.php" file where the magic really happens.
form_submit.php:
Code: Select all
<?php
//form_submit.php
if(!isset($_POST['submit'])){
    echo "You have not filled out the form.";    
}else{
    $question = $_POST['question'];
    $answer = $_POST['answer'];
    ?>
      <b>Your Question</b>: <?php echo $question; ?>
    <br>
    <b>Your Answer</b>: <?php echo $answer; ?>
    <br><br>
    Press your browser's back button to submit another one!
    <?php
}
?>
In this code, we first use an if statement to check if the form has been submitted. We use the isset() function to accomplish this. See the little "!" before "isset("? That is saying, if the form has NOT been submitted. The "!" at the beginning of an if statement is just like saying "if not" in visual basics. So if it has NOT been submitted, then echo "You have not submitted the form." The only way you will get this message is if you type in your browser "yoursite.com/form_submit.php" without submitting the form.

If it has been submitted, then declare 2 new variables.
  • $question: The variable for the question. It's value = $_POST['question'];
  • $answer: The variable for the answer. It's value = $_POST['answer'];
Again, the values for $_POST['value'] are dependent on the <input /> tag's name parameter.

Notice at this part:
Code: Select all
?>
      <b>Your Question</b>: <?php echo $question; ?>
    <br>
    <b>Your Answer</b>: <?php echo $answer; ?>
    <br><br>
    Press your browser's back button to submit another one!
    <?php
}
?>
We end the <?php tag using ?> then enter some HTML code and start PHP again using <?php and then end the if statement using "}" We are basically just echoing what's not in the PHP part of the if statement without using the echo command and it's actually a lot easier to do it that way because you don't have to enter a long code within echo() function. It's the same as saying:
Code: Select all
echo "<b>Your Question:</b>".$question."<br>";
and so on. Makes no difference, just makes it easier.

In part 2 of this article, I will teach you about submitting forums using the same PHP file (Submitting the forum to "index.php" instead of "form_submit.php")
It can make your life and your website visitor's lives easier.
Though, I personally don't use it because it makes code messier.

Notes:
  • POST: submit information without using the URL. Makes things more secure.
Please Remember:
  • Don't flame me if I made a mistake on some spelling error.
  • I appreciate constructive criticism. It helps motivate me to make more tutorials. :)
  • If you have a page consuming question, please PM it to me. Lets not spam the comments. :D
Yours Truly,
Alex.
Last edited by Alex on Wed Nov 17, 2010 12:31 am, edited 1 time in total.
<?php

$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

you can use php code to explain it better
Code: Select all
[code=php]
echo "<b>Your Question:</b>".$question."<br>"; 
[/code]
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
Alex
Member
Member
Posts: 40
Joined: Mon Feb 01, 2010 8:17 pm

Re: Beginners PHP 2:PHP POST Part 1/2
Alex
zachman61 wrote:
you can use php code to explain it better
Code: Select all
[code=php]
echo "<b>Your Question:</b>".$question."<br>"; 
[/code]
:o :o :o !!! I did not know that. Thanks, Zach. :D
<?php

$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
3 posts Page 1 of 1
Return to “Tutorials”