Beginners PHP 2:PHP POST Part 2/2

6 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 2/2
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.
<?php

$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

Re: Beginners PHP 2:PHP POST Part 2/2
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 ?
http://vagex.com/?ref=25000
User avatar
Alex
Member
Member
Posts: 40
Joined: Mon Feb 01, 2010 8:17 pm

Re: Beginners PHP 2:PHP POST Part 2/2
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.)
<?php

$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

Re: Beginners PHP 2:PHP POST Part 2/2
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
http://vagex.com/?ref=25000
User avatar
Codex
Coding God
Coding God
Posts: 2028
Joined: Wed Mar 31, 2010 5:50 pm

Re: Beginners PHP 2:PHP POST Part 2/2
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
We shall let the revolution begin.. the revolution for freedom, freedom against censorship. We shall fight in the fields and in the streets, we shall fight in the hills; we shall never surrender
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

Re: Beginners PHP 2:PHP POST Part 2/2
Axel
ok ty for reply i will surely try it when i found a name XD
http://vagex.com/?ref=25000
6 posts Page 1 of 1
Return to “Tutorials”