Reputation: 71
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 →',
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";
}
}
Upvotes: 1
Views: 83
Reputation: 97672
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";
}
}
Upvotes: 1