var submit = function (url, data) {
    // create a form
    var form = document.createElement('form');
    form.method = 'POST';
    form.action = url;
    form.style.display = 'none';

    // add the form data to the form
    for (var key in data) {
        var input = document.createElement('input');
        input.type = 'hidden';
        input.name = key;
        input.value = data[key];
        form.appendChild(input);
    }

    // submit the form
    document.body.appendChild(form);
    form.submit();
}

var toFormData = function (obj) {
    var formData = new FormData();
    for (var key in obj) {
        formData.append(key, obj[key]);
    }
    return formData;
}
var fetchPHP = async function (url, data = {}) {
    return response = await fetch(
        url,
        { method: 'POST', body: toFormData(data) }
    )
        .then(response => {
            try {
                return response.json()
            }
            catch (e) {
                var message = 'Error en la respuesta del servidor';
                Promise.reject(message)
            }
        })
        .then(response => response.error ? Promise.reject(response.error) : Promise.resolve(response))
}   