tags:

views:

127

answers:

2

Currently I've got a PHP page query.php with a form that presents a list of countries with a submit button:

Countries
[] nation1
[] nation2
[] nation3
[] nation4
SUBMIT

they're checkboxes to the same array, and the form references the page <form method="post" action="query.php">

When the submit button is pressed, a query is run on the dbase that pulls up a list of actors based upon the nations selected, and creates a new checkbox list below the first one:

Countries
[] nation1
[] nation2
[] nation3
[] nation4
SUBMIT

You have chosen countries ____.

Actors
[] guy1
[] guy2
[] guy3
[] etc
SUBMIT

Currently, that second form calls a new PHP file <form method="post" action="movie-query.php"> which searches the dbase based on the actors selected and returns all movies that contain them.

I was wondering if there's any way to keep this all on one page? I can't figure out how to get the search results to display within query.php when the second submit option is used. If I set the second form to <form method="post" action="query.php"> all it does is reset query.php to its original state of:

Countries
[] nation1
[] nation2
[] nation3
[] nation4
SUBMIT

Rather than something akin to:

Countries
[] nation1
[] nation2
[] nation3
[] nation4
SUBMIT

You have chosen countries ____.

Actors
[] guy1
[] guy2
[] guy3
[] etc
SUBMIT

You have chosen actors _____.

Movie Results
1. movietitle
2. movietitle
3. movietitle
4. etc.

all appearing on the same page.

Any ideas? Thank you.

+2  A: 

Ajax?

If you're not familiar, check out Prototype, jQuery, mootools or Dojo. You can do a request back to your server with the countries selected to get the actor choices, and then do another to get the movies.

I'd recommend using JSON to encode the data. PHP's json_encode() and json_decode() are great.

Ted Pennings
how the heck did you get a woot badge? i hate you. i've been trying forever but noooo...stupid timezones unaligned good. anyway... this answer is a good start anyway. you'll want to use ajax yes, but... have fun figuring that out :D
Mark
+1  A: 

AJAX for usability, but make sure your form works when JS is disabled/unsupported. In this case, you can have a cascade of includes: whenever the input for a form is sent, the form includes the next form.

/* At the end of query.php */
include_once('countryFields.php');
if (isset($_REQUEST['countries'])) {
    include_once('queryActor.php');
}
echo '</form>';

/* At end of queryActor.php */
include_once('actorFields.php');
if (isset($_REQUEST['actors'])) {
    // query results will end up within <form> but that's OK
    include_once('movie-query.php');
}

Each '*Fields.php' generates the fields for a form; for each '*Fields.php', there is a corresponding '*Form.php'.

/* actorForm.php */
<form action="movie-query.php" method="POST" name="actorForm">
    <?php include('actorFields.php'); ?>
</form>
<script type="text/javascript">
    // AJAX stuff here
</script>

In this example, your AJAX-based logic would submit to the '*Form.php' scripts, while your non-JS logic would submit to 'query.php'. You could extend the cascade indefinitely (though this would impact usability). Within a '*Fields.php', check for user input from the fields it creates so you don't lose a step.

/* in actorFields.php */
if (!isset($_POST['countries'])) {
    $_POST['countries'] = array();
}
if (!isset($_POST['actors'])) {
    $_POST['actors'] = array();
}
...
/* Get actors from datastore. Keys of $actors are IDs from the datastore. */
$actors = Actors::forCountries($_POST['countries']);
/* when generating list of checkboxes, check any actors set in $_POST['actors'] 
   For example:
 */
foreach ($_POST['actors'] as $id) {
    $actors[$id]->checked = 'checked';
}
echo '<ul id="Actors">';
foreach ($actors as $id => $actor) {
    echo "<li><label for='actor[$id]'></label><input type='checkbox' name='actors[$id]' value='$id'",($actor->checked ? ' checked': ''),"/>{$actor->name}</li>";
}
echo '</ul>';

Some of what is done in the example 'actorFields.php' is different than what you'd do in production code. For one thing, the code to generate a list view would be in another script.

outis
Thank you. I was indeed concerned about making a page that was usable for people who don't enable JS.
Andrew Heath