2

I've tried a lots of solution from so, but no one worked for me. So I have a javascript array and i should pass it to php for upload it into mysql.

This code is in a php page, where is an editable grid. If press enter in the grid it creates a new row, with the value of the editeble cell and this value goes into the var val=[]and this should go into mysql via `php.

I've tried two kind of pass from so as you can see below

Code I have:

<script type="text/javascript" >
    var dbs=0;
    var val=[];
    function myFunction(e) {
            if(e.keyCode == 13){
                var newId = (new Date()).valueOf();
                var value=gridbox.cells(1,0).getValue();
                gridbox.addRow(newId,value);                
                val.push(value);
                gridbox.cells(1,0).setValue('');
                gridbox.editCell(1,0);
                dbs=dbs+1;

            }
    }
    function upload(){
        var jsonString = JSON.stringify(val);
             /*$.ajax({
                type: "POST",
                url: "upload.php",
                data: {'data' : jsonString}, 
                cache: false,

                success: function(){
                    alert("OK");

                }
            });*/
        var jsonData = $.ajax({
                  url: "upload.php",
                  data: { 'data' : val},
                  dataType:"json", 
                      async: false
                  }).responseText;
        }
</script>

UPLOAD.PHP

And it creates new empty row in the db, so it runs, but the $data is empty

//$data = json_decode(stripslashes($_GET['data']));
$data=$_GET['data'];

  // here i would like use foreach:
/*
  foreach($data as $d){
     echo $d;
  }*/


$db_irattar = new indotekDbConnection("irattar");


for($i=0;$i<4;++$i){
$sql="INSERT INTO akt_hely(Tipus,Megnevezes) 
VALUES('2','$data[$i]')

";
}

$db_irattar->Execute($sql);
$db_irattar->Close();
unset($db_irattar);
?>

Network monitor

9
  • Did you look at the value of val in your browser? (using console.log(val)) Or looking at what's sent by monitoring network? What browser do you use? Commented Aug 22, 2016 at 8:53
  • Yep, it has datas in it. Commented Aug 22, 2016 at 8:53
  • How big is it? There's a limit on the size of the URL parametes, so it would probably be better to use POST instead of GET. Commented Aug 22, 2016 at 8:55
  • Use network monitor which is built-in to Firefox. Look if the data is indeed sent as query parameters. Commented Aug 22, 2016 at 8:55
  • It is not work neither with post. Commented Aug 22, 2016 at 8:56

1 Answer 1

5

It seems to me that you are assigning var jsonString = JSON.stringify(val); and sending val in ajax.

try

var jsonData = $.ajax({
              url: "upload.php",
              data: { 'data' : jsonString},
              dataType:"json", 
                  async: false
              }).responseText;

Instead of

var jsonData = $.ajax({
              url: "upload.php",
              data: { 'data' : val},
              dataType:"json", 
                  async: false
              }).responseText;

And decode the json string in php like $data=json_decode($_GET['data']);

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

3 Comments

It seems to be better, now in the network monitor I have upload.php?data=["a',"\nb"], but stil an empty rows get into mysql. What should i have in the upload.php?
I think the val[] can be longert than the maximum size of the url. But it is not work with $_POST.... Can you help me,
If he sends jsonString, he needs to use json_decode($_GET['data']) in PHP.

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.