I'm trying to use a submit button not to submit form data gathered elsewhere on the page, but to post the page to itself and to increment a variable called $selection, which will be used to register the choice in the appropriate database column (page will eventually have two buttons, YES and NO to choose between), and also load the next graphic on the page. Can anyone tell me what's wrong with the code below? I've tried many different iterations of this and am having no success getting the variable to increment when the submit button is clicked. Any help would be greatly appreciated.

<?php
$selection = 0;
$image_folder = "images/";
if (isset($_POST['selection'])) {

   ....code to update db and load next graphic....
}

echo "<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=post>";
echo "<input type=hidden name=selection value=\"" . ($selection + 1) . "\">";
echo "<input type=submit name=subnext value=\"YES\">";
?>

    Welcome to PHPBuilder! When posting PHP code, please use the board's [noparse]

    ..

    [/noparse] bbcode tags as they make your code much easier to read and analyze.

    As for your issue, can you show us what code you're using inside this if() statement:

    if (isset($_POST['selection'])) {
    
    // Where's the code?? You left out the most important part!
    }

    ?

      Thanks for the reply, and apologies for skipping the tags, will include them in the future.

      The original post below was a single button pared down version of what I'm trying to get to work. Here's the code for the YES button to write a 1 to the database and increment the $selection variable by one on button click. Apologies if my coding is 'clunky', new to PHP and figured I'd make it more efficient if I got the basic button increment working and could see that the submit button would function as I'd hoped, but that hasn't happened yet...

      The student_id is entered on the previous page and used as the row identifier for the table

      
      
      <?php
      $selection = 0; 
      $PER="PER".$selection;
      if (isset($_POST['selection'])) {
      
      mysql_query ("UPDATE survey SET $PER=1 WHERE student_id='$student_id'");
      
         }
      
      
      echo "<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=post>";
      echo "<input type=hidden name=selection value=\"" . ($selection + 1) . "\">";
      echo "second selection is" . $selection;
      echo "<input type=submit name=subnext value=\"Next\">";
      
      // this section below assigns the image to be displayed based on the value of the counter
      
      if ($PER==0) $img_src="walking.JPG";
      if ($PER==1) $img_src="sleeping.JPG";
      if ($PER==2) $img_src="eating.JPG";
      
      ?>
      
      <!-- This is the html to display the graphic -->
      
       <p><img src="TESTimages/<?php echo $img_src; ?>" width="900" height="550" align="center" /></p>
      
      

        You never use the value in $_POST['selection']; instead, you only ever use $selection which is simply hard-coded to 0:

        $selection = 0; 

        and never changed.

        In addition, this:

        $PER="PER".$selection;

        looks awfully fishy. Why do you have numbered columns in your SQL database, and what is the purpose of them? Sounds like you might want to read a few articles about database normalization as a redesign is likely in order.

          Well the contents of the database are going to be exported into either a CSV or Excel format for analysis, and this way the results of each question in the PER category will be listed under the appropriate column. So for an easy read of the spreadsheet, the answer to the first question in the Personal section will be listed under column PER1, second question under PER2.... etc.

          I have the database part working OK, I just can't seem to get the variable to increment each time either of the submit buttons is clicked. When I couldn't get my version to work, I searched for examples that used the submit button to post the page to itself and increment a counter (variable) and that $_POST['selection'] was part of a script that was supposed to work, though I was never able to get it to and probably shouldn't have integrated it into my script. I've tried many ways and have had no success. Would appreciate any suggestions.

            Got this basic version of the increment counter shown below to work using SESSION variable and a single submit button. I'm wondering if my issue all along was losing the value of the $selection variable in the hidden attribute when the submit button was clicked and the page was posted to itself?

            
            <?php
            
            $_SESSION['counter'] = (!$_SESSION['counter']) ? 0 : $_SESSION['counter'];
            
            if($_POST['submit']) {  $_SESSION['counter']++; }
            
            echo " session counter after if is " . $_SESSION['counter'];
            
            ?>
            
            <!-- form -->
            
            <form name="input" action="" method="post" />
            
            <input type="submit" name="submit" value="submit" /
            
            

            I get an initial error "Undefined index: submit" befor the first button click because ($_POST['submit']) is undefined. But after the first click, the error disappears and the counter works fine.

              Sandy1;10993626 wrote:

              Got this basic version of the increment counter shown below to work using SESSION variable and a single submit button.

              Really? You must not have shown us all of the code, then, because you're missing a call to [man]session_start/man.

              Sandy1;10993626 wrote:

              I'm wondering if my issue all along was losing the value of the $selection variable in the hidden attribute when the submit button was clicked and the page was posted to itself?

              Well yes, that was indeed the source of your problem. As I previously explained, you never attempted to use the value of that hidden attribute after the form was submitted.

              Sandy1;10993626 wrote:

              I get an initial error "Undefined index: submit" befor the first button click because ($POST['submit']) is undefined.

              You'll actually get two such errors if you start a new session, since $SESSION['counter'] won't exist either yet you still attempt to access it.

              You should always check to see if external data exists before you attempt to access it. In other words, this:
              code=php[/code]is not a good way to check if a variable exists; instead, you should use something like [man]isset/man or [man]empty/man.

                Thanks for the feedback, very helpful. Here's my new code with no errors:

                
                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                <?php session_start(); ?>
                <html xmlns="http://www.w3.org/1999/xhtml">
                <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title>Untitled Document</title>
                </head>
                
                <body>
                
                <?php
                
                if (!isset($_SESSION['counter'])) $_SESSION['counter'] = 0;  //initialize session counter to zero
                
                if (isset($_POST['submit'])) {  $_SESSION['counter']++; }  //increments counter by 1 when submit button is clicked
                
                echo " session counter is " . $_SESSION['counter'];  //echoes current value of counter
                
                ?>
                
                <form name="input" action="" method="post" />
                
                <input type="submit" name="submit" value="submit" />
                
                </body>
                </html>
                
                

                  Note that your code above will only work on servers where output buffering is enabled since [man]session_start/man causes several HTTP headers to be sent. In other words, it should always be called before any output.

                    bradgrafelman;10993750 wrote:

                    In other words, it should always be called before any output.

                    Can you elaborate on the specifics of this a bit more? Sounds like more is needed than declaring the session at the beginning of the code?

                      Any output causes PHP to first send the HTTP headers (which it delays doing as long as possible). Since [man]session_start/man needs to modify that set of headers, you must call it before the headers are actually sent to the client (you can't change the headers once they've already been sent, of course).

                      Output buffering adds an extra layer of "buffer" so to speak; as long as you don't output more data than what will fit in the output buffer, PHP delays sending that output and instead stores it in memory. However, since output buffering isn't always enabled, you shouldn't rely on this and should instead write the code properly (such as by not trying to modify HTTP headers at a point where they should already have been sent).

                        Write a Reply...