Jump to content

Recommended Posts

I would like to fetch data from database when item is selected from dropdown list and show it instantly without refreshing the site.

 

Sample:

 

<select>

<option> something </option>

<option> something2 </option>

<option> something3 </option>

</select>

 

<div>shows mysql database results here, depends on item we have chosen</div>

Link to comment
https://forums.phpfreaks.com/topic/250767-dropdown-database-select/
Share on other sites

Something like this could work

 

for($x=0; $x<=$items; $x++) 
{
$id=$result[$x]['pol_id'];
$thing=$result[$x]['pol_policy_number'];
    	$options.="<OPTION VALUE=\"$id\">".$thing;



}
?>
Select a case number to see the payout approval.
<br>
<form action="approval.php" method='post'>
<SELECT NAME=pol_policy_number>
<?=$options?>
</SELECT> 
<input name="Submit" type="submit" Value="Submit Policy Number"/>
</form>
<br>

 

You'll have to put the count of an array in $items.  And change the Policy information to match your database.

 

As to your second quetions,

 

Just use an include statement like

 


include security.inc

 

Security.inc would consist of the username and password.

Just use an include statement like

 


include 'security.inc';

 

Security.inc would consist of the username and password.

 

^^^ I hope you are not using that, because if you browse to the .inc file, you can see the username and password. You need to use a .php file so that the php code defining the username/password will not be literally output if someone browses to the file.

I was not aware of that.  I inherited this system from someone who is much smarter than I am.  I'm not even sure what the passwords to our system are.  I did browse to the link though, and it worked.  Luckily, our passwords were not stored there.

 

Our config files for the website are actually stored in /etc/something/something. 

I would like to fetch data from database when item is selected from dropdown list and show it instantly without refreshing the site.

 

Sample:

 

<select>

<option> something </option>

<option> something2 </option>

<option> something3 </option>

</select>

 

<div>shows mysql database results here, depends on item we have chosen</div>

 

What you are wanting is a simple ajax request that fires on the select's onSelect event. I would recommend using jquery or some other related javascript framework. Writing this using jquery would be very easy. For example:

 

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#select").change(function(){
        $.ajax(
            "file.php?selected=" + $(this).val(),
            "success":function(data){
                $("#results").html(data);
            }
        ) 
    })
});
</script>
<select id="select">
<option> something </option>
<option> something2 </option>
<option> something3 </option>
</select>

<div id="results">shows mysql database results here, depends on item we have chosen</div>

 

Whenever you select a new option in the drop down an ajax GET request will be sent to file.php with the value of that option in the GET param "selected". In the file.php you would access it via $_GET['selected']

 

 

@thomasw_lrd

You forgot got the close the option tags in the code you posted.

@thomasw_lrd

You forgot got the close the option tags in the code you posted.

 

I copied the code from my production server.  Can you explain a little further?  It's working, so I'm not sure what I didn't close, but I would like to fix it. 

 

Not trying to highjack the thread by the way, just trying to learn. 

@thomasw_lrd

You forgot got the close the option tags in the code you posted.

 

I copied the code from my production server.  Can you explain a little further?  It's working, so I'm not sure what I didn't close, but I would like to fix it. 

 

Not trying to highjack the thread by the way, just trying to learn. 

 

you have the following:

<?php
for($x=0; $x<=$items; $x++) 
{
$id=$result[$x]['pol_id'];
$thing=$result[$x]['pol_policy_number'];
    	$options.="<OPTION VALUE=\"$id\">".$thing;



}
?>

 

where it should be:

 

<?php
for($x=0; $x<=$items; $x++) 
{
$id=$result[$x]['pol_id'];
$thing=$result[$x]['pol_policy_number'];
    	$options.="<OPTION VALUE=\"$id\">".$thing . "</OPTION>";



}
?>

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.