473,324 Members | 2,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Cookie values in php

119 100+
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.
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. include ("config/config.php");
  4. $dbtable = "corporates";
  5. $resultvalue=0;
  6.  
  7. $first_name = strip_tags(mysql_real_escape_string($_POST["first_name"]));
  8. setcookie("first_name",$first_name,time()+3600);
  9. $middle_name = strip_tags(mysql_real_escape_string($_POST["middle_name"]));
  10. $last_name = strip_tags(mysql_real_escape_string($_POST["last_name"]));
  11. $dobdd = strip_tags(mysql_real_escape_string($_POST["dob-dd"]));
  12. $dobmm =strip_tags(mysql_real_escape_string($_POST["dob-mm"]));
  13. $dobyyyy = strip_tags(mysql_real_escape_string($_POST["dob-yyyy"]));
  14. $dob = $dobdd . "-" . $dobmm . "-" . $dobyyyy;
  15. $bloodgroup = strip_tags(mysql_real_escape_string($_POST["blood-group"]));
  16. $pmailid = strip_tags(mysql_real_escape_string($_POST["p-mailid"]));
  17. $pcell = strip_tags(mysql_real_escape_string($_POST["p-cell"]));
  18. $lno = strip_tags(mysql_real_escape_string($_POST["l-no"]));
  19. $caddress1 = strip_tags(mysql_real_escape_string($_POST["c-address"]));
  20. $caddress2 = strip_tags(mysql_real_escape_string($_POST["c-address2"]));
  21. $caddress3 = strip_tags(mysql_real_escape_string($_POST["c-address3"]));
  22. $caddress = $c-address1 . "," . $c-address2 . "," . $c-address3 . ".";
  23. $language = strip_tags(mysql_real_escape_string($_POST["languageknown"]));
  24.  
  25. 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))
  26. {
  27. /**** Setting Cookie values for the field to redirect to the same page ****/
  28. $first_name = strip_tags(mysql_real_escape_string($_POST["first_name"]));
  29. setcookie("first_name",$first_name,time()+3600);
  30. ?>
  31. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  32. <html xmlns="http://www.w3.org/1999/xhtml">
  33. <head>
  34. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  35. <title>Kindly fill all the erquired Fields</title>
  36. <script type="text/javascript">
  37. alert(" Kindly Fill all the required Fields");
  38. </script>
  39. </head>
  40. <body>
  41. <script>window.location='employee_register.php'</script>
  42. </body>
  43. </html>
  44. <?php
  45. }
  46. else
  47. {
  48.                /*    update to database  */
  49. }
  50. ?>
  51.  

Regards
magesh
Jan 3 '09 #1
2 1637
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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. header("Location: some_other_page.php");
  3. ?>
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.
Expand|Select|Wrap|Line Numbers
  1. <input type="hidden" name="subbmited" value="1" />
and then your overall page looks something like:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if($_POST['submitted']){//if they DID submit the form
  3.     //do validation
  4.     if( validation passed){
  5.          header("Location: success_page.php");
  6.          exit(); //don't display the form, just end the script here
  7.     }
  8. }
  9. //else, if it failed validation, or if the form wasn't submitted
  10. //...display the form
  11. ?>
  12. <form action="this_page" method="post">
  13. <!-- put form here y'all -->
  14. </form>
Then in each of your form fields you can do:
Expand|Select|Wrap|Line Numbers
  1. <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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start(); //gives the user a cookie to uniquely identify them
  3. //you can then save variables in the _SESSION array.
  4. //These are saved on your server, but they are unique for each user.
  5. //When you re-open the session on another page PHP will give you the variables that were set for that user.
  6. $_SESSION['name'] = $_POST['name'];
  7. $_SESSION['phone_number'] = '123';
  8. ?>
Then in another page you can do:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start(); //re-open the session
  3. print $_SESSION['name']; //will print out the name for the user that's viewing the page
  4. ?>
I hope that helps.
Altonator
Jan 3 '09 #2
Atli
5,058 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $data = array(
  3.   "someValue" => ""
  4. );
  5.  
  6. // This checks if the form has been submitted 
  7. // and tries to process it if it has.
  8. if(isset($_POST['isSubmitted'])) 
  9. {
  10.   // Do whatever checking you need to do
  11.   // and process the data here.
  12.  
  13.   if(/* The data was processed successfully */) {
  14.     header("Location: successPage.php");
  15.     die();
  16.   }
  17.   else {
  18.     // Get the data ready to be insert into the form again. 
  19.     $data = htmlentities($_POST['someValue']);
  20.   }
  21. }
  22.  
  23. // Note that if the form was successfully processed,
  24. // the code would never reach this point.
  25.  
  26. // This shows the form, including the data
  27. // your received last time it was posted. (If any)
  28. echo <<< HTML
  29. <form action="{$_SERVER['PHP_SELF']}" method="post">
  30.   <input type="hidden" name="isSubmitted" value="1">
  31.   <input type="text" name="someValue" value="{$data['someValue']}"><br>
  32.   <input type="submit">
  33. </form>
  34. HTML;
  35. ?>
Or you could simply use session to transfer the data between pages.

Edit
I see altonator beat me to it :)
Jan 3 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Jason Collins | last post by:
There seems to be an inconsistency (bug?) in the way the Set-Cookie header is handled by the WebHeaderCollection. That is, the values of Set-Cookie, when an Expires is specified, contain the ","...
3
by: Wysiwyg | last post by:
After a server created cookie is processed on the client I want it removed, cleared, or expired in the javascript block but have been unable to do this. If I set a cookie value in the server code...
5
by: Miljana | last post by:
Hi, I have one problem with cookies in ASP.NET application. It seems that I can not retreive cookie from Request.Cookies collection. I put cookie in Response.Cookies collection, and after page...
0
by: tshad | last post by:
I can add a cookie fine, but I can't seem to figure out how to change the value of a cookie and save it back. My code looks like: if (passwordSaveCookie.checked) then If...
1
by: CR1 | last post by:
I found a great cookie script below, but don't know how to make it also pass the values sent to the cookie, to a querystring as well for tracking purposes. Can anyone help? If there was a way to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.