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);
        }

    ?>
16
  • 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']; $name = $crop_insect_survey_details[$i]['name']; print("srno:".$srno); print("name:".$name); } ?> Commented Apr 20, 2016 at 5:58
  • You have the option to edit your question... Commented Apr 20, 2016 at 5:59
  • Could you echo/var_dump $_POST['data'] ? Commented Apr 20, 2016 at 6:06
  • I changed var result = []; to var result = {}; now i can echo 'data' but in php file $len value giving wrong Commented Apr 20, 2016 at 10:35
  • Object doe not have .length property! You must have [] Commented Apr 20, 2016 at 10:40

1 Answer 1

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",
Sign up to request clarification or add additional context in comments.

7 Comments

it prints "error occured" message, what changes i have to do in php code?
where it's printing "error occured" in js file or php? You can user php php://input to receive data at php file.
file_get_contents("php://input"), use this syntax it will help you.
after including file_get_contents("php://input") still it gives same message.it calls error function declared in ajax
You can see in the console what is actual error there and can print the error in comsole see this for debugging stackoverflow.com/questions/36758922/…
|

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.