Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: How to use Session in PHP like ASP

  1. #1
    Join Date
    Mar 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to use Session in PHP like ASP

    Hello,

    I am trying to make a form that will, when submitted, will call "send.php" to process the form fields and then redirect to a "thanks.php" page.

    Issue is, I want the "thanks.php" page to show the values of that form field submitted like Name, Product chosen, order No., Amount etc.

    I have done similar kind of thing in ASP using sessions but unable to do it in PHP.

    I am not using any database here. Just a simple "form mail" which sends the form contents to email & shows a "thanks.php" page for the confirmation or you may say customized Thanks to the user.

    The PHP Code i.e. "send.php" I am using is as under:

    Code:
    <?php
      $nameofproduct = $_REQUEST['nameofproduct'] ;
      $priceusd = $_REQUEST['priceusd'] ;
        
      $nametitle = $_REQUEST['nametitle'] ;
      $name = $_REQUEST['name'] ;
      $address = $_REQUEST['address'] ;
      $city = $_REQUEST['city'] ;
      $district = $_REQUEST['district'] ;
      $state = $_REQUEST['state'] ;
      $country = $_REQUEST['country'] ;
      $pincode = $_REQUEST['pincode'] ;
      
      $telephone = $_REQUEST['telephone'] ;
      $email = $_REQUEST['email'] ;
      $query = $_REQUEST['query'] ;
      
      $uorder = date("dmY-His") ;
      
      $userdetails = $_SERVER['REMOTE_ADDR'];
      $ourid = "Company Name <info@company.com>";
    
      $message = "<h4><font color=blue>$nametitle $name has placed an order. Details are as under</font></h4>
      <br><br><b><u>CONTACT/SHIPPING DETAILS:</u></b>
      <br><b>Name: </b> $title $name
      <br><br><b>Address: </b> $address
      <br><br><b>Telephone: </b> $telephone
      <br><br><b>E-mail ID: </b> $email
      <br><br><b>Any other Information/Question: </b> $query
      <br><br><b>IP Address of Customer is: </b> $userdetails
      <br><br><br><b><u>ORDER DETAILS</u></b>
      <br><b>Order No.: </b> $uorder
      <br><b>Name of Product: </b> PRODUCT NAME
      <br><br><b>Total Amount (in US Dollars): </b> $priceusd ";
      
    
      
      $repmessage = "<table border=0 width=600>
      <tr><td><br>
      <b>Dear $title $name</b>
      <br><br>Thanks for your order. Your order details:
      <br><br>Order No.: $uorder
      <br><br>Name of Product: PRODUCT NAME
      <br><br>Rate Per Unit in US Dollars: $priceusd
      <br><br>Amount in USD: $priceusd
      <br><br><br>
      <br><br>Admin Team
      <br><br>Company Name
      <br>http://www.company.com</tr></td></table>";
    
      $headers = "MIME-Version: 1.0\r\n";
      $headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
      $headers.= "From: $email\r\n";
      
      $repheaders = "MIME-Version: 1.0\r\n";
      $repheaders.= "Content-type: text/html; charset=iso-8859-1\r\n";
      $repheaders.= "From: $ourid\r\n";
    
    
      mail( "info@company.com", "Order No. $uorder received from website",
        $message, $headers );
    
      mail( "To: $email", "Thanks for your shopping at company.com",
        $repmessage, $repheaders );
    
    
      header( "Location: http://www.company.com/thanks.php" );
    ?>
    The above code works just fine to send the form contents to the email. But I am not able to display the form contents in thanks.php page.

    Actually I am neither an ASP programmar nor a PHP programmar as well. But knows how to do this in ASP. PHP is very new for me, thats why unable to do this.

    Any help will be highly appreciated.
    Last edited by paramjeetgemini; 03-26-2009 at 07:54 AM. Reason: Change in Code

  2. #2
    Join Date
    Mar 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    send.php can do the processing and emailing and still display all the items on the page.

    From your codes, you did not 'echo' the submitted values all you did is just put that there.

    They cant just show themselves, you have to print them to the screen. There are many articles to read on php. I a, relatively new in php too but its much easier to deal with than ASP which i also started with.

  3. #3
    Join Date
    Mar 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hello,

    Thanks a lot for your replies.

    Actually my form works like this:

    1. HTML Form: order.htm
    When submitted, it calls send.php to process it.

    2. PHP Form processor Code: send.php
    It processes the form in order.htm and sends the form details to the email and then displays confirmation page i.e. thanks.php

    3. Confirmation Page: thanks.php
    In this I am looking to fetch details of form submitted. The fileds would be Name etc.

    Below is the code of all the above files one by one

    CODE FOR ORDER.HTM
    Code:
    <html>
    
    <head>
    <title>
    Order Form
    </title>
    
    </head>
    
    <body>
    
    <form method="post" action="send.php" name="form1">
    
    <u><b>PRODUCT DETAILS: </b></u>
    </br>
    </br>
    
    <input type="text" name="nameofproduct" size="45" readonly value="PRODUCT NAME">
    </br>
    </br>
    
    
    <b>Price in USD: </b>
    <input type="text" name="priceusd" size="5" value="1" readonly>		
    </br>
    </br>
    </br>
    </br>
    
    
    <b><u>CONTACT DETAILS</b></u>
    </br>
    </br>
    <b>Title: </b>
    <select name="nametitle">
    <option selected value="Mr.">Mr.</option>
    <option value="Ms.">Ms.</option>
    </select>
    </br>
    </br>
    
    <b>Full Name: </b>
    <input type="text" name="name" size="35">
    </br>
    </br>
    
    <b>Address: </b>
    <textarea rows="5" name="address" size="30" cols="35"></textarea>
    </br>
    </br>
    </br>
    </br>
    
    <b><u>Details about City, District, State, Country, PIN/ZIP Code</b></u>
    </br>
    </br>
    
    <b>City: </b>
    <input name="city" size="23">
    </br>
    </br>
    
    <b>District: </b>
    <input name="district" size="23">
    </br>
    </br>
    
    <b>State: </b>
    <input name="state" size="23">
    </br>
    </br>
    
    <b>Country: </b>
    <input name="country" size="23">
    </br>
    </br>
    
    <b>PIN/ZIP Code: </b>
    <input name="pincode" size="23">
    </br>
    </br>
    
    <b>Telephone: </b>
    <input name="telephone" size="35">
    </br>
    </br>
    
    <b>E-mail: </b>
    <input name="email" size="35">
    </br>
    </br>
    
    <b>Any other information/query: </b>
    <textarea rows="5" name="query" size="30" cols="35"></textarea>
    </br>
    </br>
    
    <input type="submit" value="Submit">
    
    </form>
    
    </body>
    
    </html>
    CODE FOR THANKS.PHP

    Code:
    <html>
    
    <head>
    <title>
    Thanks for your order
    </title>
    
    </head>
    
    <body>
    
    Dear <?php echo $_Request['nametitle'] ?> <?php echo $_Request['name'] ?>
    </br></br>
    THANKS FOR YOUR ORDER. 
    
    WE WILL GET BACK TO YOU SOON
    
    </body>
    
    </html>
    Actually I am not aware of the syntax to use in send.php. But I know that in ASP we write e.g. session("name")= name to create a session variable and store the value of form field in it and then we can fetch it using <%=session("productname")%>.

    Hope someone can guide me to a very basic thing in PHP.

  4. #4
    Join Date
    Mar 2009
    Location
    Egypt
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    hello

    if you want to use SESSIONS on PHP language it mean that you need to do this step at first :

    write this function at the first line on all php files includes sessions like :

    PHP Code:
    <?php  session_start() ;

    // ON recieve file .

    $_SESSION[name] = $value ;
    $_SESSION[name2] = $value2 ;
    $_SESSION[name3] = $value3 ;

    //------- OR --------//

    $_SESSION[username] = $_POST[username] ;
    $_SESSION[email] = $_POST[email] ;

    ?>
    NOW YOU HAVE MANY SESSION VARIABLES , AND YOU CAN USE THEM ON ANY FILE INCLUDE THIS FUNCTION ( session_start(); ) ON FIRST LINE ON YOUR SCRIPT FILES .

  5. #5
    Join Date
    Mar 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Dear Ahmed Saleh,

    Thanks for the reply.

    I have tried using session but its not helping and the Thanks Page displays no values (of form submitted) at all.

    I am looking to display the "Values" of Form Submitted in the Thanks Page (the code for these are Order.htm which contains form, send.php which processes this form to send the form results to email and then redirects to Thanks Page thanks.php as mentioned in my previous two posts.).

    Can you plz let me know how to fetch Values of Form e.g. "Name" in the Thanks Page i.e. thanks.php ?

  6. #6
    Join Date
    Mar 2009
    Location
    Chennai, India
    Posts
    77
    Thanks
    16
    Thanked 7 Times in 6 Posts

    Default

    Hi, Make sure that you have these things right:

    The command php session_start() ; should appear before any other html code and it should appear on both the pages....


    You also have to assign all the variables of array $_REQUEST to the array $_SESSION

    for eg:

    $_SESSION['nameofproduct'] = $_REQUEST['nameofproduct'] ;
    $_SESSION['priceusd'] = $_REQUEST['priceusd'] ;


    and in thanks.php, you have to echo the values of $_SESSION['nameofproduct'], $_SESSION['priceusd'] and so on

    Remember, the function session_start() ; should appear before your very first html code on the page.. If there is even one html code before session_start(), the it might not work

  7. #7
    Join Date
    Mar 2009
    Location
    Egypt
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by paramjeetgemini View Post
    Dear Ahmed Saleh,

    Thanks for the reply.

    I have tried using session but its not helping and the Thanks Page displays no values (of form submitted) at all.

    I am looking to display the "Values" of Form Submitted in the Thanks Page (the code for these are Order.htm which contains form, send.php which processes this form to send the form results to email and then redirects to Thanks Page thanks.php as mentioned in my previous two posts.).

    Can you plz let me know how to fetch Values of Form e.g. "Name" in the Thanks Page i.e. thanks.php ?

    Okay ,
    in send.php you will send the email and then redirect the visitor to thanks.php
    right ?

    We can do another thing like :

    we will show the tanks message on the send.php file

    like :

    PHP Code:

    <?php
      $nameofproduct 
    $_REQUEST['nameofproduct'] ;
      
    $priceusd $_REQUEST['priceusd'] ;
        
      
    $nametitle $_REQUEST['nametitle'] ;
      
    $name $_REQUEST['name'] ;
      
    $address $_REQUEST['address'] ;
      
    $city $_REQUEST['city'] ;
      
    $district $_REQUEST['district'] ;
      
    $state $_REQUEST['state'] ;
      
    $country $_REQUEST['country'] ;
      
    $pincode $_REQUEST['pincode'] ;
      
      
    $telephone $_REQUEST['telephone'] ;
      
    $email $_REQUEST['email'] ;
      
    $query $_REQUEST['query'] ;
      
      
    $uorder date("dmY-His") ;
      
      
    $userdetails $_SERVER['REMOTE_ADDR'];
      
    $ourid "Company Name <info@company.com>";

      
    $message "<h4><font color=blue>$nametitle $name has placed an order. Details are as under</font></h4>
      <br><br><b><u>CONTACT/SHIPPING DETAILS:</u></b>
      <br><b>Name: </b> 
    $title $name
      <br><br><b>Address: </b> 
    $address
      <br><br><b>Telephone: </b> 
    $telephone
      <br><br><b>E-mail ID: </b> 
    $email
      <br><br><b>Any other Information/Question: </b> 
    $query
      <br><br><b>IP Address of Customer is: </b> 
    $userdetails
      <br><br><br><b><u>ORDER DETAILS</u></b>
      <br><b>Order No.: </b> 
    $uorder
      <br><b>Name of Product: </b> PRODUCT NAME
      <br><br><b>Total Amount (in US Dollars): </b> 
    $priceusd ";
      

      
      
    $repmessage "<table border=0 width=600>
      <tr><td><br>
      <b>Dear 
    $title $name</b>
      <br><br>Thanks for your order. Your order details:
      <br><br>Order No.: 
    $uorder
      <br><br>Name of Product: PRODUCT NAME
      <br><br>Rate Per Unit in US Dollars: 
    $priceusd
      <br><br>Amount in USD: 
    $priceusd
      <br><br><br>
      <br><br>Admin Team
      <br><br>Company Name
      <br>http://www.company.com</tr></td></table>"
    ;

      
    $headers "MIME-Version: 1.0\r\n";
      
    $headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
      
    $headers.= "From: $email\r\n";
      
      
    $repheaders "MIME-Version: 1.0\r\n";
      
    $repheaders.= "Content-type: text/html; charset=iso-8859-1\r\n";
      
    $repheaders.= "From: $ourid\r\n";

    // i will edit from here

      
    mail"info@company.com""Order No. $uorder received from website",
        
    $message$headers );

      
    $final_send mail"To: $email""Thanks for your shopping at company.com",
        
    $repmessage$repheaders );
        
        
    if (
    $final_send)
    {
        echo 
    "The Thanks Message here !"// write all that you want here .
    }

    ?>

    Try it and let me know the result .

  8. #8
    Join Date
    Mar 2009
    Posts
    65
    Thanks
    13
    Thanked 4 Times in 4 Posts

    Default

    Just one thing

    Are you running this script on your own computer with a PHP setup? If so, make sure you set the temporary path for session inside your php.ini.

  9. #9
    Join Date
    Mar 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Ahmed Saleh View Post
    Okay ,
    in send.php you will send the email and then redirect the visitor to thanks.php
    right ?

    We can do another thing like :

    we will show the tanks message on the send.php file

    like :

    PHP Code:

    <?php
      $nameofproduct 
    $_REQUEST['nameofproduct'] ;
      
    $priceusd $_REQUEST['priceusd'] ;
        
      
    $nametitle $_REQUEST['nametitle'] ;
      
    $name $_REQUEST['name'] ;
      
    $address $_REQUEST['address'] ;
      
    $city $_REQUEST['city'] ;
      
    $district $_REQUEST['district'] ;
      
    $state $_REQUEST['state'] ;
      
    $country $_REQUEST['country'] ;
      
    $pincode $_REQUEST['pincode'] ;
      
      
    $telephone $_REQUEST['telephone'] ;
      
    $email $_REQUEST['email'] ;
      
    $query $_REQUEST['query'] ;
      
      
    $uorder date("dmY-His") ;
      
      
    $userdetails $_SERVER['REMOTE_ADDR'];
      
    $ourid "Company Name <info@company.com>";

      
    $message "<h4><font color=blue>$nametitle $name has placed an order. Details are as under</font></h4>
      <br><br><b><u>CONTACT/SHIPPING DETAILS:</u></b>
      <br><b>Name: </b> 
    $title $name
      <br><br><b>Address: </b> 
    $address
      <br><br><b>Telephone: </b> 
    $telephone
      <br><br><b>E-mail ID: </b> 
    $email
      <br><br><b>Any other Information/Question: </b> 
    $query
      <br><br><b>IP Address of Customer is: </b> 
    $userdetails
      <br><br><br><b><u>ORDER DETAILS</u></b>
      <br><b>Order No.: </b> 
    $uorder
      <br><b>Name of Product: </b> PRODUCT NAME
      <br><br><b>Total Amount (in US Dollars): </b> 
    $priceusd ";
      

      
      
    $repmessage "<table border=0 width=600>
      <tr><td><br>
      <b>Dear 
    $title $name</b>
      <br><br>Thanks for your order. Your order details:
      <br><br>Order No.: 
    $uorder
      <br><br>Name of Product: PRODUCT NAME
      <br><br>Rate Per Unit in US Dollars: 
    $priceusd
      <br><br>Amount in USD: 
    $priceusd
      <br><br><br>
      <br><br>Admin Team
      <br><br>Company Name
      <br>http://www.company.com</tr></td></table>"
    ;

      
    $headers "MIME-Version: 1.0\r\n";
      
    $headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
      
    $headers.= "From: $email\r\n";
      
      
    $repheaders "MIME-Version: 1.0\r\n";
      
    $repheaders.= "Content-type: text/html; charset=iso-8859-1\r\n";
      
    $repheaders.= "From: $ourid\r\n";

    // i will edit from here

      
    mail"info@company.com""Order No. $uorder received from website",
        
    $message$headers );

      
    $final_send mail"To: $email""Thanks for your shopping at company.com",
        
    $repmessage$repheaders );
        
        
    if (
    $final_send)
    {
        echo 
    "The Thanks Message here !"// write all that you want here .
    }

    ?>

    Try it and let me know the result .
    Thanks for the reply Ahmed Saleh. Actually ditto same thing I had already tried just 2 days ago before your reply. It works as well. But even this has some issue. When someone refreshes/Click Back Button of Browser in the Thanks.php Page (which is actually now working as send.php as well because the Thanks.php & Send.php are one file), the form gets submitted again and a new order code etc. gets generated.

    Is it possible that if someone refreshes or click browser back button the form does not get submitted again ?

  10. #10
    Join Date
    Mar 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by CrazyChop View Post
    Just one thing

    Are you running this script on your own computer with a PHP setup? If so, make sure you set the temporary path for session inside your php.ini.
    Thanks for the reply Crazychop.

    Actually I am testing directing onto web hosting server as I have no idea about setting up PHP in Local System . So I dont think there is any case for editing php.ini.

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
  •