-2

I have an array of strings in JS, and I want to send it to php (to be able to add it to db). This is my current code -> showing success message, but not showing the $_POST['array'] variable.

Keep in mind that that array I want to send has string values only and is called times

JS

 function fetchDates(){
times = [];
var table = document.getElementById("timeScheduleBody");
var tableRows = table.getElementsByTagName("tr");
for(var j=0;j<tableRows.length;j++){
    var tableCells = tableRows[j].getElementsByTagName("td");
    for(var i = 0;i<tableCells.length;i++){
        if(tableCells[i].getAttribute('class').includes("success")){
            var arr = tableCells[i].getAttribute('value').split("-");
            var date, hour, mins;
            date = arr[0];
            hour = arr[1];
            mins = arr[2];
            times.push(date);
            times.push(hour);
            times.push(mins);
        }

    }
}
window.alert(times);

//send array to PHP from jQuery
jQuery.ajax({  
     type: "post",  
     url: window.location.href,  
     data: {array:times},
     success: function(){  
         alert("done"); 
     }  
}); 

}

PHP

<?php
if(isset($_POST['array'])){
$array = $_POST['array'];
echo $array[0];//just testing -> output: nothing
  foreach($array as $d){
     print '<script>window.alert($d);</script>';//also just to test -> output: nothing
  }
}
?>
M.Ghali
  • 87
  • 1
  • 9
  • 2
    Can you add where and how `times` is defined? – insertusernamehere Oct 19 '17 at 05:27
  • [passing javascript array to php through jquery $.ajax](https://stackoverflow.com/questions/2013728/passing-javascript-array-to-php-through-jquery-ajax) possible duplicate. – Salman Oct 19 '17 at 05:29
  • Easy way to send stuff from JavaScript to PHP (or other backends) is to use JSON, use `data: {array: JSON.stringify(times)}` to send and then `$array = json_decode($_POST['array']);` to get your array in PHP. – Walk Oct 19 '17 at 05:31
  • @insertusernamehere I have edited the post. Check it out. – M.Ghali Oct 19 '17 at 05:37

1 Answers1

0

But you're not getting and showing the response of the ajax call, to see the response you have to get the response in the success function...

PHP (test.php - Create this file and put it in the same directory):

<?php
if(isset($_POST['array']))
    print_r($_POST['array']);
?>

JQUERY:

$.ajax({  
    type: 'POST',  
    url: 'test.php',  
    data: { array: times },
    success: function(response) {  
        alert(response); 
    }  
}); 

You'll see the array content. You're sending the javascript array correctly, but if you want to see the response of the ajax call you have to get it in the success function and do something with it (alert it, add it to the DOM, etc.)

A. Iglesias
  • 2,625
  • 2
  • 8
  • 23
  • it's alerting the whole html :S – M.Ghali Oct 19 '17 at 05:58
  • Wait... what php file are you calling? I've edited the answer. Create that test.php file and test tha ajax call – A. Iglesias Oct 19 '17 at 06:01
  • @mickmackusa I meant that is not getting the response in the success function, so is not showing it. I've edited the answer trying to be more clear – A. Iglesias Oct 19 '17 at 06:06
  • I want it to give the data to the same page. Is it possible to help me out by calling me on Skype (mohammad_ghali). I would really appreciate it :) – M.Ghali Oct 19 '17 at 09:44