Link to home
Start Free TrialLog in
Avatar of Wyandotte
WyandotteFlag for United States of America

asked on

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

Avatar of justin-clarke
justin-clarke

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.
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.
Avatar of Wyandotte

ASKER

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.
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

You must call the function:
$num = next_num();
$subject = "Workorder Request #$num - Wyandotte" ;

Open in new window

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