Page 1 of 1

Beginners PHP 2:PHP POST Part 2/2

Posted: Wed Nov 17, 2010 9:12 pm
by Alex
This is part 2 of Beginners PHP 2:PHP POST.
This tutorial will focus on creating a 1 page form.
In Part 1, I showed you an example of how to create a form and submit it to another page using the POST method. But what if you want to use only 1 page to submit a form? Like if your page is "register.php" and you want to submit it to "register.php" (The same file). This will require a new global variable set automatically in PHP. The $_SERVER[] method.
The code for getting the current PHP script is:
Code: Select all
$_SERVER['PHP_SELF'];
 
It's getting it's self basically. So when we submit the form, it will go to it's self.
In the previous part of this lesson, we had 2 PHP files. The index.php file and form_submit.php. In this part, we will only need index.php.
Index.php will look like this:
Code: Select all
<?php
if (!isset($_POST['submit'])){
    
?>
<html>
<head>
<TITLE>PHP Post Example</TITLE>
</head>
<body>
<h1>Question yourself!</h1>
<form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" 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>
<?php
}else{
    $question = $_POST['question'];
    $answer = $_POST['answer'];
?>
      <b>Your Question</b>: <?php echo $question; ?>
    <br>
    <b>Your Answer</b>: <?php echo $answer; ?>
    <?php
}
?>
Notice in this code how I changed the form's action to "<?php echo $_SERVER['PHP_SELF']; ?>"?
At the top, we start the PHP and ask PHP if the form has been submitted. The name on the form for this is "submit", or the name of the submit button. It basically asks if the submit button has been pressed, or if you hit the enter key to submit the form.
If it hasn't, "if(!isset()) {" then it will end the php without closing the brackets and paste the HTML of the form. Then we start the PHP again and end the if statement with "}", but instead we used "}else{" which ends the php and starts an else statement in the same if(). Basically, we're saying: "If the form has not been submitted, then display the form. If it has (else) then display the results." We then create the two variables, $question and $answer and set them to $_POST['question']; and $_POST['answer']; and then end the PHP once again, to insert the html that returns as:
Code: Select all
Your Question: What is your favorite color? 
Your Answer: Green	
and end the else statement with "}" and then right after, we end the PHP.
It's really pretty simple. It just combines the two codes into one code and submits to it's self.

Notes:
  • It combines the two codes together and submits the information to it's self.
  • You MUST use the if(!isset($_POST['submit'])) part to do this because if you don't it will display as:
Code: Select all
(The Form)

Your Question:
Your Answer: 
Unless you want to do this, you will need to use the if(!isset($_POST['submit'])).
  • Using an if statement, you do not end what's inside the parenthesis with a ";".
Please post constructive criticism, and you know the rest...

Thanks,
Alex.

Re: Beginners PHP 2:PHP POST Part 2/2

Posted: Sat Nov 20, 2010 9:21 am
by Axel
i will surely use these tutorials if i knew how to use it on a website. coding is one part but then idk how to use it as a website... could you explain it the next time ?

Re: Beginners PHP 2:PHP POST Part 2/2

Posted: Tue Nov 23, 2010 2:22 am
by Alex
YourSocksRoxx wrote:
i will surely use these tutorials if i knew how to use it on a website. coding is one part but then idk how to use it as a website... could you explain it the next time ?
You need a PHP/MySQL host. Such as http://000webhost.com/

(That's the one I use.)

Re: Beginners PHP 2:PHP POST Part 2/2

Posted: Sun Nov 28, 2010 6:30 pm
by Axel
Alex wrote:
YourSocksRoxx wrote:
i will surely use these tutorials if i knew how to use it on a website. coding is one part but then idk how to use it as a website... could you explain it the next time ?
You need a PHP/MySQL host. Such as http://000webhost.com/

(That's the one I use.)
yea but how to use them , upload the php where :s where do i edit the index.php :P

Re: Beginners PHP 2:PHP POST Part 2/2

Posted: Sun Nov 28, 2010 6:51 pm
by Codex
YourSocksRoxx wrote:
Alex wrote:
YourSocksRoxx wrote:
i will surely use these tutorials if i knew how to use it on a website. coding is one part but then idk how to use it as a website... could you explain it the next time ?
You need a PHP/MySQL host. Such as http://000webhost.com/

(That's the one I use.)
yea but how to use them , upload the php where :s where do i edit the index.php :P
You can use any text program like notepad, and then save as filename.php and then using a program like filezilla you can upload the php file to your website.

1) register here. <-- my referral link please :D
2) follow the instructions, like confirm email
3) go to hereand login
4) click FTP Details and remember them.
5) open filezilla and press manage sites.
6) now upload the filename.php to your website, inside public_html

now your done
FileZilla Download = http://filezilla-project.org/download.php?type=client

Re: Beginners PHP 2:PHP POST Part 2/2

Posted: Sun Nov 28, 2010 7:14 pm
by Axel
ok ty for reply i will surely try it when i found a name XD