Post: [TUTORIAL] Echoing Information
05-10-2011, 02:17 AM #1
(adsbygoogle = window.adsbygoogle || []).push({});
Originally posted by 039

Hi, this is a tutorial on some PHP. This will cover 3 different types of echoing,

1. Echo information on the page
2. Echo information off another page ($_POST)
3. Echo information off another page and give a custom URL ($_GET)

Now we will cover the number 1. This is an easy concept to grasp, here is an example:

    
<?php
echo "<h1>I love Download</h1>";
?>


This simple piece of code will display "I love download" in heading 5, PHP can echo HTML too, but be careful having links included you will have to do:

    <a href='#'>Hi</a>


Notice the ' instead of ", these work the same way and vice versa, if you have 2 sets of " they will clash giving you an unexpected result, and most likely not a good one. You can also echo "strings" like this

    
<?php
$string = "PHP is amazing";
echo $string;
?>


Now this will produce a line in HTML saying "PHP is amazing". Very simple, on to number 2.

$_POST
This command basically just grabs information from a form, and well posts it, hence the name $_POST. This is another easy situation that I will post an example:

Form.html (File Name)
    
<form action="you.php" method="post">
Name: <input type="text" name="firstname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>


This will submit information into a form called "you.php", now to retrieve the info.

you.php (File name)
    
Hi <?php echo $_POST["firstname"]; ?>!<br />
Only <?php echo $_POST["age"]; ?> years old?


What this does is retrieve information from the text box named "firstname" the displays it on you.php, and same thing with age except with different variables. Now this will only take you to a URL like this [url]www.yoursite.com/you.php[/url] and if the user wants to see his or her information, they must fill out the form each time, and this is where $_GET comes in handy.

$_GET
To explain this function we will use the same variables as $_POST, but $_GET does all the same stuff as $_POST and more... It displays the information in the URL so a user can go back to an already filled out form and see the exact same information.

Form.html (File name)
    
<form action="you.php" method="get">
Name: <input type="text" name="firstname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>


As you can see, the exact same form as before, except for the method, so all as we do is replace the method with get. Now to get the information.

you.php (File name)
    
Hi <?php echo $_GET["firstname"]; ?>.<br />
Only <?php echo $_GET["age"]; ?> years old?


Now were getting the same variables, but in a different way, so basically the url will look like this now: [url]www.yoursite.com/you.php?firstname=Dan&age=14[/url] . The user now has access to go back and see his filled out form, hope this tutorial helped. :victoire:


source You must login or register to view this content.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo