I
I
Ivan Nikiforov2021-06-15 18:27:13
PHP
Ivan Nikiforov, 2021-06-15 18:27:13

How to send the result of javascript code to your mail?

There is a test with checkboxes that: fully works and shows results. But how to display the result of js code in the mail? I only know that php is needed. Can you send material on this topic or explain in your own words how to do it?

<form method="$_GET" action="action_handler.php" id="form">
    <section class="table_1">
        <table class="iksweb" name="checkbox">
            <tbody>
                <tr>
                    <td rowspan="3"><b>История компании «Mc donald's»</b>
                        <h3 class="the">Кто основал компанию «Mc donald's»?</h3>   
                        <section class="conteiner">
                            <div class="checkbox">
                                <input type="checkbox" class="i-6" id="i6" value="0" name="formDoor[]">
                                <label for="i6" tabindex="12">Роналд Макдоналд</label>     
                            </div>
                            <div class="checkbox">
                                <input type="checkbox" class="i-6" id="i7" value="0" name="formDoor[]">
                                <label for="i7" tabindex="13">Рэй Крок</label>     
                            </div>
                            <div class="checkbox">
                                <input type="checkbox" class="i-6" id="checkbox_68" value="1" name="formDoor[]">
                                <label for="checkbox_68" tabindex="14">Братья Дик и Мак Макдоналд</label>     
                            </div>
                            <div class="checkbox">
                                <input type="checkbox" class="i-6" id="checkbox_170" value="0" name="formDoor[]">
                                <label for="checkbox_170" tabindex="14">Клинт Иствуд</label>     
                            </div>
                            <div class="out-block out-6"></div>
                        </section>
                   </td>     
                </tr>
            </tbody>
        </table>
    </section>
    <div class="dsw">
        <button class="b-6" tabindex="11" id="btn-1" type="submit" name="formSubmit">Отправить</button>
    </div>  
</form>

/* Form (poll) main script */

const form = document.querySelector('#form')
form.addEventListener('submit', onSubmit)

function onSubmit (event) {
event.preventDefault()

let listCheckbox = document.querySelectorAll( '.i-6')
listCheckbox = [...listCheckbox]


// Check if at least one checkbox is selected
if (!listCheckbox.some(checkbox => checkbox.checked)) {
alert('You didn't select any checkbox')
}

else{
form.addEventListener('submit', onSubmit)
alert('Are you confirming the action?');
}
// Find out how many correct answers there are
const rightAnswersCount = listCheckbox.filter(checkbox => Number(checkbox.value) === 1).length

// Find out how many wrong answers in total
const wrongAnswerCount = listCheckbox.length - rightAnswersCount

// Find out the number of correct answers
const rightAnswers = listCheckbox. filter(checkbox => Number(checkbox.value) === 1 && checkbox.checked).length

// Find out the number of incorrect answers
const wrongAnswer = listCheckbox.filter(checkbox => Number(checkbox.value) === 0 && checkbox.checked).length

// Notify the user
alert(`You have selected ${rightAnswers} options out of ${rightAnswersCount} correct`);
/*alert(`You answered incorrectly ${wrongAnswer} from ${wrongAnswerCount}`)*/

//Output an answer with percentages
alert(`Percentage of correct answers: ${(rightAnswers / rightAnswersCount) * 100}`);
/*alert(`Percentage of incorrect answers: ${(wrongAnswer / wrongAnswerCount) * 100}`)*/

}

/* Script of correct and incorrect answers */
let button = document.getElementById('btn-1');
button.addEventListener('click', function(e) {
document.querySelectorAll('.i-6').forEach(item => {
let chekcbox = item.closest('div');
if (item.checked && Number (item.value)) {
chekcbox.classList.add("right");
chekcbox.classList.remove("false");
} else if (item.checked) {
chekcbox.classList.add("false"
checkbox.classList.remove("right");
} else {
checkbox.classList.remove("right");
checkbox.classList.remove("false");
}
})
});

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Spartak (Web-StyleStudio), 2021-06-15
@defroing

Easiest option:
ajax + php + PHPMailer (or at least the built-in php mail function )

S
Stefan, 2021-06-15
@MEDIOFF

Scheme of actions:
1. Raise a simple back (what is more convenient for you php, nodejs, python, ruby, etc.);
2. Choose either a mailing service and use it, or find a library that can work with a mailbox and drop messages from it, and connect to it;
3. Create a simple API, if you only need to send mail, then with 1 endpoint;
4. Accept the request on the server, take the data, and send it to the mail, the documentation for your library or service that you have chosen will tell you exactly how to send it

A
Alex P., 2021-06-15
@Big_Alex

Fast, clumsy and head-on...
Send emails with js...
https://stackoverflow.com/questions/7381150/how-to...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question