U.Alex U.Alex - 6 months ago 7
PHP Question

How to get php var with ajax

I have a html div (with id

example
) where i want to print php variable
$test
.
My php looks like this:

<?php

function Test(){
//some action;
return 'Hello World!';
}

$test = Test();
echo $test;

?>


Also i have javascript file :

$.get( "ajax/index.html", function( data ) {
$( "#example" ).html( data );
});


What's the problem ?

My html file :

...
<script>
function Ajax(){
$.get( "../Auth/user_save.php", function( data ) {
$( "#example" ).html( data );});
</script>
<div id = "example"></div>
<input type='submit' onClick='Ajax();'></input>

Answer

Your javascript has html link, but you are trying to call php file:

$.get( "ajax/index.html", function( data ) {
  $( "#example" ).html( data );
});

Change it to

$.get( "ajax/your_php_file.php", function( data ) {
  $( "#example" ).html( data );
});
Comments