How to call a function after textbox lose focus
Asked Answered
H

4

13

I have no experience with Javascript/JQuery/AJAX so I'm trying to understand if it's possible to call a function that execute a query on my DB after a textbox lose focus.

I'm displaying a table in my page (using PHP) with text boxes that contains the same values of a table on my DB, and when someone change the value on a text box I want to change the DB table with an UPDATE query to make them equals; is that a way with AJAX or JQuery to do this?

Herbherbaceous answered 28/7, 2012 at 11:6 Comment(1)
Just clarify some things: JavaScript is a language, jQuery is a library written in JavaScript and Ajax is method of communication. Maybe you find the MDN JavaScript Guide helpful.Detrude
R
16
<input type="text" id="check">​

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script type="text/javascript">
$("#check").blur(function() {
 alert('working');
});​
</script>
Reading answered 28/7, 2012 at 12:19 Comment(0)
I
3

Bind a handler to the change event:

<textarea onchange='ajax_call()'></textarea>

where ajax_call is a function for updating the DB.

Intranuclear answered 28/7, 2012 at 11:12 Comment(4)
Yes, its year 2012 and I just saw an inline js.Flocculate
its not that i dont know about the advanced method available but with a novice, i prefer to used the simplest one availble! Maybe, it's you who should realize its 2012Intranuclear
That was a lite joke bro. Keep it cool. But may be you dont realize that your method works on onchange not on losing focus as OP wanted :)Flocculate
it's 2017 and I am still using inline JS function calls.. and still see nothing wrong with it. Or maybe it's 2012 for whoever is reading this and this is me from the future.Sapajou
I
1

Yes, you can. First you need to wait for JavaScript event like onchange/onblur or other. Than you need to make an Ajax call to the server. It will change the data in db and can result some data, that you can put on the page. You can use jQuery to manipulate your page.

Another way is to wait until a person presses submit button and than update data in the db.

Inositol answered 28/7, 2012 at 11:17 Comment(0)
K
0

<form id='form'>
  <label for="name">name:</label>
  <input type="text" name="name" onblur="makePink()">
</form>

<script>
    function makePink() {
       document.body.style.backgroundColor = 'pink';
    }
</script>
Korwin answered 30/6, 2022 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.