Page 1 of 4 123 ... LastLast
Results 1 to 10 of 40

Thread: Day 7 - Talk To Server - Become PHP Expert in 30 days

  1. #1
    Join Date
    Sep 2003
    Posts
    3,040

    Default Day 7 - Talk To Server - Become PHP Expert in 30 days

    Talk To Server - GET METHOD



    GET and POST methods are used to communicate with web server. Today we will look at GET method.

    day_7_ex_1.php

    PHP Code:
    <?php

    // PHP use Array $_GET to accept input from browser.
    // Lets print_r() value of $_GET array.

    echo '<p>Value of $_GET array is: </p>';

    echo 
    '<pre>';
    print_r($_GET);
    echo 
    '</pre>';
    By default value of $_GET array will be empty.

    Now access the page with link

    Code:
    http://YOUR-WEB-SITE/day_7_ex_1.php
    You will see something like



    This is because No value is passed to server using GET method.

    Lets tell server something by adding a ?name=BIZHAT at the end of the url as follows.

    Code:
    http://YOUR-WEB-SITE/day_7_ex_1.php?name=BIZHAT.COM
    You will see



    Congratulations, you have successfully communicated with web server !

    Replace BIZHAT.COM with your name or another text and experiment.

    You can pass more than one value to server this way, separate NAME=VALUE paid with ampersand (&). For example

    Code:
    http://YOUR-WEB-SITE/day_7_ex_1.php?todo1=Learn&todo2=PHP&todo3=HTML&todo4=Java
    As you can see, $_GET is an Associative Array.

    User Friendly Way To Talk To Web Server

    Most web site visitors don't want to learn how to use GET, so we have to make a nice HTML form page for them to use. So lets make a very simple HTML form page.

    day_7_ex_2.html

    HTML Code:
    <html>
    <body>
    
    <h1>Welcome to my personal web page.</h1>
    
    <p>Please enter you name below and click that button to see my personal page.</p>
    
    <form method="GET" action="day_7_ex_2.php">
        Enter Your Name: <input name="full_name" type="text">
        <button type="submit">Enter Web Site</button>
    </form>
    
    </body>
    </html>
    In this HTML page, we set form method to GET (method="GET"). That means, when you submit form, data will be sent to web server using GET method.

    action="day_7_ex_2.php" - This specify which PHP page will handle data submitted from the HTML form.

    Lets create day_7_ex_2.php

    day_7_ex_2.php

    PHP Code:
    <?php

    echo '<h1>Hello, ' .  $_GET['full_name'] . '! Welcome to my web site.</h1>';

    It is important that you name this file same as specified in HTML form page action.

    Why 2 Files ? Lets Combine

    In previous example, we have one HTML page and a PHP page. Can't we combine both into one single PHP page ? Yes, we can.

    We use isset() function in PHP to see if we have variable full_name passed to us, if not we will just show the HTML page to the visitor, so he can enter his name.

    isset() function check if a variable is present or not.

    day_7_ex_3.php

    PHP Code:
    <?php

    if (isset($_GET['full_name'])) {
        echo 
    '<h1>Hello, ' $_GET['full_name'] . '! Welcome to my web site.</h1>';
    } else {

    echo 
    '
    <html>
    <body>

    <h1>Welcome to my personal web page.</h1>

    <p>Please enter you name below and click that button to see my personal page.</p>

    <form method="GET" action="day_7_ex_3.php">
        Enter Your Name: <input name="full_name" type="text">
        <button type="submit">Enter Web Site</button>
    </form>

    </body>
    </html>'
    ;

    }
    This code use isset() and if() functions in PHP. In HTML form code, we changed action to action="day_7_ex_3.php"



    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  2. #2
    Join Date
    Nov 2009
    Location
    kerala
    Posts
    19,076

    Default News subscription subscription using isset() and $_GET

    http://php.flashwebhost.com/sibichan...bscription.php

    PHP Code:

    <?php

    if (isset($_GET['email'])) {

    echo 
    'Subscription request has been received' ;

    }

    else {

    echo 
    '

    <h1>News Letter Subscription</h1>

    <p>Please enter Email Address below and click that button to subscribe newsletter.</p>

    <form action="subscription.php" method="GET">

    Enter Email Address : <input name="email" type="text">

    <button type="submit"> Subscribe </button>

    </form>

    '
    ;}

  3. #3
    Join Date
    Sep 2003
    Location
    india
    Posts
    11,527

    Default

    http://php.flashwebhost.com/annie/day_7_ex_2.php
    Code:
    <?php
    
    echo '<h1>Hello ' . $_GET['Mariya'] . '!. Welcome to Fashion World. Design, Play, Win!  </h1>';

  4. #4
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by annie View Post
    http://php.flashwebhost.com/annie/day_7_ex_2.php
    Code:
    <?php
    
    echo '<h1>Hello ' . $_GET['Mariya'] . '!. Welcome to Fashion World. Design, Play, Win!  </h1>';

    Day 7 Example 2 contains 2 parts. One HTML page and one PHP page. Read that part again. You need to use HTML page to pass information to this PHP page. Do Example 1 first, so you get the idea and the need for creating HTML page.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  5. #5
    Join Date
    Nov 2009
    Location
    kerala
    Posts
    19,076

    Default

    http://php.flashwebhost.com/sibichan/Day-7/result.php

    PHP Code:

    <?php
    $rollNo1 
    ='Student Name';
    $mark ='Student Mark';
    if (isset(
    $_GET['rollNo1'] )) {
    echo  
    ' Name : ' $rollNo1 '<br>';

    echo  
    ' Mark : ' $mark '<br>';

    echo  
    '<hr> ';

    echo  
    ' result here - passed/failed';

    }
    else {
    echo 
    '
    <h1>SSLC Exam Result</h1>
    <p>Please enter roll No.</p>

    <form action="result.php" method="GET">

    Roll No : <input name="rollNo1" type="text">

    <button type="submit">submit </button>

    </form>
    '
    ;}

  6. #6
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    http://php.flashwebhost.com/sherly/eyedonation.php

    Code:
    <?php
    
    if (isset($_GET['email'])) {
    
    echo 'eye donation request has been received' ;
    
    }
    
    else {
    
    echo '
    
    <h1>Eye Donation</h1>
    
    <p>if you wish to donate  your eyes, enter your name.</p>
    
    <form action="eyedonation.php" method="GET">
    
    Enter Email Address : <input name="email" type="text">
    
    <button type="submit"> Subscribe </button>
    
    </form>
    
    ';}

  7. #7
    Join Date
    Sep 2003
    Location
    india
    Posts
    11,527

    Default

    http://php.flashwebhost.com/annie/health_tips.php
    Code:
    <?php
    if (isset($_GET['email'] )) {
    echo 'Subscribe to our free email updates!';
    } 
    else {
    echo '
    
    <h1> Helpful Tips.</h1>
    
    <p>Please enter your email address below and get Daily Health Tips </p>
    
    <form method="GET" action="health_tips.php">
    
    Enter Your Email Address: <input name= "email" type="text">
    <button type="submit"> Subscribe</botton>
    </form>
    ';
    }

  8. #8
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    http://php.flashwebhost.com/sherly/poor_charity.php

    Code:
    <?php
    if (isset($_GET['email'] )) {
    echo 'poor charity request has been received' ;
    } 
    else {
    echo '
    
    <h1> Poor Charity.</h1>
    
    <p>Please enter your email address below and to help poor charity  </p>
    
    <form method="GET" action="poor_charity.php">
    
    Enter Your Email Address: <input name= "email" type="text">
    <button type="submit"> Subscribe</botton>
    </form>
    ';
    }

  9. #9
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    http://php.flashwebhost.com/sherly/result.php

    Code:
    <?php
    $rollNo1 ='Student Name';
    $mark ='Student Mark';
    if (isset($_GET['rollNo1'] )) {
    echo  ' Name : ' . $rollNo1 . '<br>';
    
    echo  ' Mark : ' . $mark . '<br>';
    
    echo  '<hr> ';
    
    echo  ' result here - passed/failed';
    
    }
    else {
    echo '
    <h1>Medical Entrance Exam Result</h1>
    <p>Please enter roll No.</p>
    
    <form action="result.php" method="GET">
    
    Roll No : <input name="rollNo1" type="text">
    
    <button type="submit">submit </button>
    
    </form>
    ';}

  10. #10
    Join Date
    Nov 2004
    Location
    India
    Posts
    65

    Default

    http://php.flashwebhost.com/stefin/facebook_signin.php

    Code:
    <?php
    
    if (isset($_GET['signin'])) {
    
    echo 'THANK YOU FOR JOINING FACEBOOK';
    
    }
    
    else {
    
    echo '
    
    <html>
    <body>
    
    <h1>Welcome To Facebook</h1>
    
    <h2>Its Free And Always Will Be</h2>
      
    <form action="welcome.php" method="GET">
    
    Enter First Name : <input name="first name" type="text">
    
    </form>
    
    ';}
    
    {
    
    echo '
    
    <form action="welcome.php" method="GET">
    
    Enter Last Name : <input name="last name" type="text">
    
    
    
    </form>
    
    ';}
    
    {
    
    echo '
    
    <form action="welcome.php" method="GET">
    
    Enter Your Email : <input name="email" type="text">
    
    
    </form>
    
    ';}
    
    {
    
    echo '
    
    
    <form action="welcome.php" method="GET">
    
    Re-Enter Your Email : <input name="re-enter email" type="text">
    
    
    
    </form>
    
    ';}
    
    {
    
    echo '
    
    <form action="welcome.php" method="GET">
    
    Enter Your Password : <input name="password" type="text">
    
    
    
    </form>
    
    ';}
    
    {
    
    echo '
    
    <form action="welcome.php" method="GET">
    
    Enter Your Birthday : <input name="birthday" type="text">
    
    <h2><button type="SIGN UP"> SIGN UP </button></h2>
    
    </form>
    
    </body>
    </html>
    
    ';}
    Last edited by stefin; 05-16-2014 at 03:06 PM.

Page 1 of 4 123 ... LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •