Possible Duplicate:
Why does this code not increment a counter properly?
I have a problem when it comes to displaying the correct page number.
I have a demo which you can use to see what is happening:
- Step 1: When you open the app, type in the number 8 in the textbox
and then submit. - Step 2: you will see at the top of the page it states “CREATING
QUESTIONS AND ANSWERS: SESSION 1 OF 8” This is correct as you are
currently on the first page out of 8. - Step 3: Now click on the “Submit Details” button at the bottom, you
will see that it now states “CREATING QUESTIONS AND ANSWERS: SESSION
2 OF 8”. Again this is fine as that you are now on the second page
out of 8.
Below is the problem:
-
Step 4: If you click on the “Add Question” button twice, you will see
2 rows appear below, both rows containing its own image file upload
form. -
Step 5: Click on the “Submit Details” button, you will realise that
you are not on page 3 as it is not stating “CREATING QUESTIONS AND
ANSWERS: SESSION 3 OF 8”, but instead it is stating “CREATING
QUESTIONS AND ANSWERS: SESSION 5 OF 8”
The reason I believe it is doing this is because when you click on the “Submit Details” button, it submits the whole form but also as that there are a couple of image file uploads forms appeanded into a row, it is counting them as well in the submit, so that is why it is stating we are on page 5 and not page 3.
So my question is that what do I need to change in my code in order to be able to display the correct page number? Everytime the user clicks on the “Submit Details” button, it should count the page number by only +1 each time.
Below is the code for how it currently counts the page numbers and how it displays it on top:
<?php
session_start();
if(isset($_POST['sessionNum'])){
//Declare my counter for the first time
$_SESSION['initial_count'] = $_POST['sessionNum'];
$_SESSION['sessionNum'] = intval($_POST['sessionNum']);
$_SESSION['sessionCount'] = 1;
}
else if ($_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
$_SESSION['sessionCount']++;
}
$sessionMinus = $_SESSION['sessionCount'];
?>
…
<h1>CREATING QUESTIONS AND ANSWERS: SESSION <?php echo $sessionMinus ?> OF <?php echo $_SESSION['initial_count'] ?></h1>
In
create_session2.php, you’ll need to differentiate the question adding and submitting of details. For instance, try usingisset($_POST['submitDetails']). This variable will only be set if your user clicked on yoursubmitDetailsbutton to get to the form.Example:
This way, you’ll know whether or not to increment your session amount as a result.