0
<?php 
function get()
{
    if(isset($_POST['enquire'])
     {

            $flying_to = $_POST['flying_to'];
            $str = "<b>".$flying_to."</b>";
            echo $str;
    }   
}  
?>

<script>
    function table()
    {
    var a = '<?php get(); ?>';
    document.getElementById("table").innerHTML = a;
    }
</script>

I am calling a php function through a javascript but I am able to see just a constant predefined value which I used earlier for testing purposes. I want to actually create a table in HTML and in that I am appending it using javascript and fetching the data from database using php.

Thanks in advance.

Kuro Neko
  • 795
  • 12
  • 19
  • The 1st thing I would change is going from `echo $str;` to `return $str;` and from `'` to `'`. – Guido Faecke Jun 01 '22 at 02:45
  • I tried using but the data is not reflecting in the webpage – Suraj Joshi Jun 01 '22 at 02:54
  • A `var_dump($_POST)` within your `get()` method might reveals why it is not showing what you expect. – Guido Faecke Jun 01 '22 at 02:59
  • First Check if your `$_POST['enquire']` is having value or not ! – Sho Jun 01 '22 at 05:26
  • 1
    _"I am calling a php function through a javascript"_ - no, you are not. Check the explanation inside the duplicate. – CBroe Jun 01 '22 at 07:05
  • PHP runs _first_ when your page is being loaded, _before_ your JavaScript - that only executes after the page has loaded into the browser. Anything your PHP get() function outputs will effectievly be hard-coded into the JS code which gets rendered to the browser. You can use the browser's View Source feature to see what I mean - you'll notice that `` is nowhere to be seen - it's been executed, and its output (if any) has been output into the page, in the middle of the JS code. To call it again from JS, you'd need to use AJAX to make a new HTTP request to the server & PHP script. – ADyson Jun 01 '22 at 07:52

0 Answers0