1

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.

2 Answers 2

1

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

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

Comments

0

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.

3 Comments

Really? I have a few jQuery UI elements within the page. I would have assumed that my direction would be similar to PHP pagination, except with only 1 record per page and within a form.
Pagination is done two ways: 1. PHP runs and lists the number of pages, when you click on the page number, it refreshes the page and shows a new set of numbers or 2. javascript creates the pagination and when you click, AJAX calls another PHP page somewhere else, then loads the information on that page without refreshing
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.