0

I have a little function, im trying pass 2 parameters for her, but dont works... Any idea/sugestion?

Don't have problems with ajax, i have tested this code without parameters, putting direct on the function, but calling her, not works, sorry about the terrible english!!

function myfunction(var_data, var_field)
{
  $(function()
  {
    $.ajax
    ({
        url : "myscriptajax.php",
        type: "POST",
        data: var_data + $(this).val(),
        dataType:"json",
        success: function(data)
        {
          if(data.status)
          {
             $(var_field).val(data.somevar);
          }
        }
    })
 })
}
$("#medicocrm").change
  (function()
  {
       myfunction("crm=","#mediconome");
  })

// edited after here for best explanation about. That works:

$(function()
{
    $("#medicocrm").change
        (function()
        {
        $.ajax
        ({
            url : "abertura.ajax.php",
            type: "POST",
            data: "crm=" + $(this).val(),
            dataType:"json",
            success: function(data)
            {
                if(data.status)
                {
                    $("#mediconome").val(data.nome);
                }
            }       
        })
        return false;
    })

$("#participantematricula").change
    (function()
    {
        $.ajax
        ({
            url : "abertura.ajax.php",
            type: "POST",
            data: "matricula=" + $(this).val(),
            dataType:"json",
            success: function(data)
            {
                if(data.status)
                {
                    $("#participantenome").val(data.nome);
                }
            }
        })
        return false;
    })

\i tried this with first answer...

and that not works:

function verifica(dados,campoid,camponome){
    $.ajax({
        url : "abertura.ajax.php",
        type: "POST",
        data: dados + campoid,
        dataType:"json",
        success: function(data){
            if(data.status){
                $(camponome).val(data.nome);
            }
        }
    });
    return false;
};

$("#medicocrm").change(function(){
verifica("crm=",this.value,"#mediconome");
});

$("#participante_id").change(function(){
verifica("id=",this.value,"#participante_nome");
});
5
  • 2
    What do you expect this to refer to? There are a couple ways to correctly pass this. One of which is to use .call() to call the function where you can pass a reference to this or by creating a third argument that references the correct this. Commented Oct 14, 2013 at 20:03
  • 1
    Remove the inner function $(function() and fix $(this).val() that is unrelated now Commented Oct 14, 2013 at 20:03
  • 1
    where is semi colons? Commented Oct 14, 2013 at 20:04
  • Jasper, i have used this to refer the element "#medicocrm". I want reduce the script with that function... passing the function for element .change events. Using one function per element works, but same implementing your way still not working... Commented Oct 14, 2013 at 20:42
  • Dvir, jquery accepts code without semicolons... Commented Oct 14, 2013 at 23:14

3 Answers 3

4

Just do a revamp.

function myfunction(var_data, var_field, elementValue){
    $.ajax({
        url : "myscriptajax.php",
        type: "POST",
        data: var_data + elementValue,
        dataType:"json",
        success: function(data){
          if(data.status){
             $(var_field).val(data.somevar);
          }
        }
    });
};

$("#medicocrm").change(function() {
       myfunction("crm=","#mediconome", this.value);
});

Here we removed the DOMContentLoaded listener and passed the value of the element through to the function..

You can use $(this).val(); in place of this.value whatever floats your boat.

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

2 Comments

@MarceloAymone maybe explaining what not's working would help.
Tomorrow i will put some alert's in code, and answer you, Thanks for everyone's help :)
0

You have a whole lot of wrappers going on, and your use of $(this) is likely breaking it. Something like this should work:

function myfunction(var_data,$var_field,whatever_this_is_val){
    $.ajax({
        url : "myscriptajax.php",
        type: "POST",
        data: var_data + whatever_this_is_val,
        dataType:"json"
    }).done(function(data){
        if(data.status){
             $var_field.value = data.somevar;
        }
    });
}

$("#medicocrm").on('change',function(){
    myfunction("crm=",this,document.getElementById(whatever_this_is).value);
});

Changes:

  • Unnecessary wrappers removed
  • Passing of $(this) ... you need to specifiy it
  • Cleanup syntax to modern use of .done().
  • Using vanilla JS where easily applied

You should also consider explicitly declaring the page that it calls to be JSON, rather than saying dataType:'json' in your call. Its bulletproof this way, and less work performed on all sides.

EDIT

If you are really just passing the value of the item changed, easiest way to do it:

function myfunction(var_data,$var_field){
    $.ajax({
        url : "myscriptajax.php",
        type: "POST",
        data: var_data + $var_field.value,
        dataType:"json"
    }).done(function(data){
        if(data.status){
             $var_field.value = data.somevar;
        }
    });
}

$("#medicocrm").on('change',function(){
    myfunction("crm=",this);
});

4 Comments

I didn't vote, but I'd guess it's because of all that whatever_this_is nonsense in place of the context of the handler.
well were making the assumption that this refers to the element changing ... if that isnt the case, something ELSE needs to be specified. sorry if that was unclear, because if he really is just wanting the same value in the function then there is a much easier way to do it.
yup, edited my answer to show the simplified implementation, where the item being changed is used for var_field.
this does use jQuery (the entire $.ajax statement is jQuery syntax), it just also uses the most basic instances of regular JS (this.value exclusively). if you absolutely refuse to use anything but jQuery syntax simply to avoid learning anything, replace this with $(this) and $var_field.value with $var_field.val() and it'll work the exact same. just slower.
0

That way worked!!!!!! With many wrappers, but... Worked!

callajax = (function(origem,dados,campo)
{$.ajax({
    url : "abertura.ajax.php",
    type: "POST",
    data: origem + "=" + dados,
    dataType:"json",
    success: function(data){
        if(data.status){
            $(campo).val(data.nome);
        }
        else{
            $(campo).val("");
            alert('Não encontrado');
        }
    }
})
});

$(function(){$("#medicocrm").change
    (function(){
        callajax('crm',this.value,"#mediconome");
    });
});

$(function(){$("#participantematricula").change
(function(){
        callajax('matricula',this.value,"#participantenome");
    });
});

$(function(){$("#prestadorcodsoc").change
    (function(){
        callajax('codsoc',this.value,"#prestadornome")
    });
});

1 Comment

I guess jQuery framework needs that wrappers to run... with my function and parameters, i have more clean code, easy to adapt to others form fields.

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.