比如说一个script需要进行计算,其中一个价格乘数需要从MySQL服务器中取值,需要先将登陆服务器所需的php代码先放在整个文档的前面吗;
以及在script中怎么把php取到的值“100.00”,放进var a 中, 让var a 可以计算出 var a = 1 * "100.00" 的结果呢?
或者可以帮忙纠下错吗TAT新手提问十分感谢
<script type="text/javascript">
function displaytotcost(){
var teachingquant= document.getElementById("txtteachingitemsquant").value;
var drinkquant = document.getElementById("txtdrinksquant").value;
var dailyusequant = document.getElementById("txtdaliyusagequant").value;
var reg_price = "<?php $mysql = mysql_connect("xx", "xxxx",
"xxx");
mysql_select_db(xxxx);
$reg_price_sql = "SELECT price FROM services WHERE serviceid=x";
$result= mysql_query($reg_price_sql) or die(mysql_error());
while ($result_row = mysql_fetch_array($result)) {
$reg_price = $result_row['price'];
echo $reg_price}?>";
var pri_price = "<?php $mysql = mysql_connect("xx", "xxxx",
"xxx");
mysql_select_db(xxxx);
$pri_price_sql = "SELECT price FROM services WHERE serviceid=2";
$result= mysql_query($pri_price_sql) or die(mysql_error());
while ($result_row = mysql_fetch_array($result)) {
$pri_price = $result_row['price'];
echo $pri_price}?>";
var reg_price = "<?php $mysql = mysql_connect("xx", "xxxx",
"xxx");
mysql_select_db(xxxx);
$exp_price_sql = "SELECT price FROM services WHERE serviceid=1";
$result= mysql_query($exp_price_sql) or die(mysql_error());
while ($result_row = mysql_fetch_array($result)) {
$exp_price = $result_row['price'];
echo $exp_price}?>";
var a= (teachingquant*exp_price+drinkquant*pri_price+dailyusequant*reg_price)*1.2;
document.getElementById('a').value = "£"+a.toFixed(2);
}
</script>
<html>
<body>
<input type="button" value="Total Cost" onclick="displaytotcost();">
<input type="text" size="5" id="txtcost" onfocus="this.blur();">
</body>
</html>
使用json_encode即可. 注意后加上分号. 例如下面这个