Cookie values in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpmagesh
    New Member
    • Nov 2008
    • 119

    Cookie values in php

    Hi to all,

    I am creating a registration page, like name, last name, Date of birth, ph number, address etc..

    When i click submit button all the values are taking to validation page using POST method.

    If any field is blank then script will say fill all the fields, then redirect to the registration page itself,

    During this process when redirecting to registration page i should get the existing values which i typed already.

    Can any one give me some idea to do so please,

    This is validation page code.
    Code:
    <?php
    include ("config/config.php");
    $dbtable = "corporates";
    $resultvalue=0;
    
    $first_name = strip_tags(mysql_real_escape_string($_POST["first_name"]));
    setcookie("first_name",$first_name,time()+3600);
    $middle_name = strip_tags(mysql_real_escape_string($_POST["middle_name"]));
    $last_name = strip_tags(mysql_real_escape_string($_POST["last_name"]));
    $dobdd = strip_tags(mysql_real_escape_string($_POST["dob-dd"]));
    $dobmm =strip_tags(mysql_real_escape_string($_POST["dob-mm"]));
    $dobyyyy = strip_tags(mysql_real_escape_string($_POST["dob-yyyy"]));
    $dob = $dobdd . "-" . $dobmm . "-" . $dobyyyy;
    $bloodgroup = strip_tags(mysql_real_escape_string($_POST["blood-group"]));
    $pmailid = strip_tags(mysql_real_escape_string($_POST["p-mailid"]));
    $pcell = strip_tags(mysql_real_escape_string($_POST["p-cell"]));
    $lno = strip_tags(mysql_real_escape_string($_POST["l-no"]));
    $caddress1 = strip_tags(mysql_real_escape_string($_POST["c-address"]));
    $caddress2 = strip_tags(mysql_real_escape_string($_POST["c-address2"]));
    $caddress3 = strip_tags(mysql_real_escape_string($_POST["c-address3"]));
    $caddress = $c-address1 . "," . $c-address2 . "," . $c-address3 . ".";
    $language = strip_tags(mysql_real_escape_string($_POST["languageknown"]));
    
    if((strlen($first_name)==0)||(strlen($middle_name)==0)||(strlen($last_name)==0)||(strlen($dob-dd)==0)||(strlen($dobmm)==0)||(strlen($dobyyyy)==0)||(strlen($dob)==0)||(strlen($bloodgroup)==0)||(strlen($pmailid)==0)||(strlen($pcell)==0)||(strlen($lno)==0)||(strlen($caddress1)==0)||(strlen($caddress2)==0)||(strlen($caddress3)==0)||(strlen($caddress)==0)||(strlen($language)==0))
    {
    /**** Setting Cookie values for the field to redirect to the same page ****/
    $first_name = strip_tags(mysql_real_escape_string($_POST["first_name"]));
    setcookie("first_name",$first_name,time()+3600);
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Kindly fill all the erquired Fields</title>
    <script type="text/javascript">
    alert(" Kindly Fill all the required Fields");
    </script>
    </head>
    <body>
    <script>window.location='employee_register.php'</script>
    </body>
    </html>
    <?php
    }
    else
    {
                   /*    update to database  */
    }
    ?>

    Regards
    magesh
  • altonator
    New Member
    • Jan 2008
    • 15

    #2
    There are several ways of doing what you're trying to do. In fact it's probably THE most common thing in PHP ever.

    Your method is slightly odd.

    Firstly, using javascript to redirect is silly. If the user has Javascript disabled then they wont be redirected. They'll just see an empty page.

    **IF** you want to redirect then do it in php:
    Code:
    <?php
    header("Location: some_other_page.php");
    ?>
    As with set_cookie() you need to do that before you output anything to the browser.

    If you want to save what the user has input into the form fields then you can either have your form submit to itself, and have a hidden form field, e.g.
    Code:
    <input type="hidden" name="subbmited" value="1" />
    and then your overall page looks something like:
    Code:
    <?php
    if($_POST['submitted']){//if they DID submit the form
        //do validation
        if( validation passed){
             header("Location: success_page.php");
             exit(); //don't display the form, just end the script here
        }
    }
    //else, if it failed validation, or if the form wasn't submitted
    //...display the form
    ?>
    <form action="this_page" method="post">
    <!-- put form here y'all -->
    </form>
    Then in each of your form fields you can do:
    Code:
    <input name="whatever" value="<?php print $_POST['whatever']; ?>" />
    Some people would say that this method is crude (which it kind of is), but I think it's far less crude than what you've got.

    If you ever want to save information about the user between pages then you can use:
    Code:
    <?php
    session_start(); //gives the user a cookie to uniquely identify them
    //you can then save variables in the _SESSION array.
    //These are saved on your server, but they are unique for each user.
    //When you re-open the session on another page PHP will give you the variables that were set for that user.
    $_SESSION['name'] = $_POST['name'];
    $_SESSION['phone_number'] = '123';
    ?>
    Then in another page you can do:
    Code:
    <?php
    session_start(); //re-open the session
    print $_SESSION['name']; //will print out the name for the user that's viewing the page
    ?>
    I hope that helps.
    Altonator

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      I usually like posting things to the same page I am on, so if I need the user to fix something in the form and resubmit, the data is all there.

      For example:
      [code=php]
      <?php
      $data = array(
      "someValue" => ""
      );

      // This checks if the form has been submitted
      // and tries to process it if it has.
      if(isset($_POST['isSubmitted']))
      {
      // Do whatever checking you need to do
      // and process the data here.

      if(/* The data was processed successfully */) {
      header("Locatio n: successPage.php ");
      die();
      }
      else {
      // Get the data ready to be insert into the form again.
      $data = htmlentities($_ POST['someValue']);
      }
      }

      // Note that if the form was successfully processed,
      // the code would never reach this point.

      // This shows the form, including the data
      // your received last time it was posted. (If any)
      echo <<< HTML
      <form action="{$_SERV ER['PHP_SELF']}" method="post">
      <input type="hidden" name="isSubmitt ed" value="1">
      <input type="text" name="someValue " value="{$data['someValue']}"><br>
      <input type="submit">
      </form>
      HTML;
      ?>[/code]
      Or you could simply use session to transfer the data between pages.

      Edit
      I see altonator beat me to it :)

      Comment

      Working...