0

Button to click

 <form action='' method='POST'>
                 <input type='submit' name='submit' />
     </form>

Variable to be incremented on button click

$currentid="1";
if(isset($_POST['submit'])){

$sql = "SELECT * from TABLENAME WHERE id='$currentid'  LIMIT 1";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"].  "<br>";
        //$currentid=$row["id"];
        $currentid=$currentid+1;
        echo "$currentid";
    }
} else {
    echo "0 results";
}
}

Task:

I want to increment $currentid="1" by 1 each time the button is clicked. This will work same like php pagination but just with Next Button. and on each button click next record will be displayed.

I would appreciate if you can suggest any other better way to achieve the same goal.

10
  • Never use name='submit' if you ever want to script the form submit - also why use a while if you limit to one row? Commented Jun 20, 2016 at 18:39
  • @mplungjan Why not? Commented Jun 20, 2016 at 18:41
  • this is just a sample code. off course i will change it in production. I am just looking for logic to acheive the goal Commented Jun 20, 2016 at 18:41
  • Because you will break the form.submi() if for has an element called submit Commented Jun 20, 2016 at 18:41
  • 4
    And use a session var to hold the counter and current id: stackoverflow.com/questions/17494444/… Commented Jun 20, 2016 at 18:59

1 Answer 1

0

Assuming that you know the starting value of currentid when the form is initiated, I would suggest you do this with a hidden input value, that is incremented by Javascript and used as a parameter to the PHP script.

Sign up to request clarification or add additional context in comments.

5 Comments

please give sample code.. I dont know java.
Java is NOT JavaScript.
I dont know Javascript as well... :) .. that why i requested a sample code :)
You do not need JS. You need a session variable and you need to index yourself into your sql table
masters,, if you can give me sample code,, that will help me under stand the concept better :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.