A
A
Andrew Incognito2018-02-20 08:56:37
JavaScript
Andrew Incognito, 2018-02-20 08:56:37

JS or JQuery function to check $_POST global array variables?

Task:
If 2 forms of feedback. One consists of 2 fields, the other of 4. There is a js script that ajax-th sends data from the form. In the ajax script, you need to write a check to see if there is a certain variable in the global POST array in order to separate the sending of the first form data from the second.
Script code:

document.getElementById('feedback-form').addEventListener('submit', function(evt){
  var http = new XMLHttpRequest(), f = this;
  evt.preventDefault();
  http.open("POST", "http://zero-waste.loc/wp-content/themes/zerowaste/d_contacts.php", true);
  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

  
    if()
    {
        http.send("nameFF=" + f.nameFF.value + "&telFF=" + f.contactFF.value + "&messageFF=" + f.messageFF.value);
    }
    else
        {
           http.send("nameFF=" + f.nameFF.value + "&telFF=" + f.telFF.value ); 
        }
  http.onreadystatechange = function() {
    if (http.readyState == 4 && http.status == 200) {
      alert(http.responseText +', Ваше сообщение получено.\nНаши специалисты ответят Вам в течении 2-х дней.\nБлагодарим за интерес к нашей фирме!');    
      
    }
  }
  http.onerror = function() {
    alert('Извините, данные не были переданы');
  }
}, false);


Tell me if in js functions to check the existence of a variable in POST, or similar to PHP isset(), isempty()???

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2018-02-20
@Eridani

Such checks are done on the php side in the handler, and already there you check if the fields you need exist.
But the easiest way is to write a foreach loop on POST, and based on the received data, form a template.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question