Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a Counter on a form 2

Status
Not open for further replies.

abbottboi

IS-IT--Management
Nov 14, 2005
94
CA
Hi,

I was wondering if its possible to add a counter to a form using PHP. Currently my office uses an online form as a short-term solution for a call ticket system for technical support.

With its function in mind i've created a basic online form but would now like it to assign a unique ID number to the email that it sends once submitted. The starting number should be "2007-001"

If this is possible using some short of simple PHP please let me know. :)
 
Hey jpadie (aka genius)

I have my form built in HTML how would i convert it? The website i designed is very complex so i wouldn't want to have to redo the whole thing so could i just add in some PHP script without having to make the entire page a PHP page?
 
Yes, you could have a file that keeps the current counter number, pull it out and then increase it append to to the ID a and send the mail. then resave the counter to the file with its new value.

Code:
[green]File to save counter in[/green]
$filename = "counter.cnt";
[green]Open file[/green]
$handle = fopen($filename, "r");
[green]Get the current number of our counter[/green]
$fcontents = fread($handle, filesize($filename));
fclose($handle);
[green]increase counter by one[/green]
$mycounter=$fcontents+1;
[green]Create unique ID with counter[/green]
$ID="2007_" . $mycounter;
[green]Open file to save new value[/green]
$handle = fopen($filename, "w");
$fcntr=$mycounter;
[green]save new value[/green]
fwrite($handle,$fcntr);
fclose($handle);

[green]The Current ID is now stored in $ID to be used anywhere. [/green]

This is not meant to be working code, only a rough example. check for errors and inconsistencies.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
OK sounds great... but unfortunently i need u to walk me through how to add this to my page. Where do i insert this code? I currently have PHP enabled. And don't have any knowledge on how to program PHP. :(
 
Wherever the code is that sends the email, just stick in before that, and use the $ID.

I'm assuming that whatever sends the email, is a PHP page.

to make it simpler for you i'll turn it into a form you can call.

Code:
function unique_id()
{
$filename = "counter.cnt";
$handle = fopen($filename, "r");
$fcontents = fread($handle, filesize($filename));
fclose($handle);
$mycounter=$fcontents+1;
$ID="2007_" . $mycounter;
$handle = fopen($filename, "w");
$fcntr=$mycounter;
fwrite($handle,$fcntr);
fclose($handle);
return $ID;
}


Place it at the top of the page that sends the mail.

and then call it before you send the mail.

$myId=unique_ID();

and add it to the message body. Without seeing your code, I can't be anymore specific than that.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
<form action=" method="post" id="someid" onsubmit="return validate(this)">
<p>
<input type="hidden" name="mailer" value="mail.domain.ca" />
<input type="hidden" name="from_address" value="callticket@domain.ca" />
<input type="hidden" name="to_address" value="to@domain.ca" />
<input type="hidden" name="subject" value="CALL TICKET" />
<input type="hidden" name="redirect" value=" />
</p>

I'm not using a PHP mailer but rather a CGI one. The form is on a plain HTML page.

what you think?
 
O.k from that, there is no place that PHP would actually run to create the ID and then add it to the email.

You'll have to change something:

There are several options:

1. You would have to change the form action to pass through a PHP page and then send it to the mailer.cgi.

2.Alter the CGI mailer to create itself the ID before sending the mail.

3. Use either phpmailer of mail from PHP.

4. Make your form a PHP page instead of just html, and include the ID as a hidden value fo the form. This mind you is the easiest one.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
great, any easy step in how to convert my existing html doc into a PHP one?
 
just change the extension from html to php. mypage.html to mypage.php

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Ok.. so now that i have my html doc saved as a php doc. where do i insert the: [$uid = uniqid(2007_01", true);]?

Please let me know, thanks

<form action=" method="post" id="someid" onsubmit="return validate(this)">
<p>
<input type="hidden" name="mailer" value="mail.domain.ca" />
<input type="hidden" name="from_address" value="callticket@domain.ca" />
<input type="hidden" name="to_address" value="to@domain.ca" />
<input type="hidden" name="subject" value="CALL TICKET" />
<input type="hidden" name="redirect" value=" />
</p>
 
I would put it before the for like this:

Code:
[red]<?
$uid = uniqid(2007_01", true);
?>[/red]
<form action="[URL unfurl="true"]http://domain/cgi-bin/email.cgi"[/URL] method="post" id="someid" onsubmit="return validate(this)">
        <p>
          <input type="hidden" name="mailer" value="mail.domain.ca" />
          <input type="hidden" name="from_address" value="callticket@domain.ca" />
          <input type="hidden" name="to_address" value="to@domain.ca" />
          <input type="hidden" name="subject" value="CALL TICKET" />
[red]<input type="hidden" name="uniqueid" value="<? echo $uid;?>" />[/red]
          <input type="hidden" name="redirect" value="[URL unfurl="true"]http://www.domain.ca/thanks.html"[/URL] />
       </p>



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Parse error: parse error, unexpected T_STRING in /mysite/callticket.php on line 186


<?
line 186: $uid = uniqid(2007_01", true);
?>
 
Check your quotes. We type our code directly into the browser and sometimes we make silly mistakes. As you can see the literal values need be quoted.
Code:
<?php
  $uid = uniqid("2007_01", true);
?>
 
COOL! so it worked!

displays this: uniqueid: 2007_014491755d42b5a8.69135021

I was hoping they would count.. meaning the next one for go 2007_02 and so on.

Can i get rid of all the extra numbers tho?
 
OH! i should say that the 2007_001 is supposed to represent the number of call tickets in the year 2007 starting with 001 and so on.. so maybe it shouldn't be a unique ID but rather a count up kind of thing.

Thanks guys, you've been soo much help.
 
Then use my code, it does exactly what you want.

here it is again:

me said:
<?
function unique_id()
{
$filename = "counter.cnt";
$handle = fopen($filename, "r");
$fcontents = fread($handle, filesize($filename));
fclose($handle);
$mycounter=$fcontents+1;
$ID="2007_" . $mycounter;
$handle = fopen($filename, "w");
$fcntr=$mycounter;
fwrite($handle,$fcntr);
fclose($handle);
return $ID;
}
?>

Place it at the very top of your form page even before the <html> tags

and then call it right before the form :

Code:
<?
$uid = unique_id();
?>
<form action="[URL unfurl="true"]http://domain/cgi-bin/email.cgi"[/URL] method="post" id="someid" onsubmit="return validate(this)">
...

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Givwes me this error, any comments?

Warning: fopen(counter.cnt): failed to open stream: No such file or directory in /home/advancement/final_adv/callticket.php on line 5

Warning: filesize(): Stat failed for counter.cnt (errno=2 - No such file or directory) in /home/advancement/final_adv/callticket.php on line 6

Warning: fread(): supplied argument is not a valid stream resource in /home/advancement/final_adv/callticket.php on line 6

Warning: fclose(): supplied argument is not a valid stream resource in /home/advancement/final_adv/callticket.php on line 7

Warning: fopen(counter.cnt): failed to open stream: Permission denied in /home/advancement/final_adv/callticket.php on line 10

Warning: fwrite(): supplied argument is not a valid stream resource in /home/advancement/final_adv/callticket.php on line 12

Warning: fclose
 
It is not being able to create the file to store the counter. can you create it manually?

Just upload a text file called whatever you want that has only a "0" zero, and change the $filename variable in my code to match whatever its name is.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top