简体   繁体   English

异步显示表单元素

[英]Asynchronously show up form element

I want to asynchronously show up the price on my form before it gets submitted. 我想在提交表单之前异步显示其价格。 I have values saved on an array in a PHP file where the prices will be based on. 我将值保存在价格将基于的PHP文件的数组中。 I formulated the following code based on my research. 我根据自己的研究制定了以下代码。

HTML 的HTML

<form>
<select id="beds-input-select">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>

<select id="bath-input-select">
<option value="1.0">1</option><option value="1.5">1.5</option>
<option value="2.0">2</option><option value="2.5">2.5</option>
<option value="3.0">3</option><option value="3.5">3.5</option>
<option value="4.0">4</option><option value="4.5">4.5</option>
<option value="5.0">5</option><option value="5.5">5.5</option>
<option value="6.0">6</option>
</select>

<div id="frequency-options">  
<input type="radio" name="frequency" id="one" checked="checked">
<label for="one">One Time Cleaning</label>

<input type="radio" name="frequency" id="weekly">
<label for="weekly">Weekly Cleaning</label>

<input type="radio" name="frequency" id="biweekly">
<label for="biweekly">Bi-Weekly Cleaning</label>
<input type="radio" name="frequency" id="monthly">
<label for="monthly">Monthly Cleaning</label>
</div>

<h5>Pay Only</h5>
<div class="estimated-price-div">$<span id="estimated-price">0</span></div>
<p><input type="submit" value="Schedule An Appointment Now!" class="estimate-submit"></p>
</form>

JAVASCRIPT JAVASCRIPT

function updatePrice() {
var bed_select = document.getElementById("beds-input-select");
var bath_select = document.getElementById("bath-input-select");
var frequency_options = document.getElementsByName("frequency");

var estimate_price = document.getElementById("estimated-price");

var bed_id = bed_select.options[bed_select.selectedIndex].value;
var bath_id = bath_select.options[bath_select.selectedIndex].value;
var frequency_id = frequency_options.checked[frequency_options.selectedIndex].value;

var url = 'subcategories.php?selected_bed_id=' + bed_id + '&selected_bath_id=' + bath_id + '&selected_frequency_id' + frequency_id;

var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
        estimate_price.innerHTML = xhr.responseText;
    }
}
xhr.send();
}

var bed_select = document.getElementById("beds-input-select");
bed_select.addEventListener("change", updatePrice);

var bath_select = document.getElementById("bath-input-select");
bath_select.addEventListener("change", updatePrice);

var frequency_options = document.getElementsByName("frequency");
frequency_options.addEventListener("change", updatePrice);

PHP 的PHP

$pricing = [
['frequency' => one, 'beds' => 1 , 'baths' => 1 , 'price' => 90],
['frequency' => one, 'beds' => 1 , 'baths' => 1.5 , 'price' => 113],
['frequency' => one, 'beds' => 1 , 'baths' => 2 , 'price' => 113],
['frequency' => one, 'beds' => 1 , 'baths' => 2.5 , 'price' => 135],
['frequency' => one, 'beds' => 1 , 'baths' => 3 , 'price' => 135],
['frequency' => one, 'beds' => 1 , 'baths' => 3.5 , 'price' => 158],
['frequency' => one, 'beds' => 1 , 'baths' => 4 , 'price' => 158],
['frequency' => one, 'beds' => 1 , 'baths' => 4.5 , 'price' => 180],
['frequency' => one, 'beds' => 1 , 'baths' => 5 , 'price' => 180],
['frequency' => one, 'beds' => 1 , 'baths' => 5.5 , 'price' => 203],
['frequency' => one, 'beds' => 1 , 'baths' => 6 , 'price' => 203],

['frequency' => weekly, 'beds' => 1 , 'baths' => 1 , 'price' => 81],
['frequency' => weekly, 'beds' => 1 , 'baths' => 1.5 , 'price' => 101],
['frequency' => weekly, 'beds' => 1 , 'baths' => 2 , 'price' => 101],
['frequency' => weekly, 'beds' => 1 , 'baths' => 2.5 , 'price' => 122],
['frequency' => weekly, 'beds' => 1 , 'baths' => 3 , 'price' => 122],
['frequency' => weekly, 'beds' => 1 , 'baths' => 3.5 , 'price' => 142],
['frequency' => weekly, 'beds' => 1 , 'baths' => 4 , 'price' => 142],
['frequency' => weekly, 'beds' => 1 , 'baths' => 4.5 , 'price' => 162],
['frequency' => weekly, 'beds' => 1 , 'baths' => 5 , 'price' => 162],
['frequency' => weekly, 'beds' => 1 , 'baths' => 5.5 , 'price' => 182],
['frequency' => weekly, 'beds' => 1 , 'baths' => 6 , 'price' => 182]
];

//PASS VALUES from the selected bed_id, bath_id, frequency_id and output $result;

echo $result;

How can I use the values selected from the form as used in (var url = 'subcategories.php?selected_bed_id=' + bed_id + '&selected_bath_id=' + bath_id + '&selected_frequency_id' + frequency_id;) to output the result on my HTML Id #estimated-price. 如何使用从(var url ='subcategories.php?selected_bed_id ='+ bed_id +'&selected_bath_id ='+ bath_id +'&selected_frequency_id'+ frequency_id;)中使用的形式中选择的值在HTML ID上输出结果#预计售价。

I do not know how to connect my php array with the chosen values. 我不知道如何用所选值连接我的php数组。 So for example, the user selects VALUE "1" for BEDS, VALUE "2" for BATHS and CHOOSES "WEEKLY" for FREQUENCY, it should OUTPUT PRICE "101". 因此,例如,用户为BEDS选择值“ 1”,为BATHS选择值“ 2”,为频率选择“每周”,它应该输出价格“ 101”。 I'm apparently new to PHP and not very familiar with it yet. 我显然是PHP新手,对它还不太熟悉。 Can somebody help me on this? 有人可以帮我吗? Thank you! 谢谢!

Your problem is that when you use getElementsByName you will get an array not a single element. 您的问题是,当您使用getElementsByName ,将获得一个数组而不是单个元素。 So, I change the code to handle the array as follows: 因此,我将代码更改为如下处理数组:

Note your updatePrice function is ok. 请注意,您的updatePrice函数正常。 If your subcategories.php will return a value, then updatePrice will print it out into your estimate_price variable. 如果您的subcategories.php将返回一个值,则updatePrice会将其输出到您的estimate_price变量中。

Update In the function updatePrice you are sending a GET request. 更新在功能updatePrice您正在发送GET请求。 So in your subcategories.php file you need to receive those variables using $_GET . 因此,在您的subcategories.php文件中,您需要使用$_GET接收这些变量。 Then do your calculation and print it out with echo . 然后进行计算,并用echo打印出来。

Last Update Replace the values one with 'one' and weekly with 'weekly' because those are string variables if you use it without ' php will assume that is a constant. 最后更新替换值one'one' ,并weekly'weekly' ,因为这些都是字符串变量,如果你使用它,而' PHP将认为是一个常数。

<?php
    $pricing = [
    ['frequency' => 'one', 'beds' => 1 , 'baths' => 1 , 'price' => 90],
    ['frequency' => 'one', 'beds' => 1 , 'baths' => 1.5 , 'price' => 113],
    ['frequency' => 'one', 'beds' => 1 , 'baths' => 2 , 'price' => 113],
    ['frequency' => 'one', 'beds' => 1 , 'baths' => 2.5 , 'price' => 135],
    ['frequency' => 'one', 'beds' => 1 , 'baths' => 3 , 'price' => 135],
    ['frequency' => 'one', 'beds' => 1 , 'baths' => 3.5 , 'price' => 158],
    ['frequency' => 'one', 'beds' => 1 , 'baths' => 4 , 'price' => 158],
    ['frequency' => 'one', 'beds' => 1 , 'baths' => 4.5 , 'price' => 180],
    ['frequency' => 'one', 'beds' => 1 , 'baths' => 5 , 'price' => 180],
    ['frequency' => 'one', 'beds' => 1 , 'baths' => 5.5 , 'price' => 203],
    ['frequency' => 'one', 'beds' => 1 , 'baths' => 6 , 'price' => 203],

    ['frequency' => 'weekly', 'beds' => 1 , 'baths' => 1 , 'price' => 81],
    ['frequency' => 'weekly', 'beds' => 1 , 'baths' => 1.5 , 'price' => 101],
    ['frequency' => 'weekly', 'beds' => 1 , 'baths' => 2 , 'price' => 101],
    ['frequency' => 'weekly', 'beds' => 1 , 'baths' => 2.5 , 'price' => 122],
    ['frequency' => 'weekly', 'beds' => 1 , 'baths' => 3 , 'price' => 122],
    ['frequency' => 'weekly', 'beds' => 1 , 'baths' => 3.5 , 'price' => 142],
    ['frequency' => 'weekly', 'beds' => 1 , 'baths' => 4 , 'price' => 142],
    ['frequency' => 'weekly', 'beds' => 1 , 'baths' => 4.5 , 'price' => 162],
    ['frequency' => 'weekly', 'beds' => 1 , 'baths' => 5 , 'price' => 162],
    ['frequency' => 'weekly', 'beds' => 1 , 'baths' => 5.5 , 'price' => 182],
    ['frequency' => 'weekly', 'beds' => 1 , 'baths' => 6 , 'price' => 182]
    ];

    if( $_GET['selected_bed_id'] && $_GET['selected_bath_id'] && $_GET['selected_frequency_id']) {

        $selected_bed_id = $_GET['selected_bed_id'];
        $selected_bath_id = $_GET['selected_bath_id'];
        $selected_frequency_id = $_GET['selected_frequency_id'];

        foreach( $pricing as $element ) {
            if( $element['frequency'] == $selected_frequency_id && 
            $element['beds'] == $selected_bed_id && 
            $element['baths'] == $selected_bath_id) {
                echo $element['price'];
                break;
            }
        }
    }

 var bed_select = document.getElementById("beds-input-select"); bed_select.addEventListener("change", updatePrice); var bath_select = document.getElementById("bath-input-select"); bath_select.addEventListener("change", updatePrice); var frequency_options = document.getElementsByName("frequency"); frequency_options.forEach(function(element) { // console.log(element); element.addEventListener("change", updatePrice); }); function updatePrice() { var estimate_price = document.getElementById("estimated-price"); var bed_id = bed_select.options[bed_select.selectedIndex].value; var bath_id = bath_select.options[bath_select.selectedIndex].value; var radios = document.querySelectorAll('input[type="radio"]:checked'); var frequency_id = radios.length > 0 ? radios[0].id : null; var url = 'subcategories.php?selected_bed_id=' + bed_id + '&selected_bath_id=' + bath_id + '&selected_frequency_id=' + frequency_id; alert( 'selected_bed_id=' + bed_id + '\\n'+ 'selected_bath_id=' + bath_id + '\\n'+ 'selected_frequency_id=' + frequency_id + '\\n'); var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { estimate_price.innerHTML = xhr.responseText; } } xhr.send(); } 
 <form> <select id="beds-input-select"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select> <select id="bath-input-select"> <option value="1.0">1</option><option value="1.5">1.5</option> <option value="2.0">2</option><option value="2.5">2.5</option> <option value="3.0">3</option><option value="3.5">3.5</option> <option value="4.0">4</option><option value="4.5">4.5</option> <option value="5.0">5</option><option value="5.5">5.5</option> <option value="6.0">6</option> </select> <div id="frequency-options"> <input type="radio" name="frequency" id="one" checked="checked"> <label for="one">One Time Cleaning</label> <input type="radio" name="frequency" id="weekly"> <label for="weekly">Weekly Cleaning</label> <input type="radio" name="frequency" id="biweekly"> <label for="biweekly">Bi-Weekly Cleaning</label> <input type="radio" name="frequency" id="monthly"> <label for="monthly">Monthly Cleaning</label> </div> <h5>Pay Only</h5> <div class="estimated-price-div">$<span id="estimated-price">0</span></div> <p><input type="submit" value="Schedule An Appointment Now!" class="estimate-submit"></p> </form> 

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

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