简体   繁体   English

如何根据下拉菜单中的选择结果相乘

[英]How To Multiply Result Based on Selection from Drop Down Menu

I'm using a Wordpress plugin that I'm trying to customize for my needs but I'm very new to Javascript. 我正在使用一个Wordpress插件,试图根据自己的需要进行自定义,但是我对Javascript还是很陌生。 The plugin calculates the distance between two zip codes which is what $distance is. 该插件计算两个邮政编码之间的距离,即$ distance是什么。 I created a drop down menu with different values based on vehicle size. 我创建了一个下拉菜单,根据车辆大小使用不同的值。 What I want to do is display the distance multiplied by the value assigned to each vehicle size (which is the cost per mile). 我要做的是显示距离乘以分配给每种车辆尺寸的值(即每英里成本)。 Any help would be appreciated. 任何帮助,将不胜感激。 Thank you. 谢谢。

<?php
/**
* Plugin Name: WP Distance Calculator
* Plugin URI: http://phpcodingschool.blogspot.com/
* Description: This plugin claculates distance between two near by locations.
* Version: 1.0.0
* Author: Monika Yadav
* Author URI: http://phpcodingschool.blogspot.com/
* License: GPL2
*/

class DistanceWPCalculator
{
public function __construct()
{       //action definations
        add_shortcode( 'distance_calculator',  array( &$this, 'distanceWPfrontend' ) ); 

        add_action( 'wp_ajax_nopriv_distancewpcalculator', array( &$this, 'distancewpcalculator_calculate' ) );
        add_action( 'wp_ajax_distancewpcalculator', array( &$this, 'distancewpcalculator_calculate' ) );

        add_action( 'init', array( &$this, 'init' ) );
}

public function init()
{
    wp_enqueue_script( 'distancewpcalculator', plugin_dir_url( __FILE__ ) . 'js/calculatedistance.js', array( 'jquery' ) );
    wp_localize_script( 'distancewpcalculator', 'DistanceCalculator', array(
        'ajaxurl' => admin_url( 'admin-ajax.php' )
    ) );
    ?>
    <script>
    var ajaxurl =  "<?php echo admin_url('admin-ajax.php'); ?>";
    </script>
    <?php
    wp_enqueue_style( 'DistanceWPCalculator-Style', plugin_dir_url( __FILE__ ) . 'css/style.css', array(), '0.1', 'screen' );
}

public function distancewpcalculator_calculate()
{   
    // The $_POST contains all the data sent via ajax
    if ( isset($_POST) ) {

    $from = urlencode($_POST['from']);
    $to = urlencode($_POST['to']);
    $data = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/json?origins=$from&destinations=$to&language=en-EN&sensor=false");
    $data = json_decode($data);
    $time = 0;
    $distance = 0;
        foreach($data->rows[0]->elements as $road) {
            $time += $road->duration->value;
            $distance += $road->distance->value;
        }
        $time =$time/60;
        $distance =round($distance/1609.344);

        //Output
        if($distance!=0){

        echo "<div id='result_generated'>";
        echo "From: ".$data->origin_addresses[0];
        echo "<br/>";
        echo "To: ".$data->destination_addresses[0];
        echo "<br/>";
        echo "Time: ".gmdate("H:i", ($time * 60))." hour(s)";
        echo "<br/>";
        echo "Distance: ".$distance." mile(s)";
        echo "</div>";         
        }else{
        echo "Sorry only nearby distance can be calculated."; 
        }                
       }

die();

}

//Function to display form on front-end
public function distanceWPfrontend( $atts ) {

?>  
    <form method ="post" id="calculator" >
        <div class="DC_title">Distance Calculator</div>
        <input type="text" id="from" name="from" placeholder="From.."></br>
        <input type="text" id="to" name="to" placeholder="To.."></br>
        <select id="carType" onchange="carsize()">
            <option value=".45">Motorcycle</option>
            <option value=".6">Small Sedan</option>
            <option value=".65">Large Sedan</option>
            <option value=".7">Wagon/Hatchback</option>
            <option value=".7">Small SUV</option>
            <option value=".8">Large SUV</option>
            <option value=".8">Minivan</option>
            <option value=".75">Small Truck</option>
            <option value=".8">Large Truck</option>
        </select>
        <input type="button" id="calculate" name="calculate" value="Calculate">
    </form></br>
    <div id="result"></div> 
    <?php
}

}

$distancewpcalculator = new DistanceWPCalculator();

?>

This is actually PHP. 这实际上是PHP。 In your distancewpcalculator_calculate() function you calculate the distance based on the results that the Google Maps API returns. distancewpcalculator_calculate()函数中,您将根据Google Maps API返回的结果计算距离。 You store the calculated distance in the $distance variable (specifically $distance =round($distance/1609.344);) . 您将计算出的距离存储在$distance变量中(特别是$distance =round($distance/1609.344);) In that assignment you convert the recorded $distance value from Km to Mi. 在该分配中,您将记录的$distance值从Km转换为Mi。

You should modify your HTML select tag and values to something like this: 您应该将HTML选择标记和值修改为如下形式:

<select id="carType" name="carType">
  <option value=".45">Motorcycle</option>
  <option value=".6">Small Sedan</option>
  <option value=".65">Large Sedan</option>
  <option value=".7">Wagon/Hatchback</option>
  <option value=".7">Small SUV</option>
  <option value=".8">Large SUV</option>
  <option value=".8">Minivan</option>
  <option value=".75">Small Truck</option>
  <option value=".8">Large Truck</option>
</select>

Doing this will include the "carType" value in the $_POST data. 这样做将在$_POST数据中包含“ carType”值。 Now that it is in the post data, you can get the value selected by doing $_POST['carType'] . 现在它已在发布数据中,您可以通过执行$_POST['carType']获得所选值。 Now that data is recorded we can multiply the value of the distance by the carType selected. 现在已经记录了数据,我们可以将距离值乘以所选的carType。

public function distancewpcalculator_calculate()
{
    // The $_POST contains all the data sent via ajax
    if (isset($_POST)) {
        ...
        $distance = round($distance / 1609.344);

        $carType = $_POST['carType'];
        $cost = $carType * $distance;

        //Output
        if ($distance != 0) {
            ...
            echo "Distance: " . $distance . " mile(s)";
            echo "<br />";
            echo "Cost: {$cost}";
            echo "</div>";
        } else {
            ...
        }
    }
}

The calculated $cost is a product of the selected vehicle type ( $carType ) and the calculated distance between the two locations ( $distance ); 计算出的$cost是所选车辆类型( $carType )与两个位置之间的计算出的距离( $distance )的乘积;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM