简体   繁体   English

自动提交表单信息而无需重新加载页面

[英]Auto submit form information without page reload

I have a form with a bunch of number fields, all these fields serves the purpose to be added to each other through php. 我有一个带有一堆number字段的表单,所有这些字段用于通过php相互添加的目的。 The only problem is that for the numbers to actually be submitted and make the php function work, I have to click the submit button which reloads the page. 唯一的问题是,实际提交数量,使PHP函数的工作,我必须点击submit按钮,重新加载页面。 While this actually does work it is a bit annoying, that it has to reload and at the same time it removes the numbers from the actual form, so the user can't see where he/she has entered their numbers. 尽管此方法确实有效,但有点麻烦,它必须重新加载,同时又从实际表单中删除数字,因此用户看不到他/她在哪里输入了数字。

I know it is possible in some way to make it submit automatically, but is it possible to make the php function get the form information every time a user enters something, so that the new total sum would be automatically updated, thus not having to reload? 我知道可以通过某种方式使其自动提交,但是有可能使php函数每次用户输入内容时都获得表单信息,因此新的total sum将自动更新,因此不必重新加载?

I have my php and html in the same script, but for the case of this question I have split them into two parts. 我在同一脚本中有php和html,但是对于此问题,我将它们分为两部分。

PHP part PHP部分

    <?php

$amount_s_bp = $_POST['amountSmallBP'];
$amount_m_bp = $_POST['amountMedBP'];
$amount_l_bp = $_POST['amountLargeBP'];
$amount_xl_bp = $_POST['amountXLBP'];
$amount_s_wp = $_POST['amountSmallWP'];
$amount_m_wp = $_POST['amountMedWP'];
$amount_l_wp = $_POST['amountLargeWP'];
$amount_xl_wp = $_POST['amountXLWP'];
$amount_s_bs = $_POST['amountSmallBS'];
$amount_m_bs = $_POST['amountMedBS'];
$amount_l_bs = $_POST['amountLargeBS'];
$amount_xl_bs = $_POST['amountXLBS'];
$amount_s_bt = $_POST['amountSmallBT'];
$amount_m_bt = $_POST['amountMedBT'];
$amount_l_bt = $_POST['amountLargeBT'];
$amount_xl_bt = $_POST['amountXLBT'];
$shirt_price = 150;

$amount_total = $amount_s_bp + $amount_m_bp + $amount_l_bp + $amount_xl_bp + $amount_s_wp + $amount_m_wp + $amount_l_wp + $amount_xl_wp + $amount_s_bs + $amount_m_bs + $amount_l_bs + $amount_xl_bs + $amount_s_bt + $amount_m_bt + $amount_l_bt + $amount_xl_bt;
$price_total = $amount_total * $shirt_price;

?>

Form part 形式部分

As you can see I have a bunch of number inputs and then at the end I have a "total" area where the total sum should be displayed using <?php echo $price_total; ?> 如您所见,我有一堆number输入,然后最后有一个“总计”区域,其中应使用<?php echo $price_total; ?>显示总金额<?php echo $price_total; ?> <?php echo $price_total; ?> and <?php echo $amount_total; ?> <?php echo $price_total; ?><?php echo $amount_total; ?> <?php echo $amount_total; ?> . <?php echo $amount_total; ?>

<form action="index.php" method="post" id="orderForm" onsubmit="return checkCheckBoxes(this);">
<div class="row">
<div class="col-xs-6">
<div class="bpShirtDesign">
    <span id="bpShirtOformT">Sort Polka</span><br>

    <span id="sBP">Small</span>
    <div id="firstName">
        <input type="number" id="amount" name="amountSmallBP" placeholder="Antal"/>
    </div>

    <span id="mP">Medium</span>
    <div id="firstName">
        <input type="number" id="amount" name="amountMedBP" placeholder="Antal"/>
    </div>

    <span id="lBP">Large</span>
    <div id="firstName">
        <input type="number" id="amount" name="amountLargeBP" placeholder="Antal"/>
    </div>

    <span id="xlBP">X-Large</span>
    <div id="firstName">
        <input type="number" id="amount" name="amountXLBP" placeholder="Antal"/>
    </div>
</div>
</div>
<div class="col-xs-6">
<div class="wpShirtDesign">
    <span id="bpShirtOformT">Hvid Polka</span><br>

    <span id="sBP">Small</span>
    <div id="firstName">
        <input type="number" id="amount" name="amountSmallWP" placeholder="Antal"/>
    </div>

    <span id="mP">Medium</span>
    <div id="firstName">
        <input type="number" id="amount" name="amountMedWP" placeholder="Antal"/>
    </div>

    <span id="lBP">Large</span>
    <div id="firstName">
        <input type="number" id="amount" name="amountLargeWP" placeholder="Antal"/>
    </div>

    <span id="xlBP">X-Large</span>
    <div id="firstName">
        <input type="number" id="amount" name="amountXLWP" placeholder="Antal"/>
    </div>
</div>
</div>
</div>

<div class="row">
<div class="col-xs-6">
<div class="bsShirtDesign">
    <span id="bpShirtOformT">Bla Stribet</span><br>

    <span id="sBP">Small</span>
    <div id="firstName">
        <input type="number" id="amount" name="amountSmallBS" placeholder="Antal"/>
    </div>

    <span id="mP">Medium</span>
    <div id="firstName">
        <input type="number" id="amount" name="amountMedBS" placeholder="Antal"/>
    </div>

    <span id="lBP">Large</span>
    <div id="firstName">
        <input type="number" id="amount" name="amountLargeBS" placeholder="Antal"/>
    </div>

    <span id="xlBP">X-Large</span>
    <div id="firstName">
        <input type="number" id="amount" name="amountXLBS" placeholder="Antal"/>
    </div>
</div>
</div>
<div class="col-xs-6">
<div class="btShirtDesign">
    <span id="bpShirtOformT">Bla Tattersall</span><br>

    <span id="sBP">Small</span>
    <div id="firstName">
        <input type="number" id="amount" name="amountSmallBT" placeholder="Antal"/>
    </div>

    <span id="mP">Medium</span>
    <div id="firstName">
        <input type="number" id="amount" name="amountMedBT" placeholder="Antal"/>
    </div>

    <span id="lBP">Large</span>
    <div id="firstName">
        <input type="number" id="amount" name="amountLargeBT" placeholder="Antal"/>
    </div>

    <span id="xlBP">X-Large</span>
    <div id="firstName">
        <input type="number" id="amount" name="amountXLBT" placeholder="Antal"/>
    </div>
</div>
</div>
</div>

<div class="row">
<div class="col-md-3">
</div>
<div class="col-md-6">
<div class="bgTotals">
<input type="submit" value="Submit" id="submitValues">
<span id="totalAmount" class="h3">I alt antal = </span><?php echo $amount_total; ?><br>
<span id="totalPrice" class="h3">I alt pris =  </span> <?php echo $price_total; ?><span> DKK</span>
</div>
</div>
<div class="col-md-3">
</div>
</div>

</form>

You don't need a server-side script for this kind of processing. 您无需服务器端脚本即可进行此类处理。 Just prevent that the form is submitted with: 只需阻止表单提交即可:

<button type="button">Button</button>

Then, using javascript, retrieve the value of each form and perform calculations. 然后,使用javascript检索每个表单的值并执行计算。

//Gets the value
var value = document.getElementById('myid')
//Repeat that last line until all values are retrieved
...
//Make your calculations
var result = value + value2 //... etc

//Display your result
document.getElementById('resultid').textContent = result;

Although, if you are going to sum a lot of elements, it might be better to give the same class to those elements, so you can do this: 虽然,如果要汇总许多元素,最好为这些元素提供相同的类,因此可以执行以下操作:

var elements = document.getElementsByClassName('myclassname');
var sum = 0;

for (var i=0, max=elements.length; i < max; i++) {
    sum = sum + parseInt(elements.item(i).textContent);
}

UPDATE UPDATE

To add an event listener to every input you can iterate over every element or you could associate a listener to a parent element like this: 要将事件侦听器添加到每个input您可以遍历每个元素,也可以将侦听器与父元素相关联,如下所示:

document.getElementById('myParentElement').addEventListener('input',   function() {
 return update();
}, false);

And you should throw all the code that retrieves the values from the inputs, perform the calculations and updates the DOM in the update() function, which will get called every time an input changes. 并且您应该抛出所有从输入中检索值,执行计算并更新DOM的代码,该代码在update()函数中,每次输入更改时都会调用该代码。

First things first, you have the id "amount" on all of your inputs. 首先,您在所有输入中都具有id“金额”。 This will not work, an id can only be used once per page. 这将不起作用,一个ID每页只能使用一次。 So, figure out a convention to make these id's unique. 因此,找出使这些ID唯一的约定。 Like id='field1-id' or something. id='field1-id' Also, remove the action and method attributes from your form tag. 另外,从表单标签中删除actionmethod属性。 They'll be unneeded at this point. 此时将不再需要它们。

Then you can use jQuery and an AJAX call to do what you are looking for. 然后,您可以使用jQuery和AJAX调用来执行所需的操作。 Something like: 就像是:

$("#submitValues").bind('click', function(e){
    var formData[];

    $("#orderForm").each('input[^="-id"]', function(){
         formData.push($(this).val());
    });

   $.ajax({
      url: 'index.php',
      data: formData
   }).success(function(response){
       $("totalAmount").val(response.returnedTotalAmount);
       $("totalPrice").val(response.returnedTotalPrice);
   }).fail(function(response){
      /* do something else */
   });

 e.preventDefault();
 });

Mind you this code is untested, and very hastily written but it should give you the general idea of how to accomplish what you're after. 请注意,此代码未经测试,并且编写得很仓促,但它应该为您提供如何完成所追求的目标的总体思路。

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

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