Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 436
  • Last Modified:

Increment number for basic PHP website email form

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
Wyandotte
Asked:
Wyandotte
  • 6
  • 4
  • 2
1 Solution
 
justin-clarkeCommented:
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
 
Roger BaklundCommented:
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
 
WyandotteAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
justin-clarkeCommented:
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
 
WyandotteAuthor Commented:
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
 
Roger BaklundCommented:
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
 
WyandotteAuthor Commented:
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
 
Roger BaklundCommented:
You must call the function:
$num = next_num();
$subject = "Workorder Request #$num - Wyandotte" ;

Open in new window

0
 
WyandotteAuthor Commented:
Wow, that works GREAT!!! THANKS!!! Now say i want to start the numbers at 300, which part of the function would I change?
0
 
Roger BaklundCommented:
Just change the file directly, it is a "normal" text file.
0
 
WyandotteAuthor Commented:
IC. VERY COOL. Thanks a million.
0
 
WyandotteAuthor Commented:
Thanks again.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now