0

I'm having a problem when trying to dynamically control the options available in a second dropdown when a certain option in a parent dropdown is selected. The value of the option selected in the initial dropdown is not passed into a PHP function which retrieves the applicable options from a database. I'm new to PHP and ajax so only know a few options to try but so far unable to solve. The value of the initial dropdown options are 1 to 63. I've tried making the PHP function parameter name the same as the ajax function parameter name and tried combinations of PHP variables but a value of '0' is passed into the PHP function instead of 1 to 63 so the 2nd dropdown is populated with the wrong options every time.

The parent dropdown uses an Ajax function which is called on change of option selected:

<select onchange="getOptionValues(this.value)">

Here is the ajax fucntion:

function getOptionValues(project)
{
$.ajax({
    url: 'subtypelist.php?project=' + project,
success: function(data) {
$("#subtype").html(data);
}
});
}

This calls getSubTypeOnProjectID() (in a PHP class file) and processing PHP file (subtypelist.php) with getSubTypeOnProjectID() parameter same as the ajax parameter.

function getSubTypeOnProjectID($sub) {
$sql= 'SELECT EST_Index as ID, EST_Text as TEXT FROM EventSubType where EST_EventType = 2 and EST_ProjectID = ?';
return $this->openSql($sql, array($sub));
}

$subTypeList = $thisEventDB->getSubTypeOnProjectID(project);
foreach ($subTypeList as $option)
echo '<option value="'. $option[ID]  . '">' . $option[TEXT] . '</option>';

I know that the correct value is being passed to the subtypelist.php via the URL subtypelist.php?project=10 but option value "0" is retrieved instead so it's more like subtypelist.php?project=0. How can I pass the project parameter value into the getSubTypeOnProjectID()?

2 Answers 2

2

you need to access the param using $_GET['project'], change this:

$subTypeList = $thisEventDB->getSubTypeOnProjectID($_GET['project']);

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

1 Comment

Ideal answer @Ali ! I wasn't aware of $_GET [``] until now so thank you!
0

Why don't you do this:

  • every option has a value in the select, from 0 to 63 right?
  • create jQuery event for selecting(click, focus) option, adding option value to select

Or even better, I think its possible to get the selected value of the select by just $('input[name="project"]').val()

Comments

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.