0

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?

3
  • what is the value of $init? Commented Nov 27, 2013 at 1:06
  • a string. example, "john" Commented Nov 27, 2013 at 1:12
  • I want to nake it an array like, "John" => owner, "John2" => user1, "john3" => user2. Commented Nov 27, 2013 at 1:14

1 Answer 1

1

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

10 Comments

console.log(data); can be init = data[''];?
well you can do init = data as you did in your initial code. Now init will have an array as you wanted. Use console.log to check the value of init and see if that's what you want.
saying "something is wrong" won't help at all. include this in the very beginning of your file: error_reporting(E_ALL|E_STRICT); ini_set('display_errors', true);
I got a values undefined.
well then you showed the wrong code, because your variable is called "value", not "values"
|

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.