In my applications I often use AJAX calls to get certain variables along with html code, sampe JSON:
'result' => 1,
'error' => 0,
'someVar' => 'abc',
'outputHTML' => '<table>....</table>',
//of course below code doesn't work
$output = '<table>'.$some_var.'</table>';
require are used to load PHP code into your application.
To get the content of a file and store it in a variable use
Do note that given path to the file is relative to the entry script. This might give unexpected results when your script is included from somewhere else. To counter this, use the
__DIR__ magic constant.
Final code would look like:
$response['outputHTML'] = file_get_contents(__DIR__ . '/table-html.php');
First of all, you might want to look at a templating engine like Twig, and save yourself some trouble.
If you want to do it yourself, the only thing you need to change in your code is to return a variable at the end of
table-html.php. I'd suggest using Heredoc to create readable and easy templating.
<?php return <<<EOT <table> <tr><td>$firstname</td><td>$lastname</td></tr> </table> EOT;