Jump to content

Drop down menu


ScarfaceJM

Recommended Posts

What's up guys! New to the forum sorry my first post is asking for help, but here's what I got.  I have a database setup as follows:

 

id = 1

Name = sample

Level = 99

Status = sampleStatus

Updated = dateLastUpdated

 

I have a page setup that gets users from the database and puts them into a drop down list.  Just the names not the enter info.

<?php
require 'connect.php';

$database = 'member_list';
mysql_select_db($database) or die("Could not connect to database: ".mysql_error());

$query = "SELECT id, Name FROM members ORDER BY Name ASC";
$query_result = mysql_query($query) or die("Query failed".mysql_error());

while($row = mysql_fetch_array($query_result)) {
	$id = $row['id'];
	$name = $row['Name'];
	
	echo "<option value=\"$id\">
        $name 
    	</option>";
}


?>

Now what I would like to do, is have it setup where when I click on a name from this menu, it will populate a table with all the users information.  So if I click on Todd his info will populate a table.  Then when I click on another name in the list, Todds info will erase and Tom's info will now populate the table.  I don't know if it can be done with php as I'm fairly new and still learning.

 

 

 

Link to comment
Share on other sites

To strictly use PHP you could wrap the <select> in a <form>, add a Submit button and then use the value of the <option> selected to run your SELECT statement against.

<form method="post">

    <select name="username">
        <option value="ScarfaceJM">ScarfaceJM</option>
        <option value="iarp">iarp</option>
    </select>
    <input type="submit" name="submit" value="Submit" />
</form>

The selected username would then be available only after submission by:
$username = $_GET['username'];

The other way would be via jQuery or javascript in some form. That would allow you to stay on the page and load data in the background.

<script type="text/javascript">

    $(document).ready(function(){
        $('select[name="username"]').on('click', function() {
            // do something ajaxy like and load it from another source maybe?
            console.log('i am in here');
        });
    });

</script>
Edited by iarp
Link to comment
Share on other sites

iarp gave you some good examples of how to do this but did include a small error in his code.

 

The form he coded up for you uses the POST method. Notice? Then in the ensuing PHP code he showed you how to capture the contents of the dropdown (from the name attribute of the select tag) by grabbing the element from the GET superglobal. Problem is the data will not be in GET since your form is doing a POST. Swap out his use of $_GET for $_POST.

  • Like 1
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.