Devolvera
Devolvera

Reputation: 23

PHP Cycle Through MySQL Table With Submit Button

Okay, I've looked for a solution to this question, but to no avail. It could be the way I worded it, but I've tried almost everything I could think of. And I know this is probably something so simple, I just can't wrap my head around it.

The problem I'm having is this:

I have a form that is prepopulated with data from a MySQL database. This part of the form is strictly for viewing the records (that were previously entered by the user). I can get it to work; I have the database linked, and it shows the data for the row of the particular table that I want. But this is what I'm having trouble with.

I want there to be a button that the user can press that cycles through each row of data in the database, and outputs it in the form accordingly.

It's a little complicated, so I'll use a simple example.

Let's say I have a basic table in a MySQL database with three columns: ID, Name, and EmailAddress.

Here's the query that grabs the data:

$sql = "SELECT * from tbl_Users ORDER BY ID ASC";
$query = mysql_query($sql);
$result = mysql_fetch_array($query);

Now I know this is deprecated, but I am working in an older version of php, and I'm trying to stay consistent other pages/apps in this particular domain.

Now let's say I have a simple form with two inputs and a submit button.

HTML

<form name="button" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="Name" value="<?php echo $row[Name]; ?>" />
<input type="text" name="emailAddress" value="<?php echo $row[EmailAddress]; ?>" />
<input type="submit" name="submit" value="Next Record" />
</form>

Now I can get it to echo what I need in the form. It can prepopulate the form with a row, as I want it to. The trouble lies with the submit button.

When the submit button is clicked, I want the next row to populate in the form, and so on until the end of the records.

I've tried the if(isset($_POST['submit']{...}, yet I don't know exactly to get this to work. I've tried loops, etc. I know there is a way, I just cannot comprehend one right now. Thanks in advanced.

Upvotes: 1

Views: 889

Answers (2)

Devolvera
Devolvera

Reputation: 23

Okay, so I DID manage to get it to work with PHP. I found a simple PHP Pagination script and changed the limit of the query to 1. Instead of echoing out the results of the query in the WHILE loop, I just called the variables for the form

Upvotes: 1

ntgCleaner
ntgCleaner

Reputation: 5985

Unfortunately in your case, PHP is server side, and it will run any queries before the client side has a chance to catch up and output it. I believe you will need to use javascript for this.

You can do one of two things, 1. Pull all table information on load (slower way) and use javascript to show/hide certain elements of that query. Or 2. You can use AJAX to pull the data on command.

I would suggest learning javascript (e.g. use a framework like jQuery) to perform an AJAX call for this.

Upvotes: 0

Related Questions