0

is it possible to add an php code directly to the Ajax Query instead of extern url?

My Ajax:

<div id="show"></div>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        setInterval(function () {
            $('#show').load('data.php')
        }, 3000);
    });
</script>

i dont want use the extern file data.php, can i add an php code directly?

Like this one:

<?php 

include('includes/config.php');


if ($connection->connect_error) {
    die("Connection error: " . $connection->connect_error);
}
$result = $connection->query("SELECT input_address FROM payments");
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo $row['input_address'] . '<br>';
    }


} else {
    echo "Wait..";
}


?>

Example:

 $('#show').load(' <?php myphpcode?> ')
irislori
  • 9
  • 2
  • 2
    Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – B001ᛦ Nov 07 '18 at 14:35
  • No, you can't. PHP is server-side, if you try to run PHP code in the client-side (like what you're trying to do in your example), it simply won't execute. – José A. Zapata Nov 07 '18 at 15:19

1 Answers1

1

The question is a bit baffling, the purpose of AJAX is to allow for the browsers (front-end), which run JavaScript, to communicate with servers (back-end) that may be using other technologies. The servers may be running any variety of environments, among them, PHP.

If you want to keep a single language between front and back end, take a look at node.js.

Asinus Rex
  • 535
  • 9
  • 29