[英]How do I send variable to Javascript from PHP?
How do I send variable value from PHP to Javascript using Ajax? 如何使用Ajax将变量值从PHP发送到Javascript? Below is the JS code and PHP code
下面是JS代码和PHP代码
PHP CODE: PHP代码:
<?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: JS代码:
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. 在JS CODE中,对于var i的每个增量,我都需要对PHP文件进行Ajax调用,该调用应返回第一个值数组,然后返回第二个数组,依此类推。 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. 如果这不是正式工作,则只需在php脚本中回显HTML。
$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: 我不是php专家,但看来您应该有类似的东西:
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... 或任何构建您的php变量的小型全局可访问版本的东西...
The best way being: 最好的方法是:
I've seen a comment about angularjs, spending some time using a proper javascript framework is definitely not wasting your time IMO! 我看过有关angularjs的评论,花一些时间使用适当的JavaScript框架绝对不会浪费您的时间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 您需要对来自$ select_query的结果使用json编码,然后可以使用ajax将结果获取到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. 现在,您可以根据需要操纵数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.