typicalalex1 Posted February 8, 2013 Share Posted February 8, 2013 Hi guys. I have been learning PHP for a couple of months now for a project at work. I need to display database records on a HTML form which i have managed to do. I have added 2 buttons that allow the users to select the next or previous record. This works fine. The form is setup to submit to the same page the form is held on. As you can see, i have setup 2 if statements at the start. These check to see what button was pressed (next or previous). I want to know if i have done this using the best method? From what i can tell, everytime Next or Previous is pressed, the entire webpage is recreated. Is there a better way of doing this or have i done this correctly? I have removed most of the code from this example because there are a lot more textboxes and other controls on the form. #CONNECT TO SQL #SELECT DATABASE $query = "select * from tblEmployees where employeeID = $i"; $rs = mysql_query($query); $row = mysql_fetch_array($rs); #IF NEXT WAS PRESSED if (isset($_POST['btnNext'])){ $i = $_POST['txtEmployee']; $i++; $query = "select * from tblEmployees where employeeID = $i"; $rs = mysql_query($query); $row = mysql_fetch_array($rs); } #IF PREVIOUS WAS PRESSED if (isset($_POST['btnPrevious'])){ $i = $_POST['txtEmployee']; $i--; $query = "select * from tblEmployees where employeeID = $i"; $rs = mysql_query($query); $row = mysql_fetch_array($rs); } ?> <form action="" method="post" id="frmMain" style="display:inline-block;background-color:lightgray" align ="center"> <table> <tr> <input name="txtEmployee" type="text" size="22" maxlength="30" value= <?php echo $i ?>> <td><p>Title:</p></td> <td><input type="text" style="border:1px solid" name="txtTitle" size="5"></td> <td><p>First Name:</p></td> <td><input name="txtFirstName" style="border:1px solid" type="text" size="15" value='<?php echo $row['firstName'] ?>'></td> <td><p>Surname:</p></td> <td><input name="txtLastName" style="border:1px solid" type="text" size="15" value='<?php echo $row['lastName'] ?>'></td> <td><p>Status:</p></td> <td><input name="txtStatus" style="border:1px solid" type="text" size="15" value='<?php echo $row['status'] ?>'></td> </tr> </table> <table> <tr> <td><input type="submit" name="btnPrevious" value="Previous"/></td> <td><input type="submit" name="btnNext" value="Next"/></td> </tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/274210-html-form-submit-to-same-page-help/ Share on other sites More sharing options...
Drongo_III Posted February 8, 2013 Share Posted February 8, 2013 (edited) There are probably lots of ways of acheiving this and if it does what you want then who is to argue? If you would prefer the page didn't refresh itself then you should look into ajax. Just do a google search on 'ajax php' and you will find lots of examples. Ajax uses javascript to get/post data asynchronously enabling data to be exchanged with the server in the background without need to to refresh the page. ajax looks a bit puzzling at first but once you break it down it's very straight forward. If you have any more questions on it after googling just shout. Edited February 8, 2013 by Drongo_III Quote Link to comment https://forums.phpfreaks.com/topic/274210-html-form-submit-to-same-page-help/#findComment-1411075 Share on other sites More sharing options...
typicalalex1 Posted February 11, 2013 Author Share Posted February 11, 2013 Thank you for the reply. Its quite a big project which is going to take up a lot of my time, so i dont want to want a solution without knowing its the best way of doing it. I will have a look into Ajax as well. Thanks for the advice Quote Link to comment https://forums.phpfreaks.com/topic/274210-html-form-submit-to-same-page-help/#findComment-1411696 Share on other sites More sharing options...
typicalalex1 Posted February 11, 2013 Author Share Posted February 11, 2013 Wow this is confusing What i want to do is have a HTML form with several textboxes. These textboxes are populated from a mySQL table when the page opens. I need a way of navigating through the records using 2 buttons, Next and Previous. Im trying to do this using Ajax, but i cant seem to find any good examples. Any chance you can point me in the right direction? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/274210-html-form-submit-to-same-page-help/#findComment-1411707 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.