Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    you could use ajax to call and submit

  • #3
    New Coder
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    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 :)

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    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 :)

  • #7
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    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.

  • #8
    New Coder
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #9
    New Coder
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #10
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    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 :)

  • #11
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •