ksa Posted October 15, 2016 Share Posted October 15, 2016 I don’t know if this can be accomplished with php, html, javascript. Anyway, I have a form that I want to be submitted to a php file that will create a pdf with the data from the form. This part I know how to do. However, whenever the form is submitted I want to create an auto increment number every time the form is submitted. I know this could be accomplished with a database that stores the form data and keep track of the number sequencing. Since I don’t have a database, I want to know if there is another way to accomplish this. Do I create a text file and record the last number in the file and read from that file to see what number to use next? Does php have a function that I can use to keep track every time a php scripts is ran or can this be accomplish with javascript/html? Quote Link to comment https://forums.phpfreaks.com/topic/302336-how-do-i-auto-increment-a-form-once-submitted/ Share on other sites More sharing options...
kicken Posted October 15, 2016 Share Posted October 15, 2016 You have to store the number somewhere on the server and use PHP to control access to it. Any javascript solution is unreliable and would only work for a single client. If you cannot use a database then that leaves you with storing the number in text file. You'll need to create a file with the starting number then write code that will read the current number, increment it, and write it back out to the file. During this read/write process you also need to lock the file to prevent concurrent script executions from interfering with each other. To do this you'll be using the fopen, fread, fwrite, rewind, ftruncate, flock, fflush and fclose functions. For example <?php $filename = 'counter.txt'; $fp = fopen($filename, 'r+'); if (!$fp){ die('Could not open file. Create it first.'); } echo 'Attempting to acquire lock'.PHP_EOL; if (flock($fp, LOCK_EX)){ echo 'Lock has been acquired'.PHP_EOL; $value = trim(fgets($fp)); rewind($fp); ftruncate($fp, 0); fwrite($fp, $value+1); echo 'Read value '.$value.'; Incremented value to '.($value+1).' and wrote it back'.PHP_EOL; echo 'Pausing for a while for testing'.PHP_EOL; sleep(10); fflush($fp); flock($fp, LOCK_UN); echo 'Lock has now been released'.PHP_EOL; } fclose($fp); echo 'Finished'.PHP_EOL; First the script opens the file containing the number for read/write access. Next it attempts to acquire an exclusive lock on the file. The script will pause on this line until the lock can be acquired. Once the lock is acquired it means that no other instances of the script can access the file until the lock is released (they will pause instead). After acquiring the lock we read the current value of the file. We then clear the file's existing contents by moving the pointer back to the start and truncating the file to 0 length. The new value is then written to the file. The sleep is there just for testing so you can try running multiple copies of the script and see how they wait on each other due to the locking. Next the file is flushed to disk and unlocked and closed so that other scripts can access it. Quote Link to comment https://forums.phpfreaks.com/topic/302336-how-do-i-auto-increment-a-form-once-submitted/#findComment-1538299 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.