Aysu Vural
Aysu Vural

Reputation: 73

I cant get response from json encode

I am trying to get values from form with serialize function and I correctly post and save to database but after this step my codes don't work . Someone help please ?

 $(document).ready(function() {
       $("#kform").submit(function() {
         var data = $(this).serialize();

         $.ajax({
           type: "POST",
           url: "yenikayit2.php",
           data: data,
           dataType: "json",
           success: function(data) {
             if (data.tip === "dosya") {
               alert("tralalala d");
             }
             if (data.tip === "tercume") {
               alert("tralalala t");
             }
             if (data.tip === "hata") {
               alert("tralalala hata");
             }
           }



         });


       });

PHP Code

<?php

if($musteri_ekle) { //mysql control function
$musteri_id=mysqli_insert_id($baglanti);
$_SESSION[ 'musteri_id']=$musteri_id;
if ($secilen=="dosya" ) 
{ echo json_encode(array( "tip"=>"dosya")); }
else if ($secilen == "tercume") 
{ echo json_encode(array("tip"=>"tercume")); } }
else { echo json_encode(array("tip"=>"hata")); } ?>

Upvotes: 2

Views: 70

Answers (2)

Aysu Vural
Aysu Vural

Reputation: 73

Well, i realized that i forgot put this fields on form action="" and method=""

after this i changed my jquery code :

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
$("#kayit").click(function(event) { var data=$("#kform").serialize(); }

And this works correctly

Upvotes: 0

ash__939
ash__939

Reputation: 1599

Modify yenikayit2.php to avoid PHP fatal error and notices. You can either use error_reporting(null); or edit the code as below

if (isset($musteri_ekle)) { // to avoid undefined variable error
    $musteri_id = mysqli_insert_id($baglanti);
    $_SESSION['musteri_id'] = $musteri_id;
    if ($secilen == "dosya") {
        echo json_encode(array("tip" => "dosya"));
    } else if ($secilen == "tercume") {
        echo json_encode(array("tip" => "tercume"));
    }
} else {
    echo json_encode(array("tip" => "hata"));
}

Upvotes: 1

Related Questions