Solved

Increment number for basic PHP website email form

Posted on 2009-04-15
12
430 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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
 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

831 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