Beginners PHP 2:PHP POST Part 2/2
6 posts
Page 1 of 1
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:
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:
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:
It's really pretty simple. It just combines the two codes into one code and submits to it's self.
Notes:
Thanks,
Alex.
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
It's getting it's self basically. So when we submit the form, it will go to it's self. $_SERVER['PHP_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
Notice in this code how I changed the form's action to "<?php echo $_SERVER['PHP_SELF']; ?>"?<?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
}
?>
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
and end the else statement with "}" and then right after, we end the PHP. Your Question: What is your favorite color?
Your Answer: Green
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
Unless you want to do this, you will need to use the if(!isset($_POST['submit'])).
(The Form)
Your Question:
Your Answer:
- Using an if statement, you do not end what's inside the parenthesis with a ";".
Thanks,
Alex.
<?php
$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
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 ?
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();
}
?>
$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
Alex wrote:yea but how to use them , upload the php where :s where do i edit the index.php :PYourSocksRoxx 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.)
YourSocksRoxx wrote: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.Alex wrote:yea but how to use them , upload the php where :s where do i edit the index.php :PYourSocksRoxx 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.)
1) register here. <-- my referral link please

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
ok ty for reply i will surely try it when i found a name XD
6 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023