1

I use sweetallert js in my file and this code get some data about users but this code is not sending data to the controller.

when I click button sweetallert function is run and ask some questions to user

Ajax code in view file

<script>
 function kitap_ekle(){
swal.mixin({
  input: 'text',
  confirmButtonText: 'Sonraki &rarr;',
  showCancelButton: false,
  progressSteps: ['1', '2', '3', '4'],
}).queue([
  {
    title: 'Kitap İsmi',
    text: 'Lütfen Kitap İsmi Giriniz',
    inputPlaceholder: 'Enter here',
   },
  {
    title: 'sayfa Sayısı',
    text: 'Lütfen Sayfa sayısı '
  },
]).then((result) => {
  if (result.value) {
   swal.fire({
 
      title: 'Blok ekleme işlemi tamalandı',
      html:
        'Your answers: <pre><code>' +
          JSON.stringify(result.value) +
        '</code></pre>',
      confirmButtonText: 'Teşekkürler!'
 
    }),
         $.ajax({
          url:'Slemler/blok_ekle',
          type:'post',
          dataType:'json',
          contentType:'application/json',
          data:JSON.stringify(result.value),
        
      }
              )
  }
})
}
 </script>

this is controller file

public function blok_ekle(){
    if(json_decode($_POST["myData"])!=""){

      $data=json_decode($_POST["myData"]);
      $this->db->insert('bloklar',$data);
    } else {
        echo "no data";
    }
}
0

1 Answer 1

1

The $_POST super global is only populated for application/x-www-form-urlencoded and multipart/form-data. So for application/json it will not be populated.

To read the json you can use file_get_contents

public function blok_ekle(){
    if(json_decode(file_get_contents('php://input'), true)!=""){

      $data=json_decode($_POST["myData"]);
      $this->db->insert('bloklar',$data);
    } else {
        echo "no data";

    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.