random generated number integrated into a form

Reply

Join Date: Sep 2009
Posts: 6
Reputation: TLCJohn is an unknown quantity at this point 
Solved Threads: 0
TLCJohn TLCJohn is offline Offline
Newbie Poster

Re: random generated number integrated into a form

 
0
  #11
Sep 29th, 2009
Hi there and thanks

Not thinking I did not put it in a .php page but a .html page. Thank you for your help. I am on the design side making site look pretty, I have some Basic knowledge of PHP but not enough. almostbob, if you could go further into your explanation I would be interested in looking at the way you have implemented the random number. I would like to take this opportunity to thank you both for your help. It is greatly appreciated.

John
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1,394
Reputation: almostbob has a spectacular aura about almostbob has a spectacular aura about almostbob has a spectacular aura about 
Solved Threads: 168
almostbob's Avatar
almostbob almostbob is offline Offline
Nearly a Posting Virtuoso

Re: random generated number integrated into a form

 
0
  #12
Sep 29th, 2009
  1. <?php if ($_POST['email']) { /* check whether the file is loading from the post and assign values to the variables */
  2. $firstname=$_POST['firstname'];
  3. $lastname=$_POST['lastname'];
  4. $email=$_POST['email'];
  5. $address=$_POST['address'];
  6. $telephone=$_POST['telephone'];
  7. $countfile = "/cgi-bin/countforms.bcnt"; /* define the file containing the count */
  8. $outfile = "/cgi-bin/outputforms.csv"; /* define the file containing the form data, in this case a .csv text file containing 1 row of each form submitted */
  9. $form_id = file_get_contents( $file ) + 1; /* read the last form number and increment */
  10. $fp = fopen($file, 'w'); /* open the couint file and erase the contents */
  11. fputs ($fp, sprintf('%06d',$form_id)); /* put the new count number to the count file
  12. fclose($fp);
  13. $fo = fopen($outfile,'a+'); /* open the output file to append the new data to the end /*
  14. fputs ($fo, "$form_id,$firstname,$lastname,$email,$address,$telephone\n"); /* write the form to the output file */
  15. fclose($fo);
  16. }
  17. else {
  18. $firstname=$lastname=$email=$address=$telephone=" "; /* blank all data fields */
  19. $form_id='id will be generated on submission'; /* information prompt */
  20. } ?>
  21. <form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
  22. <div>ID= <?php echo $form_id." Please record this number for your records"; ?></div>
  23. Firstname <input type=text name='firstname' value="$firstname" />
  24. Lastname <input type=text name='lastname' value="$lastname" /><br />
  25. Email <input type=text name='email' value="$email" />
  26. Address <input type=text name='address' value="$address" /><br />
  27. telephone <input type=text name='telephone' value="$telephone" /><br />
  28. <?php if (!S_POST['email']) {echo "<input type='submit' name='submit' value='submit' />" ;} else { echo "thank you"; }
  29. </form> <!-- normal html form -->
the form submits to itself, on submission it writes the id number into the id field and displays the data entered in other fields,
I delete the submit button on the post-submit form on this one so that it is not submitted over n over
the number is consecutive
the serial number is a simple file containing a six digit number,
the number is initially set to 000000 when the script is installed
the number is read, incremented and written back to the erased file after the form is validated
in this concept the validation is lousy(nonexistent), twas just to try it out
thanks edwinhermann for the code correction
the new six digit number and the input data are written to the output file and to the browser for the user to read
the output file is .csv comma separated values, the initial content of the file is a comma separated list of the field names
the data fields are written comma separated
excell reads csv as spreadsheets,
today I am doing accounts so Im excell in my head
the data could be sent to mail(), to the user and the site owner, or all of the above.
Last edited by almostbob; Sep 29th, 2009 at 1:57 pm.
Failure is not an option It's included free, you don't have to do anything to get it
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it

Please mark solved problems, solved
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 6
Reputation: TLCJohn is an unknown quantity at this point 
Solved Threads: 0
TLCJohn TLCJohn is offline Offline
Newbie Poster

Re: random generated number integrated into a form

 
0
  #13
Sep 29th, 2009
Hi almostbob

Thanks for that, but to be very honest, the information has flown over my head, could I have it in layman's english, sorry but, thick as thr brown stuff this end.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,081
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 137
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster

Re: random generated number integrated into a form

 
0
  #14
Sep 29th, 2009
@ AB:

  1. $countfile = "/cgi-bin/countforms.bcnt"; /* define the file containing the count */
  2. $outfile = "/cgi-bin/outputforms.csv"; /* define the file containing the form data, in this case a .csv text file containing 1 row of each form submitted */
  3. $form_id = file_get_contents( $file ) + 1; /* read the last form number and increment */
Is the $file variable = $countfile? I couldn't see it referenced previously.
Happy Humbugging Christmas
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 224
Reputation: chrishea is an unknown quantity at this point 
Solved Threads: 32
chrishea's Avatar
chrishea chrishea is offline Offline
Posting Whiz in Training

Re: random generated number integrated into a form

 
0
  #15
Sep 29th, 2009
I have used this little routine to create a unique (ascending) sequence number for registrations. These aren't random, they are very predictable. If you are using the number as a unique reference number, then this is probably what you want to do. It uses a text file so you don't need a database. It locks the file because you could have multiple users trying to do this at the same time (so it needs to be done in a serial fashion).
  1. $fc =fopen("reg_ctr.txt","a+") or die("Error!");
  2. flock($fc,LOCK_EX); /* lock the file */
  3. $seq_ctr = fread($fc,10);
  4. $seq_ctr_next = $seq_ctr;
  5. @$seq_ctr_next = $seq_ctr_next+1;
  6. ftruncate($fc,0);
  7. $ret=fwrite($fc,$seq_ctr_next);
  8. fflush($fc);
  9. flock($fc,LOCK_UN); /* unlock the file */
  10. $ret=fclose($fc);

In other cases where "semi-unique" is close enough (the number generated is never used on its own to find the associated information), I have used the time stamp as the reference number. If you use the full ten characters, then it is unique unless two threads request a number in the same second. If you limit it to the last six characters, then it is less unique and you will eventually get some duplicates. If you want it more unique, you use microtime and a much longer number. For this type of use, the time-stamp info can also be combined with some other info (e.g. last name or part of the phone number) to make it even more unique.
Last edited by chrishea; Sep 29th, 2009 at 3:05 pm.
Chris
See my list of PHP development tools at:
InnovationsDesign.net
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1,394
Reputation: almostbob has a spectacular aura about almostbob has a spectacular aura about almostbob has a spectacular aura about 
Solved Threads: 168
almostbob's Avatar
almostbob almostbob is offline Offline
Nearly a Posting Virtuoso

Re: random generated number integrated into a form

 
0
  #16
Sep 29th, 2009
Originally Posted by ardav View Post
@ AB:
Is the $file variable = $countfile? I couldn't see it referenced previously.
Thanks, Ardav, the obvious error, wasnt obvious when I looked at it first time, i searched for bugs, and missed it
  1. $form_id = file_get_contents( $countfile ) + 1;
the editor I am trying this time tries to complete the code, great when you can't remember the parameterss, lousy when you walk away to grab a coffee and havent completed the line
Last edited by almostbob; Sep 29th, 2009 at 5:02 pm.
Failure is not an option It's included free, you don't have to do anything to get it
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it

Please mark solved problems, solved
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 77
Reputation: Tulsa is an unknown quantity at this point 
Solved Threads: 15
Tulsa's Avatar
Tulsa Tulsa is offline Offline
Junior Poster in Training

Re: random generated number integrated into a form

 
0
  #17
Sep 30th, 2009
Hi,
I also faces this same problem then i found solution which i posted here...

  1. $i=0;
  2. $rand_no="";
  3. while($i<6)
  4. {
  5. $rand_no=$rand_no.rand(0,9);
  6. $i++;
  7. }
  8. echo "6 digit random no.".$rand_no;
  9.  
  10. I hope this code may help you
  11. Thanks
  12. Tulsa
"Be honest"
"Confidence is everything"
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 43
Reputation: edwinhermann is an unknown quantity at this point 
Solved Threads: 9
edwinhermann edwinhermann is offline Offline
Light Poster

Re: random generated number integrated into a form

 
0
  #18
Sep 30th, 2009
Originally Posted by Tulsa View Post
Hi,
I also faces this same problem then i found solution which i posted here...

  1. $i=0;
  2. $rand_no="";
  3. while($i<6)
  4. {
  5. $rand_no=$rand_no.rand(0,9);
  6. $i++;
  7. }
  8. echo "6 digit random no.".$rand_no;
  9.  
  10. I hope this code may help you
  11. Thanks
  12. Tulsa
Or, a shorter version of what you've got is this:

  1. $rand_no = rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
  2. echo $rand_no
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,081
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 137
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster

Re: random generated number integrated into a form

 
-1
  #19
Sep 30th, 2009
It's a shame php doesn't have a python-like zfill() function, then you'd just need the one rand() or mt_rand() call, not 6. Perhaps I'm wrong, anyone out there know of a function? You could always write your own function, like this:

  1. function random_zfill() {
  2. $int = mt_rand(0,999999);
  3. for($i=0; $i<(6-strlen($int)); $i++)$zeroes .= '0';
  4. return $zeroes.$int;
  5. }
  6.  
  7. $my_number = random_zfill();

Anyway, this is all academic since the thread starter will be using a file-based counter (I assume).
Obviously, you could expand the function to have parameters like start, end etc.
Last edited by ardav; Sep 30th, 2009 at 6:53 am.
Happy Humbugging Christmas
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 6
Reputation: TLCJohn is an unknown quantity at this point 
Solved Threads: 0
TLCJohn TLCJohn is offline Offline
Newbie Poster

Re: random generated number integrated into a form

 
0
  #20
Sep 30th, 2009
Hi there Ardav

To answer your question, I will be using the code you wrote at the top of this post, it works well and now that I have had to add another digit, I think it will be ok, any case the client is happy, and at the end of the day, that is the main thing. I need to find something to help me write PHP - A job for Christmas and the New Year. THANKS to everyone who has answered my question, and I mean that.

Kind regards John
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 605 | Replies: 21
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC