0

I can't seem to figure out what's the problem. For my next little project I'm creating dynamic web page with database and etc. I need to get all the necessary variables from PHP file. But for some reason I cannot do it if I include another PHP file. (I need it for database queries).

main.php

include ('databaseQueries.php');

    if (isset($_POST["points"])){
        $points = json_decode($_POST["points"]);
        if($_POST["execute"] == 1){

        }
    }

    $advert= array(
        'Hello' => 'Hello world!',
        'bye' => 'Why bye?',
     );

    echo json_encode($advert, $another);

pageJs.js

$.ajax({
        url : 'php/main.php',
        type : 'POST',
        dataType : 'json',
        success : function (result) {
            console.log(result);
        },
        error : function (err) {
           console.log("Failed");
        }
    })

databaseQueries.php

$another = "another string";

If I remove the include and $another variable from json_encode. Everything works and I get object in console log. But if I leave the those two things, Ajax call fails.

What I'm doing wrong and how can I get both the $test array and $another variable?

Thank's in advance!

5
  • 1
    Where is $advert defined? Commented Mar 18, 2016 at 14:17
  • "fails" how? What is the response from the server? Commented Mar 18, 2016 at 14:18
  • Can you use try catch inside the PHP code snippet? Commented Mar 18, 2016 at 14:19
  • What are you doing in include ('databaseQueries.php');? Are you echo'ing stuff, because if you do, the response you're sending back is not valid JSON Commented Mar 18, 2016 at 14:23
  • Corrected the mistake of "$advert". David - the ajax error function executes and spits out in the console "Failed". Elias- it's just that one variable for now. :) Commented Mar 18, 2016 at 15:22

2 Answers 2

6

You are using json_encode incorrectly. From the documentation:

string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )

You are trying to send $another to the function as the options parameter.

You could do something like this:

$ret = array($advert, $another);
echo json_encode($ret);
Sign up to request clarification or add additional context in comments.

1 Comment

Both you and Greg where corrected. I was using json_encode wrong. Thanks!
2

Unless I'm completely wrong, I can't see where you're sending anything TO your post

$.ajax({
    url : 'php/main.php',
    type : 'POST',
    dataType : 'json'
    // I would expect a data call here, something like:
    data: $(form).serialize(), // OR { points: 3, execute: 1 }
    success : function (result) {
        console.log(result);
    },
    error : function (err) {
       console.log("Failed");
    }
})

I assume that you want to spit back some results with the format of result->key;

So Keeleon's answer above is good:

$ret = array($advert, $another);
echo json_encode($ret);

But you can also do:

//Adding everything to array, and asking json_encode to encode the array is the key. Json_encode encodes whatever is in the first argument passed to it.
$ret = array('advert'=>$advert, 'another'=>$another,'test'=>$test);
echo json_encode($ret);

Hopefully, this answers your questions.

Comments

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.