...

View Full Version : Problem with form action=""



jbird123
01-19-2010, 02:00 AM
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:

<?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 (http://c4h.us/test2.php))

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

seco
01-19-2010, 02:35 AM
you could use ajax to call and submit

jbird123
01-19-2010, 02:58 AM
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!

JAY6390
01-19-2010, 03:03 AM
Take a look here
http://www.deathmonkeyz.com/tutorials/
There are a few great AJAX tutorials on there

MattF
01-19-2010, 03:21 AM
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.

JAY6390
01-19-2010, 03:23 AM
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

MattF
01-19-2010, 03:32 AM
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.

jbird123
01-19-2010, 10:38 AM
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.


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
01-19-2010, 11:06 AM
THANK YOU! I just couldnt work it out and it was bugging me so much but ive got it thanks to this place :D

I put
<?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

$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 :D

JAY6390
01-19-2010, 01:52 PM
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)

MattF
01-19-2010, 03:32 PM
I cant for the life of me think why I couldnt work that out myself

You're not alone. :D I doubt there's a single coder on here who hasn't had at least one of those moments at some point in time. :D Glad you got it sorted. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum