Web Design Forums

PHP

Have questions about PHP? Ask them here and our experts will assist you before you know it! You can also find help in the documentation at PHP.net.

Generate unique reference number upon form submission



Site of the Month Nominations
ENTER YOUR SITE NOW!

Reply
 
LinkBack Thread Tools
Old January 21 '09, 05:46 PM (#1)
dsmflash is offline
WDF Member
 
dsmflash's Avatar
 
Join Date: January 2007
Location: Indiana
Posts: 40
dsmflash is an unknown quantity at this point
Generate unique reference number upon form submission

Hi,

I have a client who needs a form to produce a unique 6 digit sequential number, like a reference or invoice number, upon submission and the form results emailed to the user with the number as well.

1. can anyone give me some direction on this
2. will it require a database

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 21 '09, 10:14 PM (#2)
mlseim is offline
WDF Staff
 
mlseim's Avatar
 
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
mlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud of
I would recommend you use a database (MySQL) because it can create
a unique number for each added entry AND save the data for other uses,
such as saving name, email address, etc.

Now, depending on how sensitive, or secure the site needs to be, you could
do this without a MySQL database, but it would not be as secure. Your "database"
in that case would be a simple text file stored on your website.

You also could create a 6 digit random number instead of sequential...

Pretend my number is 100123, and you are using sequential numbers ...
I know that someone has number 100122 and 100121.
Depending on what your website is all about, perhaps knowing someone
else's number is not a good thing?

With MySQL, each person would have a unique ID number that is used for
the admin, but another field with a random 6-digit ID could be used along with
the MySQL ID record.

So, think about these things and let us know if you've changed what you want.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 21 '09, 11:12 PM (#3)
dsmflash is offline
WDF Member
 
dsmflash's Avatar
 
Join Date: January 2007
Location: Indiana
Posts: 40
dsmflash is an unknown quantity at this point
Thanks for the help.

I believe the project calls for use on an intranet.
I don't believe security is a major issue, just the unique number for each form submission. Its just a reference number that will be unique to that user. I just want that number and the form results to be sent to them and the user.
I have been Googling and figured that i would have to create a unique id field in my database.
I'm not much of a database or php person, but i think i might be able to figure it out. I would just create code that outputs the MySql unique id field into the form results, right?

thanks again!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 22 '09, 09:34 AM (#4)
mlseim is offline
WDF Staff
 
mlseim's Avatar
 
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
mlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud of
You wouldn't need to use MySQL if there were no issues with security.

Create a text file called "id.db" and put the starting number on a line,
like this ...
100001

Save that file, upload it and set file permissions to 777, so the script can write to it.

PHP Code:
<?php

// Get the email address from the form (from the person using the form).
$email $_POST['email'];

// Who else will get this email ... the reply email address.
$recipient "johnsmith@aol.com";

// This is the path and name of your file that holds the sequence number.
$url "id.db";

// Open the file to see the current sequence number.
$id file($url);

// Increment the number to the next sequence.
$next $id[0]+1;

// Go ahead and email the ID number to both parties.
// First create the subject and message ...
$toaddr $email.",".$recipient;
$subject "Here is Your ID";
$message "

Online Form ...
---------------------------------------------------
Here is your ID number: $next
---------------------------------------------------
$email,
Thanks for using our online form to
get your ID number.

---------------------------------------------------
"
;

// Add any extra headers if you want.
$extraheaders "From: $recipient\nReply-To: $recipient\n\n";

// Send the actual email to both parties.
if (! mail($toaddr$subject$message$extraheaders))
  echo 
"Mail did not send for some reason.";

// Now, save the new number into the file.
$fh fopen($url'w') or die("can't open file");
fwrite($fh$next);
fclose($fh); 

// Go to this page after the email is sent.
header("Location: index.php");

?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 23 '09, 12:47 PM (#5)
dsmflash is offline
WDF Member
 
dsmflash's Avatar
 
Join Date: January 2007
Location: Indiana
Posts: 40
dsmflash is an unknown quantity at this point
// This is the path and name of your file that holds the sequence number.
$url = "id.db";


can you give me some indication of what this db file would look like, or am i pulling from a unique id field in my database?


thank you
.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 23 '09, 03:08 PM (#6)
mlseim is offline
WDF Staff
 
mlseim's Avatar
 
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
mlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud of
The text file called "id.db" is just one number on a line all by itself (the first line).

123123


You could call it "number.txt", "id.txt" ... I just called it "id.db"
I don't like to use the .txt extension on any website files (just a personal preference).

You don't have a database, it's just a text file.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 23 '09, 05:58 PM (#7)
dsmflash is offline
WDF Member
 
dsmflash's Avatar
 
Join Date: January 2007
Location: Indiana
Posts: 40
dsmflash is an unknown quantity at this point
thank you so much. i will try it and hopefully i'm smart enough to make it work.

so just put your code in a file, say form.php, and make that the action?


thank you , you are very helpful!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 23 '09, 06:13 PM (#8)
mlseim is offline
WDF Staff
 
mlseim's Avatar
 
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
mlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud of
Yes ...

<form action="form.php" method="post">
Enter your email: <input type="text" name="email" value=""><br />
<input type="submit" name="submit" value="Get Your ID Now">
</form>

Also ...

Make sure when you upload your file that has the 6 digit number ( id.db ),
that you set the file permission to 777 (or 0777) using your FTP program.

The script needs to write the next number into that file and you'll get an
error ( file permission denied ) if the permission is not set.


EDIT:
If you're really lost, PM me with your FTP account login and I'll go in there
and make it work for you. I just think it's a good idea for you to try ...
you'll learn a lot more by doing this yourself.


.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 24 '09, 01:49 AM (#9)
dsmflash is offline
WDF Member
 
dsmflash's Avatar
 
Join Date: January 2007
Location: Indiana
Posts: 40
dsmflash is an unknown quantity at this point
I think the project calls for this to be used on an intranet, i don't know if that makes any difference. i should be able to get it to work. i agree with doing it your self. that's how i've learned everything i know so far.
i just dont program or do db work, but i am learning.

if its ok, i'll pm you if i run into trouble and can't get it resolved.

thanks again.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 24 '09, 02:42 PM (#10)
mlseim is offline
WDF Staff
 
mlseim's Avatar
 
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
mlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud of
intranet should be fine ...

I assume you can run PHP (and it's not an ASP type of site)?

If it's a Windows server with ASP, then PHP is probably not installed.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 24 '09, 09:09 PM (#11)
dsmflash is offline
WDF Member
 
dsmflash's Avatar
 
Join Date: January 2007
Location: Indiana
Posts: 40
dsmflash is an unknown quantity at this point
yeah, i hope its not ASP. i shot an email to my contact to make sure. i'm not a fan of windows servers or asp.

i'll post again and let you know.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old January 26 '10, 12:07 PM (#12)
Preecer is offline
New Member!
 
Preecer's Avatar
 
Join Date: January 2010
Posts: 1
Preecer is an unknown quantity at this point
Thanks for this, just what I was looking for. It works on PHP 6 and is very simple to set up.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old April 10 '10, 10:10 AM (#13)
kubochan is offline
New Member!
 
kubochan's Avatar
 
Join Date: April 2010
Posts: 1
kubochan is an unknown quantity at this point
can u help me...
i put '000' in id.db like u said.but it count start with '1'.can u help me on this because i want the sequence number be like '001,002,003....'
Thank you.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old April 10 '10, 10:58 AM (#14)
Shadowfiend is offline
Code beautifully and honorably
 
Shadowfiend's Avatar
 
Join Date: June 2005
Location: Atlanta, GA
Posts: 4,143
Shadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond repute
I have a slight addendum question -- how many users will be on the system? The problem with the flat file approach is that if two requests hit the server at the same time and see the same `last number', you will likely end up with a uniqueness issue. The benefit of a database is the atomicity of the number increment -- the way a database works, if two requests try to write at the same time, you will still get a unique id. Even with relatively few users, this can be a problem, because it's the timing that matters, not the load.

As to printing the number right, instead of just using $id, do this:

PHP Code:
// Open the file to see the current sequence number. 
$id file($url); 

// Increment the number to the next sequence. 
$next sprintf('%03d'$id[0]+1); 
sprintf lets you pad it with 0s to fill the space of three characters (you can change %03d to %04d for a 4-digit number, and so on.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old April 14 '10, 09:56 AM (#15)
Steax is offline
Cookie Monster
 
Steax's Avatar
 
Join Date: December 2006
Location: Bandung, Indonesia
Posts: 1,208
Steax is a splendid one to beholdSteax is a splendid one to beholdSteax is a splendid one to beholdSteax is a splendid one to beholdSteax is a splendid one to beholdSteax is a splendid one to beholdSteax is a splendid one to behold
The simple solution is to append a userID or something, unique to each user, on one end of the number.

The other more random solution is to just generate a random, say, 10 digit number, and on every run generate some numbers and see if it's already used. This will require a database, though. But any complex solution will likely require a database.

(For that second method, depending on the code it might still be possible for two machines to generate the same random ID between their checks and insert it into the database simultaneously. Some rigorous testing is needed to make sure your implementation is foolproof, although the chances of 2 computers generating the same 10 digit number at the same time is very low - about one in one ***tillion.)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

  Web Design Forums » Programming Help » PHP

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Form submission to populate a HTML/PHP page Mimic HTML and CSS Help 7 June 14 '10 01:21 PM
Form Submission to a Secure Site? Abstract PHP 2 March 14 '07 05:11 PM
your website submission form doesn't work firedragonp1 Forum Feedback 2 November 14 '06 01:11 PM
WML & WMLSCript ???? gemini_shooter HTML and CSS Help 0 January 8 '06 05:49 PM
PHP submission form on RH9 Intranet pc tekquest PHP 0 January 5 '04 07:51 PM

 
User Infomation
Your Avatar

Site Of The Month

Ticket Cake
Ticket Cake

Ticket Cake is a drupal based event ticketing platform. It features that ability to browse events and share them.

Nominate Your Site Now!

Advertisement
WolfCMS.org

Latest Articles
- by RickM
- by bfsog

Advertisement

Partner Links



All times are GMT -4. The time now is 02:36 AM.


WebDesignForums.net is Copyright © 2010 RikeMedia.

SEO by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163