There is a small php-код that pulls data from the database and using an instance of the class sends an SMS message to a phone number with some text.

There is a form with which I add the type of distribution and the message sent.

It is necessary that by pressing the add data button in the form - the php-код is executed. How can I do this? I understand it with js , but I don’t know how exactly. Please tell me which way to look.

  • read about ajax jquery.page2page.ru/index.php5/… ... - Bogdan Gudyma
  • In this case, you do not need to at least show the PHP code in the question. And how best to do it through JS, you probably need to first look at which JS libraries you use in the interface. jquery, motools or something else. - Dmitriy Gvozd
  • 2
    Weird question. You can clarify what exactly is happening in the form, that you should use js to send it. For now, I see that the usual action attribute will do. - DanielOlivo
  • In the form I simply add the data that is sent to the database, and then displayed on the page in the form of a table. - luckydutch
  • one
    Well, right there where your request is processed and send an email. - Naumov

1 answer 1

Ajax requests to help you. jquery example ajax request

 <form> <p><input type = 'text' name='phone' /></p> <p> <select name="delivery_type"> <option value="0">Выберите тип рассылки</option> <option value="sms">SMS</option> <option value="email">E-Mail</option> </select> </p> <p><button type = 'submit'>Submit</button></p> </form> $('form').on('submit', function(e) { e.preventDefault(); var delivery_type = $(this).find('select[name=delivery_type]').val(); if( !(delivery_type.length > 1) ) { alert('Сперва выберите тип рассылки'); return false; } var data = $(this).serialize(); console.log(data); $.ajax({ url: "send.php", method: 'POST',//или GET, dataType:'json', data:data, success: function(result) { console.log(result);// } }); }); 

result it will be an object that will return the php page to which you link

For example, the send.php code returns

 header('Content-type: application/json'); echo jsone_encode(['success'=>true]); 

In this case

 result = {success:true} 

Good luck :)

  • I still do not understand how to tie it to my button. - luckydutch
  • I changed the answer, look - Vanya Avchyan