0

Hey guys i am a newbie to php.What problem i am facing is i have created a dropdown which is populated from the data from database using this code.This is working fine for me and it is populating dropdown too

    include('connect.php');
    $query="select * from faculty";
    $result=mysql_query($query);
    while($row = mysql_fetch_assoc($result))
    {$dropdown.="\r\n<option value='{$row['Designation']}'>{$row['Designation']}      </option>";}
    echo "<select>".$dropdown."</select>";

Solution i want is,when a user selects a value from dropdown,result should be retrieved from database and should be displayed in table.Please help me guys

2

2 Answers 2

0

You have to basically :
1) Perform a form sending to some server side script(PHP in your case) when there is a change in the dropdown selection (use onchange event for the dropdown) and fetch the values from the db ,
2) Tell the server side script to spit out an html string which contains the table containing the desired information.3)
3) Output the string on your page.

This will do a page refresh.

If you donot want to have a page refresh, resort to using Ajax. P.S. I recommend using some framework such as jQuery in case you need to use Ajax

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

3 Comments

i ll b very thankfull if u can provide me a link to the tutorial which will help me
Answers should be marked accepted if the problem gets solved. This shows that SO is being useful in solving problems.
0

What you have to do is something like this:

In your Html:

<select onchange="fetchContent()">
    <option id="1_Designation">abcd</option>
    <option id="2_Designation">1234</option>
    <option id="3_Designation">lkjh</option> 
</select>

In your javascript:

fetchContent()
{
    id = $(this).id;
    $.ajax({
      type: "POST",
      url: "/path/content.php?id="+id,
      success: function(response) {
         $("#tableRow").html(response);
      }
   });
}

In content.php you will have to get the value of id and then do the necessary data retrieval and then return the data.

$id = $_POST['id'];
//retrieve the data to $data
echo $data;

1 Comment

Nice way to give a feel of things :) I just wish to enhance this answer by changing: $.ajax({ type: "POST", url: "/path/content.php?id="+id, success: function(response) { $("#tableRow").html(response); } }) to $.post({ "/path/content.php", {id: id}, function(response) { $("#tableRow").html(response); } })

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.