How to echo 'None' and then run my script

Discussion in 'PHP' started by Ipodman79, May 24, 2014.

  1. #1
    This is what I have,
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>Bale Counter</title>
        <style type="text/css">
        .button {
         padding:50px;
         background-color: #dcdcdc;
         border: none;
         color:#000;
         text-decoration:none;
         font-size: 50px;
        }
        </style>
    </head>
    <body>
    <center>
    <h1>Bales Out: <?php
    session_start();
    
    function back_to_zero() {
      $_SESSION['count'] = -1;
    }
    // if your variable is not yet defined, you assigned it with 0
    if (!isset($_SESSION['count']))
    {
        $_SESSION['count'] = -1;
    }
    if ($_SESSION['count'] === -1) {
        echo 'None';
        $_SESSION['count']++;
    }
    if(isset($_POST['submit']))
    {
        $_SESSION['count']++;
        echo $_SESSION['count'];
    }
    elseif (isset($_POST['back_to_zero'])) {
        back_to_zero();
    }
    ?></h1>
    <form action="index.php" method="post">
        <input type="submit" name="submit" value="Bale Out" class="button">
        <input type="submit" name="back_to_zero" value="Reset" onclick="back_to_zero()" class="button">
    </form>
    </center>
    </body>
    </html>
    
    PHP:
    I want it to display, 'None' beside the <h1> that says 'Bales Out: '. However with my script I currently have it doesn't display the word 'None' unless you press reset a few times. It then also says 'None1' when it should just saw '1'.


    Thanks
     
    Ipodman79, May 24, 2014 IP
  2. TIEro

    TIEro Active Member

    Messages:
    741
    Likes Received:
    177
    Best Answers:
    5
    Trophy Points:
    70
    #2
    Assuming you're on a learning curve here, the best thing to do (rather than just have someone solve it) is to put lots of echo statements in. Echo what's happening and the values at each step (plus a line break) so you can see what's happening and work out the logic (or lack of it!) that php uses.

    I've had similar problems dozens of times (especially when I learned to use sessions... ugh, horrible) and putting in things like "Session started: ID #", "Current vars: count = #, submit = #, back_to_zero = #" and so on REALLY helps.
     
    TIEro, May 24, 2014 IP
  3. mxscripts

    mxscripts Well-Known Member

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    115
    Digital Goods:
    1
    #3
    You must put session_start(); before any output:

    <?php
    session_start();
    ?>
    <!DOCTYPE html>
    ............
     
    mxscripts, May 27, 2014 IP
  4. mikejwatson

    mikejwatson Greenhorn

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    13
    #4
    You've got lots of stuff here that won't work. I can see you're on a learning curve - well done for giving it a shot!!! Here are some things to help you:

    1. You're using onclick="back_to_zero()". This will call the javascript function "back_to_zero" when the button is clicked. However the function you have defined is in PHP. All PHP is server-side language, i.e. it will be processed on the server. You cannot call a PHP function using onlick (unless you use AJAX - this comes later!!)

    2. I think that this statement might be return true every time:
    
    elseif (isset($_POST['back_to_zero']))    
    
    PHP:
    I'm not 100% certain, but as TIEro says, try echoing out your POST variables to the screen . I think you can just try:

    
    print_r($_POST);
    
    PHP:
    you could place this immediately after your session_start() statement.

    I think if you play around with those two ideas you'll get the script working as intended.

    Hope this helps
    Mike
     
    mikejwatson, May 29, 2014 IP
  5. TIEro

    TIEro Active Member

    Messages:
    741
    Likes Received:
    177
    Best Answers:
    5
    Trophy Points:
    70
    #5
    This is one of the biggest bugbears in php: figuring out when variables are set (when it's not you doing the setting). Still catches me out frequently, with things being set when I don't expect it - and not being set when I do! That's why I echo everything, just to see what's going on inside the demented mind of my server... :)
     
    TIEro, May 29, 2014 IP
  6. Jigney

    Jigney Active Member

    Messages:
    168
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    93
    #6
    It's never possible to call a php function on "onclick", You are doing a wrong thing, "onclick" works only for javascript or jQuery.

    Your syntax and logic is wrong, Plz take a look at jquery function and php function call.
     
    Jigney, May 30, 2014 IP