簡體   English   中英

如何使用動態值將php變量傳遞給javascript函數

[英]how to pass php variable to javascript function using dynamic value

嗨親愛的 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>

這是 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.
}

是的,我解決了它:)

通過使用這種類型的格式:)

<?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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM