1

I am building a website using a technique I've never seen, and I may have just found out why, but I thought I'd check with those who know better than I. My project is set up with a Tasks.php that looks something like:

<?php
    if($_GET['task'] == 'task1'){
        $response = {'someTag':'someValue'};
    }
    if($_GET['task'] == 'task2'){
        $response = {'someTag':'someValue'};
    }
    if($_GET['task'] == 'task3'){
        if($_GET['info'] == 'someInfo'){
            $response = {'someTag':'someValue'};
        }
    }
    echo json_encode($response);
    exit;
?>

this way, in my js I can essentially call php functions with ajax I.E.

$.get('Tasks.php',{'task':'task3','info':'someInfo'},function(response){
    var data = JSON.parse(response);
    console.log(data);
});

which would log a js object litteral:

{'someTag':'someValue'}

The problem I have is that my php has lost the functionality to do something like echo "$(body).append($('<div>'));"; Which would (using jquery) append a div to my document's body. The reason I'm doing it this way is because I want all my pages to have the html extension for other stuff. I would assume there's a way to possibly have that div there already, and reference it by id, or somehow make it visible with just php, and maybe my assumption is off base, or I need a plugin or extension or something, but, I need to confirm my crazyness(or stupidity).

Spencer Cornwall
  • 289
  • 2
  • 14

1 Answers1

1

You're getting a JSON response and you can't get both HTML and JSON, so you're on the right track. You should have a a 'success' block in your ajax that tells it what to do with the JSON that comes back. There are TONS of tutorials out there on how to do this so I don't need to duplicate the code here. But if you get stuck after this, then come back and ask another question. Good luck!

Difster
  • 3,264
  • 2
  • 22
  • 32