Solved

Increment number for basic PHP website email form

Posted on 2009-04-15
12
428 Views
Last Modified: 2013-11-10
Attached is the code for a PHP file for an email form we are running. I want to attach a unique number to each work order that is sent out. I would assume that I need to increment numbers but I can't get anything to work. I'm not for sure why. Currently the subject line says Work Order Request - Wyandotte and I want it to say Work Order Request #1 - Wyandotte and then the next work order that is sent out will say Work Order Request #2 - Wyandotte and so on. Any help is greatly appreciated.
<?
 

$mailto = 'email@email.com' ;

$subject = "Workorder Request - Wyandotte" ;

$formurl = "http://www.websiteform.com" ;

$errorurl = "http://www.websiteform.com/erremail.html" ;

$thankyouurl = "http://www.websiteform.com/tyemail.html" ;

$uself = 0;

$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;

$name = $_POST['name'] ;

$email = $_POST['email'] ;

$username = $_POST['username'] ;

$department = $_POST['department'] ;

$problem = $_POST['problem'] ;

$priority = $_POST['priority'] ;

$reboot = $_POST['reboot'] ;

$comments = $_POST['comments'] ;

$http_referrer = getenv( "HTTP_REFERER" );

if (!isset($_POST['email'])) {

	header( "Location: $formurl" );

	exit ;

}

if (empty($name) || empty($email) || empty($username) || empty($comments)) {

   header( "Location: $errorurl" );

   exit ;

}

if ( ereg( "[\r\n]", $name ) || ereg( "[\r\n]", $email ) ) {

	header( "Location: $errorurl" );

	exit ;

}

if (get_magic_quotes_gpc()) {

	$comments = stripslashes( $comments );

}

$messageproper =

	"Name of sender: $name\n\n" .

	"Email of sender: $email\n\n" .

	"Name of user with problem: $username\n\n" .

	"Department: $department\n\n" .

	"Problem: $problem\n\n" .

	"Priority Level: $priority\n\n" .

	"Has the user rebooted their computer?: $reboot\n\n" .

	"------------------------- PROBLEMS -------------------------\n\n" .

	$comments .

	"\n\n------------------------------------------------------------\n" ;

mail($mailto, $subject, $messageproper,

	"From: \"$name\" <$email>" . $headersep . "Reply-To: \"$name\" <$email>" . $headersep . "X-Mailer: chfeedback.php 2.07" );

header( "Location: $thankyouurl" );

exit ;

?>

Open in new window

0
Comment
Question by:Wyandotte
  • 6
  • 4
  • 2
12 Comments
 
LVL 4

Expert Comment

by:justin-clarke
ID: 24148262
Well it won't be able to remember what the last number was if the page is closed, unless this value is saved / retrieved from a database ?

If you want a unique number without doing that then you can use datetime to get a UNC datetime string which will be a unique number.
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24148293
Do you have a database? I would insert each submission into a row in a database table, and use the primary key of the db table as an identifier for each Workorder Request.
0
 

Author Comment

by:Wyandotte
ID: 24148343
I do not have a database. Does it require a database? I pictured it in my head as being something along these lines:

num = 1

on submission increment num 1 place

num = 2

Would something along these lines not work? I've done this in C++ many many years ago but I can't get anything to work on the website form.
0
 
LVL 4

Expert Comment

by:justin-clarke
ID: 24149261
Wouldnt really work, as I said the variable "num" would be declared as 1 on each page load, or in each session. So therefore if the page/browser was closed then the "num" variable would be reset to 1 again.
0
 

Author Comment

by:Wyandotte
ID: 24149289
IC, i thought that num would change to 2 in the actual code and then to 3 and so on. So there is no way to actually change the code in the PHP file? You have to setup SQL. I have just never worked with the SQL on website so I don't quote know what I am doing.
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24149366
You need to store the "num" between submissions. The best option would be to store it in a database, but you can also try to store it in a file. The problem with that is that it is not "safe", if two users submit at the exact same time, they could get the same number.
function next_num() {

  $f = @fopen('num.txt','r');

  if($f) {

    $num = (int)fread($f,10) + 1;

  } else $num = 1;

  $f = @fopen('num.txt','w');

  fwrite($f,$num);

  fclose($f);

  return $num;

}

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:Wyandotte
ID: 24149484
This is what i have now. Note that I also changed the subject line of the email. Nothing shows up after the number sign.
<?

 

function next_num() {

  $f = @fopen('num.txt','r');

  if($f) {

    $num = (int)fread($f,10) + 1;

  } else $num = 1;

  $f = @fopen('num.txt','w');

  fwrite($f,$num);

  fclose($f);

  return $num;

}

$mailto = 'email@email.com' ;

$subject = "Workorder Request #$num - Wyandotte" ;

$formurl = "http://www.websiteform.com" ;

$errorurl = "http://www.websiteform.com/erremail.html" ;

$thankyouurl = "http://www.websiteform.com/tyemail.html" ;

$uself = 0;

$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;

$name = $_POST['name'] ;

$email = $_POST['email'] ;

$username = $_POST['username'] ;

$department = $_POST['department'] ;

$problem = $_POST['problem'] ;

$priority = $_POST['priority'] ;

$reboot = $_POST['reboot'] ;

$comments = $_POST['comments'] ;

$http_referrer = getenv( "HTTP_REFERER" );

$to = $mailto.','. $email;

if (!isset($_POST['email'])) {

        header( "Location: $formurl" );

        exit ;

}

if (empty($name) || empty($email) || empty($username) || empty($comments)) {

   header( "Location: $errorurl" );

   exit ;

}

if ( ereg( "[\r\n]", $name ) || ereg( "[\r\n]", $email ) ) {

        header( "Location: $errorurl" );

        exit ;

}

if (get_magic_quotes_gpc()) {

        $comments = stripslashes( $comments );

}

$messageproper =

        "Name of sender: $name\n\n" .

        "Email of sender: $email\n\n" .

        "Name of user with problem: $username\n\n" .

        "Department: $department\n\n" .

        "Problem: $problem\n\n" .

        "Priority Level: $priority\n\n" .

        "Has the user rebooted their computer?: $reboot\n\n" .

        "------------------------- PROBLEMS -------------------------\n\n" .

        $comments .

        "\n\n------------------------------------------------------------\n" ;

mail($to, $subject, $messageproper,

        "From: \"$name\" <$email>" . $headersep . "Reply-To: \"$name\" <$email>" . $headersep . "X-Mailer: chfeedback.php 2.07" );

header( "Location: $thankyouurl" );

exit ;

?>

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24149574
You must call the function:
$num = next_num();

$subject = "Workorder Request #$num - Wyandotte" ;

Open in new window

0
 

Author Comment

by:Wyandotte
ID: 24149652
Wow, that works GREAT!!! THANKS!!! Now say i want to start the numbers at 300, which part of the function would I change?
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24149874
Just change the file directly, it is a "normal" text file.
0
 

Author Comment

by:Wyandotte
ID: 24150040
IC. VERY COOL. Thanks a million.
0
 

Author Closing Comment

by:Wyandotte
ID: 31570448
Thanks again.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now