John Bobs
John Bobs

Reputation: 539

get data as an array using ajax post

I have a code that will get the data from the php file. Here it is:

function getMods(){
    var init;
    $.ajax({
    type: "POST",
    url: "init.php",
    data: { 'id': getID()},
    cache: false,
    success: function(data)
        {
            init = data;
        },
        async:false
    });
    return init;
}

Here is the php file:

<?php
include('dbconnect.php');
if(isset($_POST['id'])){
$id = $_POST['id'];
$value = "";
$init = array("Name","Owner","Admin1","Admin2","Admin3","Admin4");
for($i = 0; $i > 6;$i++){
$value[$i] = chatMods($init[$i],$id,$username,$password);
}
echo json_encode($value);
}
?>

Php file send data that is a string. I want to send a data that is an array of string. How can I do that?

Upvotes: 0

Views: 1685

Answers (1)

Matthew
Matthew

Reputation: 11280

If you want to send an array from your PHP, the best is to use json :

In your PHP file:

$data = array('hello', 'world');
echo json_encode($data);

JS:

$.ajax({
type: "POST",
dataType: "json",  
url: "init.php",
data: { 'id': getID(), 'Name': name },
cache: false,
success: function(data)
    {
        console.log(data);
    },
    async:false
});

Upvotes: 1

Related Questions