Search code examples
javascriptphparraysjsonsweetalert2

Sweetalert2 Swal.mixin - is it possible to extract input data to json / php?


Using this code:
Swal.mixin({
  input: 'text',
  confirmButtonText: 'Next →',
  showCancelButton: true,
  progressSteps: ['1', '2', '3']
}).queue([
  {
    title: 'Question 1',
    text: 'Chaining swal2 modals is easy'
  },
  'Question 2',
  'Question 3'
]).then((result) => {
  if (result.value) {
    const answers = JSON.stringify(result.value)
    Swal.fire({
      title: 'All done!',
      html: `
        Your answers:
        <pre><code>${answers}</code></pre>
      `,
      confirmButtonText: 'Lovely!'
    })
  }
})

In this case, my answers:

["1","1","2"]

how could it receive the values ​​passed in a php file? I do not know well about both subjects, but I am experiencing this need. Sorry, and sorry for my english.


Solution

  • Update:

    Using this code: Sweet Alerts - Queue form Make change swal for Swal.fire

      var strAns1;
        var strAns2;
    
        swal.mixin({
          input: 'text',
          confirmButtonText: 'Next &rarr;',
          showCancelButton: true,
          progressSteps: ['1', '2', '3']
        }).queue([
          {
            title: 'Question 1',
            text: 'Chaining swal2 modals is easy',
            preConfirm: function(value)
                    {
                        strAns1= value;
                    }
          },
    
          {
            title: 'Question 2',
            text: 'Chaining swal2 modals is easy',
            preConfirm: function(value)
                    {
                        strAns2= value;
                    }
          }
        ]).then((result) => {
          if (result.value) {
            Swal.fire({
              title: 'All done!',
              html:
                'Your answers: <pre>' +
                  JSON.stringify(result) +
                '<pre>Answer1- ' + strAns1+
                '<pre>Answer2- ' + strAns2+
                '</pre>',
              confirmButtonText: 'Lovely!'
            })
          }
        })
    

    Change

    swal({
          title: 'All done!',
          html:
            'Your answers: <pre>' +
              JSON.stringify(result) +
            '<pre>Answer1- ' + strAns1+
            '<pre>Answer2- ' + strAns2+
            '</pre>',
          confirmButtonText: 'Lovely!'
        })
      }
    })
    
    To
    
    Swal.fire({
          title: 'All done!',
          html:
            'Your answers: <pre>' +
              JSON.stringify(result) +
            '<pre>Answer1- ' + strAns1+
            '<pre>Answer2- ' + strAns2+
            '</pre>',
          confirmButtonText: 'Lovely!'
        })
      }
    })
    

    Im get in result this:

    {"value":["adf","fffff"]}
    Answer1- adf
    Answer2- fffff
    

    Now can I pass this on to a php? Like for example? Thank you thank you very much.