0

Just trying to get the result of query to the javascript array or something.

<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<title>Admin check</title>
<meta charset="UTF-8">
</head>

<body>
<script type='text/javascript'> 
<?php
include 'pdo_connect.php';
function pleaseWork() {
    return dataQuery("SELECT * FROM `grupy`")->fetchAll();
}

$work = pleaseWork();

echo "jArray = JSON.parse(<?php echo JSON_encode($work);?>);";
?>
</script>

Got code like that and there is the result:

<br />
<b>Notice</b>:  Array to string conversion in      <b>/virtual/chemioterapia2137.cba.pl/adminCheck.php</b> on line <b>20</b><br />
jArray = JSON.parse(<?php echo JSON_encode(Array);?>);  //jArray = JSON.parse('');

How can I get it working?

7
  • Why are you opening php tags in a string? Commented Feb 16, 2016 at 15:25
  • Sorry but this is a good example of "spaghetti code" You need to separate your code. A good rule is to separate PHP from JS as much as possible. Commented Feb 16, 2016 at 15:27
  • you forgot to set your var as well. Commented Feb 16, 2016 at 15:28
  • 2
    @stevenhawkingsbiggestfan just trying to help you out man. Commented Feb 16, 2016 at 15:32
  • 1
    It never ceases to amaze me - people come here for help, but then complain about the comments. yes, it may work without the var, but it's not right without the var. Listen to the comments, people are trying to help! Commented Feb 16, 2016 at 15:34

2 Answers 2

3

A couple of errors:

  • Your code is very messy. Its easy to separate this. Try to place the most PHP outside of your JS.
  • When using json_encode it is already turning your array into a readable JS object. So no need for parsing.

Rewrite your code in this manner, I created the array result manually to mimic your DB results:

<?php

function pleaseWork()
{
    return array(
        array(
            "name" => "John",
            "age" => 52
        ),
        array(
            "name" => "Jane",
            "age" => 48
        )
    );
}

$work = pleaseWork();
$json = json_encode($work);
?>
<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
        <title>Admin check</title>
        <meta charset="UTF-8">
    </head>
    <body>
        <script type='text/javascript'>
            var jArray = <?= $json ?>;
            console.log(jArray);
        </script>
    </body>
</html>

your browser's console result:

0: Object
  age: 52
  name: "John"

1: Object
  age: 48
  name: "Jane"
Sign up to request clarification or add additional context in comments.

7 Comments

And what if I want to make php function that I can use in javascript for making querys? Like makeQuery("DELETE * FROM ``grupy`` WHERE 1");
I would create that function in PHP, and use AJAX to run it through JS.
Any tips? I never did ajax, I don't know where to start.
Sure. I suggest opening up a new question here on SO and I can provide a thorough answer. Just let me know when you do so that I can check it out.
Ok but I have to wait 90 minutes to do that.
|
3

Just change it to:

echo "var jArray = JSON.parse(". JSON_encode($work) .");";

4 Comments

Damn I was so close, I tried to do echo "jArray = JSON.parse(. JSON_encode($work) .);";
Uncaught SyntaxError: Unexpected token o pointing on [object Object],[object Object]
What is your code now? I think now its a problem with your js: stackoverflow.com/questions/8081701/…
When I get rid of JSON_parse it throws <b>Notice</b>: Array to string conversion</b> jArray = . JSON_encode(Array) .; </script> I just added this attribute to connection $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); and changed echo echo "jArray = . JSON_encode($work) .;";.

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.