Jump to content

[SOLVED] noob: need help updating page instantly when drop down menu item selected


Recommended Posts

I made a php form that allows the user to select movies from a fairly standard html select menu, but currently you need to select a movie from the drop down list, then hit the submit button before the movie details will be populated to the page.

 

What I'd like to have happen is this: As soon as the user selects a movie from the drop down list, the movie details are populated to the page, without the need to hit a submit button.  It seems like it's a real simple proceedure, but for some reason I cant figure it out...

 

Here's the foreach code that populates the list with movie info:

echo '<div class="container">';
echo '<div>Select a Movie:</div>';
echo '<form method="post" action=?>';
echo '<input type="hidden" name="id" value="' . $id . '" >';
echo '<select class="input" name="id">';
foreach($title_array as $value) {
$id = $value['id'];
$title = $value['title'];
$t = '<option value="' . $id . '"';

if ($value['id'] === $_POST['id'])
	$t .= ' SELECTED';
echo $t . '>'. $title . '</option>\n';	
}
echo '</select><input type="submit" value="Submit"></form></div>';
echo '</div>';

 

Any help would be greatly appreciated.  ;)

echo '<div class="container">';
echo '<div>Select a Movie:</div>';
echo '<form method="post" action=?>';
echo '<input type="hidden" name="id" value="' . $id . '" >';
echo '<select onchange="this.parent.submit()" class="input" name="id">';
foreach($title_array as $value) {
   $id = $value['id'];
   $title = $value['title'];
   $t = '<option value="' . $id . '"';

   if ($value['id'] === $_POST['id'])
      $t .= ' SELECTED';
   echo $t . '>'. $title . '</option>\n';   
}
echo '</select><input type="submit" value="Submit"></form></div>';
echo '</div>';

Thanks Russel for your help. I copied/pasted your code example but it didnt work. I found out why though.

 

After some research on the options for the select menu "onchange" attribute (which I didnt know existed...) I found an example showing this: 

<select name="select" class="tabletxt"  onChange="document.form1.submit()">

 

I adapted it to fit my code by adding name="form1" to the form tag, and and tried it but had a problem. The only thing that was displaying on the page was the select menu. All the movie details (which normally appear below the select menu) were simply not there... When I selected from the drop down, the page refreshed so I could tell that it was functioning properly so I dove into the code again and after some expirementing I found what was causing the movie details to not be displayed.

 

It was the submit button/code. For some reason with the submit button visible, the movie details were invisible. I commented out the submit button but that didnt work, so I changed the submit buttons type from "submit" to "hidden" and viola! It works like a charm.

 

Just FYI for anyone else that may have the same issue... I had no idea it wasnt a PHP issue, so sorry for that, but thanks for the help and thanks for helping me learn.[/code]

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.