Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function in Jquery passing form element's ID as parameter

I'm trying to make a function in Jquery that will take the ID of form SELECT element's ID where a dynamically created option should be displayed. But, i have to repeat this work for other form instances as well. i have created the following code, but it did not work.

JQuery Script

function getOffice(ID){
    $.post('dynamicOffice.php',{operator:$(this).val()},function(output){
    $('ID').html(output);
    });
        $('ID').removeAttr('disabled');
}

Main HTML File

<select id="senderOperator" name="senderOperator" tabindex="1" onchange=getOffice(sender)>
    <option value=""><--SELECT an Operator --></option>                 
        <?php getOption($operator,Operator) ?>
</select>

<select id="sender" name="sender" tabindex="1" disabled="disabled">
    <option value=""><--SELECT the Operator First --></option>
</select>

dynamicOffice.php

<?php
include('generateOption.php');
    $country=$_POST['operator'];
    $officeSql="SELECT * FROM myoffice WHERE Operator='$country'";
    getOption($officeSql,Name);
?>

generateFormElement.php

<?php
include("include/dbConnect.php");       

function getOption($rsSql,$colName){

    $sResult=mysql_query($rsSql) or die("Could Not Fetch Records");

    while ($s_Office = mysql_fetch_array($sResult))
    {
        echo("<option value='".$s_Office["$colName"]."'>".$s_Office["$colName"]."</option>");
    }
}
?>
like image 680
Raju Rimal Avatar asked May 14 '11 23:05

Raju Rimal


1 Answers

Remove the quotes from around the parameter, and concatenate a # to the beginning of it.

$('#' + ID).html(output);

$('#' + ID).removeAttr('disabled');

Also, this will likely reference the window instead of whatever element you expect, so the following won't work:

{operator:$(this).val()}

If it should reference the select element, then add this as a second argument:

onchange=getOffice(sender,this)

...and reference it with a parameter:

function getOffice(ID, el){
    $.post('dynamicOffice.php',{operator:$(el).val()},function(output){
        $('#' + ID).html(output);
    });
    $('#' + ID).removeAttr('disabled');
}
like image 53
user113716 Avatar answered Sep 30 '22 18:09

user113716