Saving a form using sweetalert

Viewed 13

I have a survey form and i am using ajax as its query on saving to the database. And i have a sweetalert button. The problem is that if I remove the form tag on the php file the sweet alert works but it does not save the informations submitted to the database. BUt when i put back the form tag the informations are saved but the sweetalert does not work. How should i get it done? below is my script for the submition of the form.

<script>
  $(document).ready(function() {

    $("#submit").click(function() {

      var agent_id = $("#agent_id").val();
      var tenure = $("#tenure").val();
      var site_names = $("#site_names").val();
      var department = $("#department").val();
      var pi_role = $("#pi_role").val();
      var email = $("#email").val();
      var motivation = $("#motivation").val();
      var scale_fun = $("[type='radio']:checked").val();
      var kind = $("#kind").val();
      var job_performance = $("[type='radio']:checked").val();
      var progress = $("[type='radio']:checked").val();
      var aspects_found = $("#aspects_found").val();
      var aspects_hold = $("#aspects_hold").val();
      var adjectives = $("#adjectives").val();
      var organizational_values = $("[type='radio']:checked").val();
      var why_onefirst = $("#why_onefirst").val();
      var communicate = $("[type='radio']:checked").val();
      var feedback_first = $("[type='radio']:checked").val();
      var role_model = $("[type='radio']:checked").val();
      var why_twofirst = $("#why_twofirst").val();
      var scale_treat = $("[type='radio']:checked").val();
      var why_onesecond = $("#why_onesecond").val();
      var recognized = $("[type='radio']:checked").val();
      var feedback_second = $("[type='radio']:checked").val();
      var recognition = $("[type='radio']:checked").val();
      var explain_score = $("#explain_score").val();
      var opinion = $("[type='radio']:checked").val();
      var team_meeting = $("[type='radio']:checked").val();
      var why_twosecond = $("#why_twosecond").val();
      var oppurtunity = $("[type='radio']:checked").val();
      var change_job = $("#change_job").val();
      var count_coworkers = $("[type='radio']:checked").val();
      var training_priorites = $("#training_priorites").val();
      var senior_leadership = $("[type='radio']:checked").val();
      var work_load = $("[type='radio']:checked").val();
      var department_belong = $("#department_belong").val();
      var department_incharge = $("#department_incharge").val();

      if (agent_id == '' || tenure == '' || site_names == '' || department == '' || pi_role == '') {
        Swal.fire('Opss.. something went wrong', 'Fill in required fields', 'error')
        return false;
      }
      Swal.fire({
        title: 'Are you sure?',
        showCancelButton: true,
        confirmButtonText: `Save`,

      }).then((result) => {
        /* Read more about isConfirmed, isDenied below */
        if (result.isConfirmed) {
          $.ajax({
            type: "POST",
            url: "includes/save2.php",
            data: {
              agent_id: agent_id,
              tenure: tenure,
              site_names: site_names,
              department: department,
              pi_role: pi_role,
              email: email,
              motivation: motivation,
              scale_fun: scale_fun,
              kind: kind,
              job_performance: job_performance,
              progress: progress,
              aspects_forward: aspects_forwad,
              aspects_hold: aspects_hold,
              adjectives: adjectives,
              organizational_values: organizational_values,
              why_onefirst: why_one_first,
              communicate: communicate,
              feedback_first: feedback_first,
              role_model: role_model,
              why_twofirst: why_twofirst,
              scale_treat: scale_treat,
              why_onesecond: why_onesecond,
              recognized: recognized,
              feedback_second: feedback_second,
              recognition: recognition,
              explain_score: explain_score,
              opinion: opinion,
              team_meeting: team_meeting,
              why_twosecond: why_twosecond,
              oppurtunity: oppurtunity,
              change_job: change_job,
              count_coworkers: count_coworkers,
              training_priorities: training_priorities,
              senior_leadership: senior_leadership,
              work_load: work_load,
              department_belong: department_belong,
              department_incharge: department_incharge

            },
            cache: false,
            success: function(e) {
              if (e == 1) {
                Swal.fire('Saved!', '', 'success')
              } else {
                Swal.fire('Opss.. something went wrong ' + e, '', 'error')
              }
            },
            error: function(xhr, status, error) {
              console.error(xhr);
            }
          });


        } else if (result.isDenied) {

          Swal.fire('Changes are not saved', '', 'info')

        }
      });

    });

  });
</script>
0 Answers
Related