0

I have a PHP function that looks like this:

addPhone('859080', '111111111');

How can I call that function, from JavaScript, using AJAX, when I select a specific field on the form?

jwpfox
  • 5,124
  • 11
  • 45
  • 42
Tom Miron
  • 57
  • 2
  • 10
  • 3
    Possible duplicate of [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – u_mulder Dec 01 '16 at 16:45
  • See: [How to call a PHP file from HTML or Javascript](http://stackoverflow.com/questions/20637944/how-to-call-a-php-file-from-html-or-javascript/20639432#20639432) – Theraot Dec 04 '16 at 00:27

1 Answers1

0

You can use ajax to make this work like this:

You HTML goes like this:

<button id="addPhone" data-phone="9999999999">
    Add Phone 9999999999
</button>

You JS (jQuery) goes like this:

$('#addPhone').on('click', function(e) {

    phone = $(this).data('phone');
    addPhone(phone);

});

function addPhone(phone) {

    $.ajax({
    url: "url_to_php_function",
    type: 'json',
    data: {
        'phone': phone
    },
    success: function(data){
      // You can access response here in data variable
    },
    failure: function(data){
      // handle errors here
    }
  });

}

Hope this Helps!

Saumya Rastogi
  • 13,159
  • 5
  • 42
  • 45