0

I am trying to pass the value of a html textbox form field to another .php file using jquery and ajax when the user defocuses that textbox. The textbox has the id "aircraftReg". I am using the code as follows...

$(document).ready(function(){
    $("#aircraftReg").blur(function() {
        var aircraftReg = $(this).value;
        $.get("searchDatabase.php?q=aircraftReg", function(data){                   
            // My function                 
        });  
    });
});

I think the problem lies in creating the var aircraftReg. I am attempting to assign its value to the text within the text box with id "aircraftReg".

Can anyone see what is going wrong?

3 Answers 3

1

Try to change it like this:

 var aircraftReg = $(this).val();
 $.get("searchDatabase.php?q="+aircraftReg , function(data){                   
            // My function                 
        }); 

To get value of text field (or other input) there is .val() method in jQuery.

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

Comments

1

You need to change your Url because you are passing javascript var to url so it must me added with +.

var aircraftReg = $(this).val();
$.get("searchDatabase.php?q="+aircraftReg , function(data){                   
     //function logic goes here             
});

4 Comments

seems like your answer is exactly like mine? ;)
but some explanation must be there.
ok , good explanation is even more important than right code!
i not said explanation is even more important than right code but if we need to explain little for OP understanding.
1

You have passed aircraftReg as regular string not as a javascript variable.

$(document).ready(function(){
    $("#aircraftReg").blur(function() {
        var aircraftReg = $(this).value; //or $(this).val();
        $.get("searchDatabase.php?q="+ encodeURIComponent(aircraftReg), function(data){                   
            // My function                 
        });  
    });
});

Also, you have three encoding options:

escape() will not encode: @*/+

encodeURI() will not encode: ~!@#$&*()=:/,;?+'

encodeURIComponent() will not encode: ~!*()'

Note: escape() function is non-ASCII, encodeURI() and encodeURIComponent() are UTF-8 compatible.

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.