简体   繁体   中英

Passing two value in javascript

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!

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.

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'];

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM