Jump to content

HTML form submit to same page help


typicalalex1

Recommended Posts

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>

Link to comment
Share on other sites

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 by Drongo_III
Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.