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.