Hit Counter on a button | Newbie | | Join Date: Jun 2007
Posts: 8
| | |
Greetings, newbie here. I have a PHP page where someone will submit there resume by entering their name, email address, attach a file and click the submit button. What i need is an invisible hit counter on the "submit" button that will show how many times the "submit" button has been clicked. Any ideas? Thanks for any help. Much appreciated.
Below is the code i am currently using...[php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<html>
<head>
<title>Submit Your Resume</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
// we'll begin by assigning the To address and message subject
$to="resume@job.ca";
$subject="Sending My Resume From job.ca";
// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_ran d())."x";
// store the file information to variables for easier access
$tmp_name = $_FILES['filename']['tmp_name'];
$type = $_FILES['filename']['type'];
$name = $_FILES['filename']['name'];
$size = $_FILES['filename']['size'];
// here we'll hard code a text message
// again, in reality, you'll normally get this from the form submission
$message = "Please find my resume attached: $name";
// if the upload succeded, the file will exist
if (file_exists($tmp_name)){
// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){
// open the file for a binary read
$file = fopen($tmp_name,'rb');
// read the file content into a variable
$data = fread($file,filesize($tmp_name));
// close the file
fclose($file);
// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}
// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
// next, we'll build the message body
// note that we insert two dashes in front of the
// MIME boundary when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content and set another boundary to
// indicate that the end of the file has been reached
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
// now we just send the message
if (@mail($to, $subject, $message, $headers))
echo "Thank you. Your message has been sent.";
else
echo "Failed to send";
}
} else {
?>
<p><img src="http://www.is2.ca/web_images/is2logo.jpg" alt="is2 logo" width="159" height="100" align="baseline" /></p>
<p>To submit your resume to the branch, please enter your name, email address and attach your resume below:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data" name="form1">
<p>From name: <input type="text" name="fromname"></p>
<p>From e-mail: <input type="text" name="fromemail"></p>
<p>File: <input type="file" name="filename"></p>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
<?php } ?>
</body>
</html>[/php]
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 913
| | | re: Hit Counter on a button
PLEASE!!! Put code tags around your post. Click Edit on your last post and put [PHP ] [/PHP ] (remove spaces) around all that code! Then we will read it.
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: Hit Counter on a button warning:
Please enclose your posted code in [code] tags (See How to Ask a Question).
This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.
Please use [code] tags in future.
MODERATOR
| | Member | | Join Date: Jul 2006
Posts: 92
| | | re: Hit Counter on a button
Hi Burgs, welcome to bytes.
Please explain what is it that you want to achieve
| | Newbie | | Join Date: Jun 2007
Posts: 8
| | | re: Hit Counter on a button
Sorry about the code tags, i had no idea. Like i said i'm new to this site. So what i'm trying to acheive is a hidden counter or a counting system of some sort that will let me know how many times the submit button has been clicked. Any ideas....please? Anybody?
| | Member | | Join Date: Jul 2006
Posts: 92
| | | re: Hit Counter on a button Quote:
Originally Posted by burg226 So what i'm trying to acheive is a hidden counter or a counting system of some sort that will let me know how many times the submit button has been clicked Yes, that was evident from your first post but what I'm trying to find out is why you want to count the hits. Perhaps there are alternative solutions to your problem than counting hits?
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: Hit Counter on a button Quote:
Originally Posted by burg226 Sorry about the code tags, i had no idea. Like i said i'm new to this site. So what i'm trying to acheive is a hidden counter or a counting system of some sort that will let me know how many times the submit button has been clicked. Any ideas....please? Anybody? Okay, clear. Now let's get more practical.
Do you want to count the submit hits just for the session of that particular user or do you want to count all submits by all users of your site? In the first case you can keep the counter in storage and it will be destroyed when the user logs out or the browser session ends.
In the second case you have to keep the counter in a file or database and that requires some server side actions.
Ronald
| | Newbie | | Join Date: Jun 2007
Posts: 8
| | | re: Hit Counter on a button Quote:
Originally Posted by ronverdonk Okay, clear. Now let's get more practical.
Do you want to count the submit hits just for the session of that particular user or do you want to count all submits by all users of your site? In the first case you can keep the counter in storage and it will be destroyed when the user logs out or the browser session ends.
In the second case you have to keep the counter in a file or database and that requires some server side actions.
Ronald I need it to count all submits by all users of the site...on-going thanks. Any ideas???
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: Hit Counter on a button
So you will store your counters in a database, right? Now there are 2 ways of doing this:
1. the 'catching' script, i.e. the script that receives the posted values, first updates the counter stats in the database and then processes the posted values.
2. after submit use Ajax to go to the server, update the stats and then submits the form. For this one the process would be:
a. user fills in form
b. user hits submit button
c. submit button hit routed to JS routine, which:
- uses Ajax to go to server hit counter update routine
- returns to issuing JS routine
- JS routine submits form
I can show the setup code for the latter. I will not go into your code you have shown. You'll have to write your own form and form handling code. Interested?
Ronald
| | Newbie | | Join Date: Jun 2007
Posts: 8
| | | re: Hit Counter on a button Quote:
Originally Posted by ronverdonk So you will store your counters in a database, right? Now there are 2 ways of doing this:
1. the 'catching' script, i.e. the script that receives the posted values, first updates the counter stats in the database and then processes the posted values.
2. after submit use Ajax to go to the server, update the stats and then submits the form. For this one the process would be:
a. user fills in form
b. user hits submit button
c. submit button hit routed to JS routine, which:
- uses Ajax to go to server hit counter update routine
- returns to issuing JS routine
- JS routine submits form
I can show the setup code for the latter. I will not go into your code you have shown. You'll have to write your own form and form handling code. Interested?
Ronald Either of these sound great to me. Show me the code. The only problem is I have no idea how to incorporate this into my code. Would anyone be willing to help me on this?
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: Hit Counter on a button
The 1st option is the easiest. It starts in your code somewhere after statement 9. It updates a counter (and nothing else!) in table 'hits'. You can work out the adaptation of that code yourself[php]if ($_SERVER['REQUEST_METHOD']=="POST"){
/** ----------------------------------------------
* increment the hit counter in the database
* ----------------------------------------------- */
// open server connection and select database
$conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS)
or die("Cannot connect to server: ".mysql_error());
mysql_select_db(SQL_DB,$conn)
or die("Cannot select database: " . mysql_error());
// update the hit counter in table 'hits'
$result=mysql_query("UPDATE hits SET counter=counter+1")
or die("Cannot increment hit counter: " . mysql_error());
mysql_close($conn);
// ..... the rest of your code ..............
//
// we'll begin by assigning the To address and message subject[/php]Ronald
| | Newbie | | Join Date: Jun 2007
Posts: 8
| | | re: Hit Counter on a button Quote:
Originally Posted by ronverdonk The 1st option is the easiest. It starts in your code somewhere after statement 9. It updates a counter (and nothing else!) in table 'hits'. You can work out the adaptation of that code yourself[php]if ($_SERVER['REQUEST_METHOD']=="POST"){
/** ----------------------------------------------
* increment the hit counter in the database
* ----------------------------------------------- */
// open server connection and select database
$conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS)
or die("Cannot connect to server: ".mysql_error());
mysql_select_db(SQL_DB,$conn)
or die("Cannot select database: " . mysql_error());
// update the hit counter in table 'hits'
$result=mysql_query("UPDATE hits SET counter=counter+1")
or die("Cannot increment hit counter: " . mysql_error());
mysql_close($conn);
// ..... the rest of your code ..............
//
// we'll begin by assigning the To address and message subject[/php]Ronald Awesome thanks. Now if i could only figure out MYSQL we'd be in business. I have an SQL database but have no idea how to access, modify or use it.
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: Hit Counter on a button
Then you'd better first determine what sort of SQL database (Oracle, DB2, MySQL, PostgreSQL, etc.) and then follow some beginner's tutorial on that database. You'll find many of those on the web.
Ronald
| | Newbie | | Join Date: Jul 2008
Posts: 1
| | | re: Hit Counter on a button
Hiiiii all,
I am so excited to share a very good sitecounters website with you all that i found over the internet. This has so many different kind of counter style, play, music, fashion, computers etc. But the good thing is this, you can make your own sitecounters. You just have to chose font color, style and background color.
I had a tough time to find out a pale background color sitecounters for my website but i found the following site and made my own.
http://www.sitecounters.info/
You can also see the demo on the following URL.
http://www.sitecounters.info/pages/demo.php
This sitecounters site also provide a very good and real time statistic.
i would share more sitecounters site as i will find more good then this one
<table border="0" cellspacing="0" cellpadding="0"><tr> <td colspan="2"><a href="http://www.sitecounters.info" target="_blank"><img src="http://sitecounters.info/pages/counter.php?id=78" border="0" alt="www.sitecounters.info"></a></td> </tr> <tr class="style1" style="CURSOR: hand" onClick='window.open("http://www.sitecounters.info/admin/");'> <td valign="top" bgcolor="#B4CE42"><div align="center"><font size="1" face="Arial, Helvetica, sans-serif">COUNTER</font></div></td> <td valign="top" bgcolor="#3D1DA4"><div align="center"><font color="#FFFFFF" size="1" face="Arial, Helvetica, sans-serif">STATS</font></div></td> </tr> </table>
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,392 network members.
|