Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-19-2010, 01:00 AM   PM User | #1
jbird123
New Coder

 
Join Date: Jan 2010
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
jbird123 is an unknown quantity at this point
Problem with form action=""

Right, ive got this script. Its recording how many times a button is pressed. Its been bugging me for hours why it wont work but its just figured it out, but the reason it wont work cannot be easily solved!

Ill keep it as simple as possible. I have something like this:
Code:
<?php 
$logFile = 'countLog.log';
$totalCount = (int)file_get_contents($logFile);
if($_POST['button1']){
$fp = fopen($logFile, 'w');
$totalCount = $totalCount + 1;
fwrite($fp, $totalCount);
fclose($fp);
}
?>
<form method="post">
<input type="submit" name="button1">
</form>
<br/><br/>
Form has been submitted <?php echo $totalCount; ?> times.
(EXAMPLE)

That works. However I have my own form, which is fine, but there is ONE tiny, ESSENTIAL bit that stops it from working..
action="contact.php"
If I simply rmove that bit it counts fine. But as you can guess, i NEED that line, or the form is useless for anything other than counting how many times its been clicked! Thats clearly no good.

Any ideas how I can fix this??

So basicaly, if I keep the bit action="contact.php" then the form submits ok but it wont count, but if I remove that line then it will count ok but it wont submit anything anywhere!

Its a bit hard to explain so im not sure if ive made myself clear. But, please help if you can!!

Thank youuu

Lloyd
jbird123 is offline   Reply With Quote
Old 01-19-2010, 01:35 AM   PM User | #2
seco
Regular Coder

 
seco's Avatar
 
Join Date: Nov 2008
Location: Oregon
Posts: 682
Thanks: 5
Thanked 79 Times in 77 Posts
seco has a little shameless behaviour in the past
you could use ajax to call and submit
seco is offline   Reply With Quote
Old 01-19-2010, 01:58 AM   PM User | #3
jbird123
New Coder

 
Join Date: Jan 2010
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
jbird123 is an unknown quantity at this point
ThaNks for the quick reply. How would o go about that? I've never touched ajax tbh. I'm a bit of a n00b really!
jbird123 is offline   Reply With Quote
Old 01-19-2010, 02:03 AM   PM User | #4
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
Take a look here
http://www.deathmonkeyz.com/tutorials/
There are a few great AJAX tutorials on there
__________________
My site: JayGilford.com
Resources:
PHP Pagination Class | Getting all page links | Handling PHP Errors properly
If you like a users help, show your appreciation with the rep and thanks buttons :)
JAY6390 is offline   Reply With Quote
Old 01-19-2010, 02:21 AM   PM User | #5
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
Increment the count via the section of the code which processes the form rather than at the form display side of things. The likes of AJAX are of no use if someone has javascript disabled.
MattF is offline   Reply With Quote
Old 01-19-2010, 02:23 AM   PM User | #6
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
I see this kind of comment all the time about javascript being disabled, but a vast majority of browsers come with javascript enabled and pretty much every user uses javascript with their browser. You could use the <noscript> tags to display an error message if the user doesn't have javascript enabled to let them know it needs turning on
__________________
My site: JayGilford.com
Resources:
PHP Pagination Class | Getting all page links | Handling PHP Errors properly
If you like a users help, show your appreciation with the rep and thanks buttons :)
JAY6390 is offline   Reply With Quote
Old 01-19-2010, 02:32 AM   PM User | #7
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
Any form of Javascript should be used for enhancement only. There should always be a hard coded method of doing things as a fallback. Plus, there are plenty of scenarios where JS may be disabled. Corporate networks, for example. JS should be used for pretty effects, enhancement etc, but the fundamentals should always work perfectly well without it, not be reliant upon it.
MattF is offline   Reply With Quote
Old 01-19-2010, 09:38 AM   PM User | #8
jbird123
New Coder

 
Join Date: Jan 2010
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
jbird123 is an unknown quantity at this point
Quote:
Originally Posted by MattF View Post
Increment the count via the section of the code which processes the form rather than at the form display side of things. The likes of AJAX are of no use if someone has javascript disabled.
Ahh, I see what you mean. That is something I hadnt thought of yet for some reason! (Mot sure why because Ive had to do the exact same thing with other parts of the same site :S) Ill do a lottle work on that now and se how it goes. Thanks.

Quote:
Originally Posted by MattF View Post
Any form of Javascript should be used for enhancement only. There should always be a hard coded method of doing things as a fallback. Plus, there are plenty of scenarios where JS may be disabled. Corporate networks, for example. JS should be used for pretty effects, enhancement etc, but the fundamentals should always work perfectly well without it, not be reliant upon it.
This is just an enhancement to teh site, everything works perfectly at the moment but we thought it would be nice to count the amount of times the form has been submitted as it makes it look more genuine and shows how popular the page is (its about it get hit by a LOT of traffic soon hopefully)

Thanks for your help guys, ill report back when ive tried to let you you know how it goes
jbird123 is offline   Reply With Quote
Old 01-19-2010, 10:06 AM   PM User | #9
jbird123
New Coder

 
Join Date: Jan 2010
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
jbird123 is an unknown quantity at this point
THANK YOU! I just couldnt work it out and it was bugging me so much but ive got it thanks to this place

I put
Code:
<?php 
$logFile = 'countLog.log';
$totalCount = (int)file_get_contents($logFile);
?>
<?php echo $totalCount; ?>
In the html file. Then I kept action="contact.php" in my form and added
Code:
$logFile = 'countLog.log';
$totalCount = (int)file_get_contents($logFile);
$fp = fopen($logFile, 'w');
$totalCount = $totalCount + 1;
fwrite($fp, $totalCount);
fclose($fp);
To contact.php

So simple but it works! I cant for the life of me think why I couldnt work that out myself, I just got so wound up with it not working lol.

Happy now
jbird123 is offline   Reply With Quote
Old 01-19-2010, 12:52 PM   PM User | #10
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
If you want to count the amount of times it's been submitted accurately just use PHP to do the counting each submission. No javascript required. You could get AJAX to run if it's active and stop the action from sending the form with a refresh (I know this is possible with the onsubmit and returning false to stop it sending like you do with form validations)
__________________
My site: JayGilford.com
Resources:
PHP Pagination Class | Getting all page links | Handling PHP Errors properly
If you like a users help, show your appreciation with the rep and thanks buttons :)
JAY6390 is offline   Reply With Quote
Old 01-19-2010, 02:32 PM   PM User | #11
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
Quote:
Originally Posted by jbird123 View Post
I cant for the life of me think why I couldnt work that out myself
You're not alone. I doubt there's a single coder on here who hasn't had at least one of those moments at some point in time. Glad you got it sorted.
MattF is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:18 AM.


Advertisement
Log in to turn off these ads.