如何从PHP向Javascript发送变量?

How do I send variable value from PHP to Javascript using Ajax? Below is the JS code and PHP code

PHP CODE:

<?php
    $username = "trainerapp";
    $password = "password";
    $hostname = "localhost";
    $link = @mysql_connect($hostname, $username, $password);

    if(@mysql_select_db("trainer_registration"))
    {
        echo "Connected Successfully";
    }
    else
    {
        echo "Connection Error";
    }



    $select_query_num = @mysql_query("select id,program_name,company,date_prog from program_details");
    $num_rows = @mysql_num_rows($select_query_num);
    for ($i = 0; $i <= $num_rows; $i++){
            $id = $_POST["idjs"];
            $pgmname = $_POST["pgmnamejs"];
            $comp = $_POST["compjs"];
            $datephp = $_POST["datephpjs"];
        $select_query = @mysql_query("select id,program_name,company,date_prog from program_details where id = $i");
        $fetch_query = @mysql_fetch_assoc($select_query);
        $id =  $fetch_query['id'];
        echo $id;
        $pgmname = $fetch_query['program_name'];
        echo $pgmname;
        $comp =  $fetch_query['company'];
        echo $comp;
        $datephp = $fetch_query['date_prog'];
        echo $datephp;  

        }
?>

JS CODE:

window.onload = function createdivs() {
        var id;
        var pgmname;
        var comp;
        var datephp;
        var i = 1;
        for (;i < 10;i++)
        {
            div = "<div>.display";
            var list = document.createElement("div");
            document.getElementById('fulldisplay').appendChild(list);
            list.className = "container content-rows";
        }
        $.ajax({
        url:'displaycontent.php',
        data:{idjs:id, pgmnamejs:pgmname, compjs:comp, datephpjs:datephp},
        type:'POST',
        success:function(retval){
        alert(retval);
        }
    });

    }

Questions:

In JS CODE, for every increment of var i, I need to make a Ajax call to the PHP file which should return the first array of values and second and so on. I'm actually confused of how to do this. An explanation would be better. By the above code I get only the last array value with an unidentified index error.

If this is not a formal work then just echo the HTML in the php script.

$select_query = @mysql_query("select id,program_name,company,date_prog from program_details where id = $i");
$fetch_query = @mysql_fetch_array($select_query. MYSQL_NUM);
list($id, $pgname, $comp, $datephp) = $fetch_query;
echo "
  <div>
    $id $pgname $comp $datephp
  </div>
"; 

I'm no php expert, but it seems like you should have something like:

echo '<script type="text/javascript">';
// bunch of:
echo '  var myvar = ' + $fetch_query['myvar'] + ';';
// probably use a proper way to escape those things
echo '</script>';

or anything that builds a small global accessible version of your php variables...

The best way being:

  • have a REST api providing your data in json or whatever you like
  • use it!

I've seen a comment about angularjs, spending some time using a proper javascript framework is definitely not wasting your time IMO!

You need to use json encode on the results that come from the $select_query then you can use ajax to get that results into javascript

//echo the json_encode in php to see the results
json_encode($select_query)

//do ajax with jquery
$.ajax({
    url: 'the page that has the $select_query which brings the results',
    type: 'POST',
    dataType: 'json',
    data: data,
    succes: function(data){
                console.log(data);
    }
});

Now you can manipulate the data as you want.