How to submit selected responses along with the form?

Viewed 26

I have a questionnaire that the user goes through step by step and finally submits the form. I want the form where the user submits their name and phone number to be sent along with the selected responses. How can I add the user's selected responses to the form and submit them?

//An example of a question where the user selects a gender
function appGender() {
  scrollDown();
  $(".chat-content-list").append(
    '<div><span class="chooseGenderM">Male</span></div><div class="chat-content-buttons-gender-block"><span class="chooseGenderW">Woman</span></div>'
  );
}

function genderNext() {
  $(".chooseGenderM").click(() => {
    document.querySelector(".chat-content-buttons-gender").style.display =
      "none";
    myMassange("Male");
  });
  $(".chooseGenderW").click(() => {
    document.querySelector(".chat-content-buttons-gender").style.display =
      "none";
    myMassange("Woman");
  });
}

//submit form
const form = document.getElementById("order_form");
form.addEventListener("submit", formSend);

async function formSend(e) {
  e.preventDefault();

  let error = formValidate(form);
  let formData = new FormData(form);

  if (error === 0) {
    let response = await fetch("sendmail.php", {
      method: "POST",
      body: formData,
    });
    if (response.ok) {
      let result = await response.json();
      alert(result.message);
      formPreview.innerHTML = "";
      form.reset();
    }
  }
}

//sendmail.php

//Body
if (trim(!empty($_POST['name']))) {
    $body .= '<p><strong>Name:</strong>' . $_POST['name'] . '</p>';
}
if (trim(!empty($_POST['phone']))) {
    $body .= '<p><strong>Phone:</strong>' . $_POST['phone'] . '</p>';
}

0 Answers
Related