0

I need to upload websql table to the server using ajax. when I use indexed array or simple variable code works fine. I cant figure out what problem is? plz help me. thanks in advance... here is javascript:

function upload()
{

var db = cropsap.webdb.db;

 db.transaction(function (tx) {

                tx.executeSql('SELECT * from survey', [],function(tx, results) {
                    var data = "";
                    var survey=[];
                    for(var i=0; i < results.rows.length; i++) {
                        var row = results.rows.item(i); 
                        var result = [];


                        for(var key in row) 
                        {         
                            result[key] = row[key];  
                        }     
                        survey.push(result);
                    }
                    //alert(survey[0]['srno']);//here i get output

               var url = "";
                url = document.getElementById("url").value;
                $.ajax({
                    type: "POST",
                    url: url,
                    async: true,

                    data: { "data": JSON.stringify(survey) },
                    error: function () { alert("error occured");},
                    success: function (data) {

                        alert("sucess");
                        alert(data);

                    }
                });
            });
        });
}

php code:

<?php
$survey= json_decode(stripslashes($_POST['data']));

        $len = sizeof($crop_insect_survey_details);
        print(" len: ".$len);

            for($i=0;$i<$len;$i++)
        {
            $srno = $crop_insect_survey_details[$i]['srno'];//here cant get output
            $name = $crop_insect_survey_details[$i]['name'];
            print("srno:".$srno);
            print("name:".$name);
        }

    ?>
SysAdmin
  • 5
  • 5

1 Answers1

0

Instead of using data: { "data": JSON.stringify(survey) }, You can pass your data inside data key itself.

data:JSON.stringify(survey)
dataType: "json",
contentType: "application/json; charset=utf-8",
Afshan Shujat
  • 541
  • 4
  • 9
  • it prints "error occured" message, what changes i have to do in php code? – SysAdmin Apr 20 '16 at 06:38
  • where it's printing "error occured" in js file or php? You can user php php://input to receive data at php file. – Afshan Shujat Apr 20 '16 at 08:31
  • file_get_contents("php://input"), use this syntax it will help you. – Afshan Shujat Apr 20 '16 at 08:35
  • after including file_get_contents("php://input") still it gives same message.it calls error function declared in ajax – SysAdmin Apr 20 '16 at 10:27
  • You can see in the console what is actual error there and can print the error in comsole see this for debugging http://stackoverflow.com/questions/36758922/how-to-post-data-to-php-file-using-ajax/36759072#36759072 – Afshan Shujat Apr 21 '16 at 05:20
  • I used error: function (xhr, status, error) { alert(error); }..it gives alert like-- syntaxError:Unexpected token l – SysAdmin Apr 21 '16 at 06:35
  • It happens sometimes for malformed json, try the same request after removing contentType from ajax request. – Afshan Shujat Apr 21 '16 at 06:41