如何使用动态值将php变量传递给javascript函数

时间:2014-10-21 20:42:48

标签: javascript php

嗨亲爱的Javascript专家, 我想将php变量传递给javascript函数

这是正常工作的主要功能:

// Get Windows Price
function getwindowsPrice()
{
    var WindowsPrice=0;

    var theForm = document.forms["next"];
    var InsideWindows = theForm.elements["ins_windows"];

    if(InsideWindows.checked==true)
    {
        WindowsPrice=10;
    }
    return WindowsPrice;
}

但我需要WindowsPrice = 10;到WindowsPrice =;其中$ ins_win_pri = 10;

我尝试过但它没有用,

// Get Windows Price
function getwindowsPrice()
{
    var WindowsPrice=0;

    var theForm = document.forms["next"];
    var InsideWindows = theForm.elements["ins_windows"];

    if(InsideWindows.checked==true)
    {
        WindowsPrice=<?php echo $ins_win_pri; ?>;
    }
    return WindowsPrice;
}

全功能:

<?php
 include("sag-admin/config.php");
            $query = mysql_query("select * from pri_man");
            $total_rows = mysql_num_rows($query);
            $total_date = mysql_fetch_array($query);

                         $fix_pri = $total_date["fix_pri"];

                          $beb_pri = $total_date["beb_pri"];
                           $bath_pri = $total_date["bath_pri"];

                            $cle_pro_pri = $total_date["cle_pro_pri"];

                             $one_ti_pri = $total_date["one_ti_pri"];
                              $end_of_le_pri = $total_date["end_of_le_pri"];
                               $weekly_pri = $total_date["weekly_pri"];
                                $fortnightly_pri = $total_date["fortnightly_pri"];

                              $tot = $fix_pri + $beb_pri + $bath_pri + $cle_pro_pri ;
                               $tot_fin = $tot * $fortnightly_pri;

?>


<script>

/** Javascript calculation by Uzzal (abdulawal.com) ) **/ 
 var bedroom = new Array();
 bedroom["1"]=<?php echo $beb_pri; ?>;
 bedroom["2"]=<?php echo $beb_pri * 2; ?>;
 bedroom["3"]=<?php echo $beb_pri * 3; ?>;
 bedroom["4"]=<?php echo $beb_pri * 4; ?>;
 bedroom["5"]=<?php echo $beb_pri * 5; ?>;
 bedroom["6"]=<?php echo $beb_pri * 6; ?>;

var bathroom = new Array();
 bathroom["1"]=<?php echo $bath_pri; ?>;
 bathroom["2"]=<?php echo $bath_pri * 2; ?>;
 bathroom["3"]=<?php echo $bath_pri * 3; ?>;
 bathroom["4"]=<?php echo $bath_pri * 4; ?>;
 bathroom["5"]=<?php echo $bath_pri * 5; ?>;
 bathroom["6"]=<?php echo $bath_pri * 6; ?>;

 var clean_product = new Array();
 clean_product["1"]=<?php echo $cle_pro_pri; ?>;
 clean_product["0"]=0;   

 var frequency = new Array();
 frequency["1"]=<?php echo $one_ti_pri; ?>;
 frequency["1.85"]=<?php echo $end_of_le_pri; ?>;
 frequency["0.65"]=<?php echo $weekly_pri; ?>;
 frequency["0.65"]=<?php echo $fortnightly_pri; ?>;

//Get Bedroom Price
function getBedroomPrice()
{  
    var BedroomPrice=0;
    var theForm = document.forms["next"];
    var bedRoom = theForm.elements["bedroom"];
    for(var i = 0; i < bedRoom.length; i++)
    {
        //if the radio button is checked
        if(bedRoom[i].checked)
        {
            BedroomPrice = bedroom[bedRoom[i].value];
            break;
        }
    }
    return BedroomPrice;
}

// Get Bathroom Price
function getBathroomPrice()
{  
    var BathroomPrice=0;

    var theForm = document.forms["next"];

    var bathRoom = theForm.elements["bathroom"];

    //We loop through each radio buttons
    for(var i = 0; i < bathRoom.length; i++)
    {
        //if the radio button is checked
        if(bathRoom[i].checked)
        {
            BathroomPrice = bathroom[bathRoom[i].value];

            break;
        }
    }

    return BathroomPrice;
}

// Clean Product Price

function getcpPrice()
{  
    var cpPrice=0;

    var theForm = document.forms["next"];

    var cleanProduct = theForm.elements["clean_product"];

    for(var i = 0; i < cleanProduct.length; i++)
    {
        //if the radio button is checked
        if(cleanProduct[i].checked)
        {
            cpPrice = clean_product[cleanProduct[i].value];

            break;
        }
    }
    return cpPrice;
}

// Get Frequency
function getFrequency()
{  
    var Frequency=0;

    var theForm = document.forms["next"];

    var FreQuency = theForm.elements["frequency"];

    for(var i = 0; i < FreQuency.length; i++)
    {
        //if the radio button is checked
        if(FreQuency[i].checked)
        {

            Frequency = frequency[FreQuency[i].value];

            break;
        }
    }
    return Frequency;
}
// Get Fridge Price 

function getfridgePrice()
{
    var FridgePrice=0;

    var theForm = document.forms["next"];

    var InsideFridge = theForm.elements["ins_fridage"];

    if(InsideFridge.checked==true)
    {
        FridgePrice = <?php echo (json_encode($isn_fri_pri)); ?>;

    }
    return FridgePrice;

}

// Get Oven Price
function getovenPrice()
{
    var OvenPrice=0;

    var theForm = document.forms["next"];
    var InsideOven = theForm.elements["ins_oven"];

    if(InsideOven.checked==true)
    {
        OvenPrice=<?php echo (json_encode($ins_ove_pri)); ?>;
    }
    return OvenPrice;
}

//get Cabinets Price
function getcabinetsPrice()
{
    var CabinetsPrice=0;

    var theForm = document.forms["next"];
    var InsideCabinets = theForm.elements["ins_cabinets"];

    if(InsideCabinets.checked==true)
    {
        CabinetsPrice='<?php echo $ins_cab_pri; ?>';
    }
    return CabinetsPrice;
}

// Get Windows Price
function getwindowsPrice()
{
    var WindowsPrice=0;

    var theForm = document.forms["next"];
    var InsideWindows = theForm.elements["ins_windows"];

    if(InsideWindows.checked==true)
    {
        WindowsPrice='<?php echo $ins_win_pri; ?>';
    }
    return WindowsPrice;
}

function calculateTotal()
{
    var totPrice = 80 + getBedroomPrice() + getBathroomPrice() + getcpPrice() + getfridgePrice() + getovenPrice() + getcabinetsPrice() + getwindowsPrice();
    var freq = getFrequency();
    var finalPrice = totPrice * freq;
    var divobj = document.getElementById('pp_tk');
    divobj.style.display='inline-block';
    divobj.innerHTML = "$"+finalPrice;

}

function hideTotal()
{
    var divobj = document.getElementById('pp_tk');
    divobj.style.display='inline-block';
}

</script>

<span class="price_amnt" id="pp_tk"> <?php echo $tot_fin; ?></span>

2 个答案:

答案 0 :(得分:0)

这是JavaScript中的一个热门问题。我建议使用一些全局对象来保存从PHP传递的变量,即:

<script type="text/javascript" src="your_script.js"></script>
<script type="text/javascript">
    window.vars = window.vars || {};
    window.vars.foo = window.vars.foo || "<?php echo $foo; ?>";
    window.vars.bar = window.vars.bar || "<?php echo $bar; ?>";
</script>

your_script.js

function foo() {
    alert(window.vars.foo + window.vars.bar);
    // or
    alert(vars.foo + vars.bar);
    // BUT! Be careful not to have another local 'vars' value in second case.
}

答案 1 :(得分:0)

是的,我解决了这个问题:)

使用此类型表格:)

<?php
$php_var = "Hello world from PHP";
?>

<html>
  <head>
    <title>Pass variable from PHP to JavaScript - Cyberster's Blog'</title>
  </head>
  <body>
    <script>
      var js_var = "<?php echo $php_var; ?>";
        alert(js_var);
    </script>
  </body>
</html>

修改:

// Get Oven Price
function getovenPrice()
{
    var OvenPrice=0;

    var theForm = document.forms["next"];
    var InsideOven = theForm.elements["ins_oven"];

    if(InsideOven.checked==true)
    {
        OvenPrice = <?php echo $ins_ove_pri; ?>;
    }
    return OvenPrice;
}

完整代码:

<?php
 include("sag-admin/config.php");
            $query = mysql_query("select * from pri_man");
            $total_rows = mysql_num_rows($query);
            $total_date = mysql_fetch_array($query);

                         $fix_pri = $total_date["fix_pri"];

                          $beb_pri = $total_date["beb_pri"];
                           $bath_pri = $total_date["bath_pri"];

                            $cle_pro_pri = $total_date["cle_pro_pri"];

                            $isn_fri_pri = $total_date["isn_fri_pri"];
                              $ins_ove_pri = $total_date["ins_ove_pri"];
                               $ins_cab_pri = $total_date["ins_cab_pri"];
                                $ins_win_pri = $total_date["ins_win_pri"];

                             $one_ti_pri = $total_date["one_ti_pri"];
                              $end_of_le_pri = $total_date["end_of_le_pri"];
                               $weekly_pri = $total_date["weekly_pri"];
                                $fortnightly_pri = $total_date["fortnightly_pri"];

                              $tot = $fix_pri + $beb_pri + $bath_pri + $cle_pro_pri ;
                               $tot_fin = $tot * $fortnightly_pri;

?>


<script>

/** Javascript calculation by Uzzal (abdulawal.com) ) **/ 
 var bedroom = new Array();
 bedroom["1"]=<?php echo $beb_pri; ?>;
 bedroom["2"]=<?php echo $beb_pri * 2; ?>;
 bedroom["3"]=<?php echo $beb_pri * 3; ?>;
 bedroom["4"]=<?php echo $beb_pri * 4; ?>;
 bedroom["5"]=<?php echo $beb_pri * 5; ?>;
 bedroom["6"]=<?php echo $beb_pri * 6; ?>;

var bathroom = new Array();
 bathroom["1"]=<?php echo $bath_pri; ?>;
 bathroom["2"]=<?php echo $bath_pri * 2; ?>;
 bathroom["3"]=<?php echo $bath_pri * 3; ?>;
 bathroom["4"]=<?php echo $bath_pri * 4; ?>;
 bathroom["5"]=<?php echo $bath_pri * 5; ?>;
 bathroom["6"]=<?php echo $bath_pri * 6; ?>;

 var clean_product = new Array();
 clean_product["1"]=<?php echo $cle_pro_pri; ?>;
 clean_product["0"]=0;   

 var frequency = new Array();
 frequency["1"]=<?php echo $one_ti_pri; ?>;
 frequency["1.85"]=<?php echo $end_of_le_pri; ?>;
 frequency["0.65"]=<?php echo $weekly_pri; ?>;
 frequency["0.65"]=<?php echo $fortnightly_pri; ?>;

//Get Bedroom Price
function getBedroomPrice()
{  
    var BedroomPrice=0;
    var theForm = document.forms["next"];
    var bedRoom = theForm.elements["bedroom"];
    for(var i = 0; i < bedRoom.length; i++)
    {
        //if the radio button is checked
        if(bedRoom[i].checked)
        {
            BedroomPrice = bedroom[bedRoom[i].value];
            break;
        }
    }
    return BedroomPrice;
}

// Get Bathroom Price
function getBathroomPrice()
{  
    var BathroomPrice=0;

    var theForm = document.forms["next"];

    var bathRoom = theForm.elements["bathroom"];

    //We loop through each radio buttons
    for(var i = 0; i < bathRoom.length; i++)
    {
        //if the radio button is checked
        if(bathRoom[i].checked)
        {
            BathroomPrice = bathroom[bathRoom[i].value];

            break;
        }
    }

    return BathroomPrice;
}

// Clean Product Price

function getcpPrice()
{  
    var cpPrice=0;

    var theForm = document.forms["next"];

    var cleanProduct = theForm.elements["clean_product"];

    for(var i = 0; i < cleanProduct.length; i++)
    {
        //if the radio button is checked
        if(cleanProduct[i].checked)
        {
            cpPrice = clean_product[cleanProduct[i].value];

            break;
        }
    }
    return cpPrice;
}

// Get Frequency
function getFrequency()
{  
    var Frequency=0;

    var theForm = document.forms["next"];

    var FreQuency = theForm.elements["frequency"];

    for(var i = 0; i < FreQuency.length; i++)
    {
        //if the radio button is checked
        if(FreQuency[i].checked)
        {

            Frequency = frequency[FreQuency[i].value];

            break;
        }
    }
    return Frequency;
}
// Get Fridge Price 

function getfridgePrice()
{
    var FridgePrice = 0;

    var theForm = document.forms["next"];

    var InsideFridge = theForm.elements["ins_fridage"];

    if(InsideFridge.checked==true)
    {
        FridgePrice = <?php echo $isn_fri_pri; ?>;

    }
    return FridgePrice;

}

// Get Oven Price
function getovenPrice()
{
    var OvenPrice=0;

    var theForm = document.forms["next"];
    var InsideOven = theForm.elements["ins_oven"];

    if(InsideOven.checked==true)
    {
        OvenPrice = <?php echo $ins_ove_pri; ?>;
    }
    return OvenPrice;
}

//get Cabinets Price
function getcabinetsPrice()
{
    var CabinetsPrice=0;

    var theForm = document.forms["next"];
    var InsideCabinets = theForm.elements["ins_cabinets"];

    if(InsideCabinets.checked==true)
    {
        CabinetsPrice = <?php echo $ins_cab_pri; ?>;
    }
    return CabinetsPrice;
}

// Get Windows Price
function getwindowsPrice()
{
    var WindowsPrice=0;

    var theForm = document.forms["next"];
    var InsideWindows = theForm.elements["ins_windows"];

    if(InsideWindows.checked==true)
    {
        WindowsPrice = <?php echo $ins_win_pri; ?>;
    }
    return WindowsPrice;
}

function calculateTotal()
{
    var totPrice = <?php echo $fix_pri; ?> + getBedroomPrice() + getBathroomPrice() + getcpPrice() + getfridgePrice() + getovenPrice() + getcabinetsPrice() + getwindowsPrice();
    var freq = getFrequency();
    var finalPrice = totPrice * freq;
    var divobj = document.getElementById('pp_tk');
    divobj.style.display='inline-block';
    divobj.innerHTML = "$"+finalPrice;

}

function hideTotal()
{
    var divobj = document.getElementById('pp_tk');
    divobj.style.display='inline-block';
}

</script>