Jump to content

[SOLVED] populating a second "select" field


daled

Recommended Posts

is there a way to populate a second select field in a form based on input for a first select without reloading and using only php?

 

Filter Reports Where:
<select name='filterby'>
<option value='date'>Date</option>
<option value='offender'>Offender</option>
<option value='by'>Reported By</option>
</select>";
is:
<select name='field'><option value=""></option></select>
</form><br>

 

What I want to happen is when the first changes, the second pulls possible choices from the database corresponding to what was chosen.

 

I tried using a function and calling upon it the same way you would javascript with an "onchange" in the first select:

<select name='filterby' onchange='populate_second'>
<?php
function populate_second($filterby) {
$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('masterdatabase', $conn);

$query_field = "SELECT `".$filterby."` FROM `reports` WHERE `type` = '".$_GET['action']."'";
$field = mysql_query($query_field, $conn);
$row_field = mysql_fetch_assoc($field);
};
?>

is there something wrong with the function, the way i'm calling upon it, or is it something that just can't be done?

thanks if you can help!

Link to comment
https://forums.phpfreaks.com/topic/65738-solved-populating-a-second-select-field/
Share on other sites

is there a way to populate a second select field in a form based on input for a first select without reloading and using only php?

 

Nope. PHP runs on the server so you must make a request to the server to use it. What your trying to do is best achieved with Javascript, or if you really need database interaction, Ajax.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.