Jump to content

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.

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.