1

I'm new to this so I have a question regarding dependent drop down menu.

I have two drop down menu first, Leave Type and the second Available Balance.

Below shows the js and form.

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {

$("#parent_cat").change(function() {
$(this).after('<div id="loader"><img src="img/loading.gif" alt="loading subcategory" /></div>');
$.get('loadsubcat.php?parent_cat=' + $(this).val(), function(data) {
    $("#sub_cat").html(data);
    $('#loader').slideUp(200, function() {
        $(this).remove();
    });
}); 
});

});

$sql = mysql_query("SELECT * FROM leaves WHERE emp_id=$emp_id'");

<form method="get">
<label for="category">Leave Type</label>
<select name="parent_cat" id="parent_cat">
 <option> Select one </option>   
<?php
if ($row['leave_al'] == 1)
{
    echo "<option value='Annual Leave'> Annual Leave </option>";
}

if ($row['leave_matl'] == 1)
{
    echo "<option value='Maternity Leave'> Maternity Leave </option>";
}

?>
</select>

<label>Available Balance</label>
<select name="sub_cat" id="sub_cat"></select>
</form>

loadsubcat.php

<?php

$parent_cat = $_GET['parent_cat'];
$emp_id = $_GET['emp_id'];

$query = mysql_query("SELECT * FROM leaves WHERE emp_id = 'OR9090'");
$row = mysql_fetch_array($query);

if ($parent_cat == 'Annual Leave')
{
   echo "<option value='$row[id]'>$row[total_annual]</option>";
}

if ($parent_cat == 'Maternity Leave')
{
  echo "<option value='$row[id]'>$row[maternity_days]</option>";
}
?>

Example above, parent_cat is been passed to loadsubcat in order to load the available balance. I would also like to pass $emp_id to loadsubcat so that the sql can read based on $emp_id. Right now, I can only assign the emp_id manually. Please help me thanks!

3
  • Where is emp_id stored in the HTML? Commented May 15, 2015 at 0:43
  • Sir, emp_id is not stored in the html. The value is being passed from another process. I just need the emp_id for the sql query Commented May 15, 2015 at 0:48
  • Where can you access emp_id in your HTML file above? Commented May 15, 2015 at 0:50

3 Answers 3

2

and you can try this:

<form method="get">
<input type="hidden" name="emp" value="<?php echo $emp_id ?>"/>
<label for="category">Leave Type</label>
<select name="parent_cat" id="parent_cat">

Put the emp_id on hidden input and then get the value on jquery change

$("#parent_cat").change(function() {
var emp = $(this).siblings('input[name=emp]').val(); (...)

and then complete with Drake said.

$.get('loadsubcat.php?parent_cat=' + $(this).val() + '&emp_id=' + emp, function(data) {

Oh, and be careful with the GET vars from the loadsubcat.php use mysql_real_escape_string or PDO to protect SQL injections.

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

3 Comments

Excuse me sir, but may I know why when the emp_id has been passed and when I echo the emp_id, it says undefine?
try to put the input tag before select tag. And on the js function check with alert(emp) to see if the emp_id is not undefined.
Hi sir, I've tried like what you asked me to. When I tried alert(emp) is says undefined. So I assume that the id has not been passed? I just don't know where to amend on the code as I'm not familiar about js
1

I would also like to pass $emp_id to loadsubcat

To add another parameter to the loadsubcat.php query string, you can change

$.get('loadsubcat.php?parent_cat=' + $(this).val(), function(data) {

to

$.get('loadsubcat.php?parent_cat=' + $(this).val() + '&emp_id=' + $("...").val(), function(data) {

where $("...").val() is the location of the employee ID value in some HTML tag, then in your PHP file you can access them with your code below:

$parent_cat = $_GET['parent_cat'];
$emp_id = $_GET['emp_id'];

Comments

1

my code is now working! I've changed the

var emp = $(this).siblings('input[name=emp]').val(); 

to

var $form = jQuery(this).closest('form');
var emp = $form.find("input[name='emp']").val();

The rest of code given above is working perfectly fine!

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.