 |
January 21 '09, 05:46 PM (#1)
|
|
|
WDF Member
Join Date: January 2007
Location: Indiana
Posts: 40
|
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
|
|
January 21 '09, 10:14 PM (#2)
|
|
|
WDF Staff
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
|
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.
|
|
January 21 '09, 11:12 PM (#3)
|
|
|
WDF Member
Join Date: January 2007
Location: Indiana
Posts: 40
|
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!
|
|
January 22 '09, 09:34 AM (#4)
|
|
|
WDF Staff
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
|
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");
?>
|
|
January 23 '09, 12:47 PM (#5)
|
|
|
WDF Member
Join Date: January 2007
Location: Indiana
Posts: 40
|
// 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.
|
|
January 23 '09, 03:08 PM (#6)
|
|
|
WDF Staff
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
|
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.
|
|
January 23 '09, 05:58 PM (#7)
|
|
|
WDF Member
Join Date: January 2007
Location: Indiana
Posts: 40
|
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!!
|
|
January 23 '09, 06:13 PM (#8)
|
|
|
WDF Staff
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
|
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.
.
|
|
January 24 '09, 01:49 AM (#9)
|
|
|
WDF Member
Join Date: January 2007
Location: Indiana
Posts: 40
|
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.
|
|
January 24 '09, 02:42 PM (#10)
|
|
|
WDF Staff
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
|
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.
|
|
January 24 '09, 09:09 PM (#11)
|
|
|
WDF Member
Join Date: January 2007
Location: Indiana
Posts: 40
|
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.
|
|
January 26 '10, 12:07 PM (#12)
|
|
|
New Member!
Join Date: January 2010
Posts: 1
|
Thanks for this, just what I was looking for. It works on PHP 6 and is very simple to set up.
|
|
April 10 '10, 10:10 AM (#13)
|
|
|
New Member!
Join Date: April 2010
Posts: 1
|
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.
|
|
April 10 '10, 10:58 AM (#14)
|
|
|
Code beautifully and honorably
Join Date: June 2005
Location: Atlanta, GA
Posts: 4,143
|
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.
|
|
April 14 '10, 09:56 AM (#15)
|
|
|
Cookie Monster
Join Date: December 2006
Location: Bandung, Indonesia
Posts: 1,208
|
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.)
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
| Advertisement |
|
|
|