简体   繁体   English

如何在javascript是交互式的情况下正确在php页面之间传递变量?

[英]How to pass variable between php pages correctly where javascript is interactive?

I am new in this field, and I am currently working on small eCommerce website. 我是这个领域的新手,目前正在小型电子商务网站上工作。 Therefore, it would be very much appreciated if somebody can give me any advice or tip, or link the similar question as the answer to this question. 因此,如果有人可以给我任何建议或技巧,或将类似的问题与该问题的答案联系起来,将不胜感激。

I know this post is pretty long one...however, I wanted to share my situation as precisely as possible so that people understand correctly what I am struggling with. 我知道这篇文章很长...但是,我想尽可能准确地分享我的情况,以便人们正确理解我正在努力解决的问题。 That's why it became long post...... 这就是为什么它发表了很长时间……

I have a flow and codes as you can see below. 我有一个流程和代码,如下所示。

And, where I am stack is that I don't know how I can make "Edit" button works correctly in revieworder.php page while other functions works properly together.... 而且,我的问题是我不知道如何使“编辑”按钮在revieworder.php页面中正常工作,而其他功能一起正常工作。

So, my question is how can I achieve following things simultaneously ? 因此,我的问题是如何同时实现以下目标?

  1. passing variables from revieworder.php to options.php and deliveryinformation.php page when user press “Edit” button in revieworder.php page 当用户在revieworder.php页面中按“编辑”按钮时,将变量从revieworder.php传递到options.php和deliveryinformation.php页面

  2. after user jump to deliveryinformation.php page from revieworder.php page, javascript for country selection still works properly (by changing the country, javascript returns shippingfee, retrieved from DB, depending on the selected country in the same page(deliveryinformation.php).) 用户从revieworder.php页面跳转到deliveryinformation.php页面后,用于国家/地区选择的javascript仍然可以正常工作(通过更改国家/地区,javascript返回从数据库中检索到的运费,具体取决于同一页面中的所选国家/地区(deliveryinformation.php)。 )

  3. shippingfee, totalfee and country data are passed in following flow properly 运费,总费用和国家/地区数据按以下流程正确传递
    revieworder.php → delieveryinformation.php → revieworder.php revieworder.php→delieveryinformation.php→revieworder.php
    revieworder.php → options.php → delieveryinformation.php → revieworder.php revieworder.php→options.php→delieveryinformation.php→revieworder.php

Could you please teach me how I can archive that ? 您能教我如何存档吗?

Flow of my website is here; 我的网站流程在这里;

1st page(options.php): User can choose the detail of product that they would like to order 第一页(options.php):用户可以选择要订购的产品的详细信息

2nd page(deliveryinformation.php): User is supposed to input information for delivery such as their name, address, and so on. 第二页(deliveryinformation.php):用户应该输入要发送的信息,例如姓名,地址等。 Besides, by changing the country, javascript returns shippingfee, retrieved from DB, depending on the selected country in the same page(deliveryinformation.php). 此外,通过更改国家/地区,javascript将返回从数据库中检索到的运费,具体取决于同一页面中的选定国家/地区(deliveryinformation.php)。

3rd page(revieworder.php): User can review the detail of product and information for delivery. 第三页(revieworder.php):用户可以查看产品的详细信息和交货信息。 And user can press “Edit” button to go back to options.php page or deliveryinformation.php page to change their input 用户可以按“编辑”按钮返回options.php页面或deliveryinformation.php页面以更改其输入

Codes; 代码;

options.php options.php

<form name="selection" method="POST" action="deliveryinformation.php">  
  <div class="form-group">
    <select id="dropdownforoption" name="options" class="form-control">
      <option value="" select="selected">Option</option>
      <option value="babysname">Baby's name</option>
    </select>
  </div>
  <div>
    <textarea id="request" name="request" placefolder="please type your request"></textarea>
  </div>
  <div class="form-group">
    <select id="dropdownforsize" name="size" class="form-control">
      <option value="" select="selected">Size</option>
      <option value="27cm x 24cm">27cm x 24cm</option>
      <option value="specialsize">special size</option>
    </select>
  </div>
  <button id="delivery" type="submit" onclick="return onclickvalidation()">Proceed to develivery information</button>
</form>  

deliveryinformation.php deliveryinformation.php

<?php
  $options = $_POST['options'];
  $request = $_POST['request'];
  $size = $_POST['size'];
>
<html>
  <head>
    <script type="text/javascript" src="toppage.js"></script>
  </head>
...
<form name="delivery" method="POST" action="revieworder.php">
  <input type="hidden" name="options" value="<?= $options; ?>"/>
  <input type="hidden" name="request" value="<?= $request; ?>"/>
  <input type="hidden" name="size" value="<?= $size; ?>"/>
  <div class="row">
    <div class="col-md-4">
      <p>Name</p>
      <p>Country</p>
    </div>
    <div class="col-md-4">
      <input id="name" name="name" placeholder="please type your name">Name</input>
      <div class="form-group">
        <select id="countryselection" name="country" class="form-control">
          <option value="" select="selected">Choose your country</option>
          <option value="germany" select="selected">Germany</option>
          <option value="other" select="selected">Other</option>
        </select>
      </div>
    </div>
    <div class="col-md-4">
      <div class="col-md-6">
        <p>shipping fee</p>
        <p>total fee</p>
      </div>
      <div class="col-md-6">
        <p class="shippingfee">EUR <input type="hidden" name="shippingfee" id="shippingfees2"><span id="shippingfees">xxx</span></input></p>
        <p class="totalfee">EUR <input type="hidden" name="totalfee" id="totalfees2"><span id="totalfees">xxx</span></input></p>
      </div>
    </div>
  </div>
..
</html>

toppage.js toppage.js

$(document).ready(function(){
$('#countryselection').change(function(){
    if($('#countryselection').val()!='Germany'){
        var country = 'Other'
    }else{var country = 'Germany'}

    var priceforframe = document.delivery.framefees.value;

    $.ajax({ 
        type: 'GET', 
        url: '/shippingfee.php', 
        data: {'countryforshippingfee': country},
        dataType: 'json',
        success: function (data) { 

            document.getElementById("shippingfees2").value = data;
            $('#shippingfees').html(data);

            var total = data + 50;

            document.getElementById("totalfees2").value = total;
            $('#totalfees').html(total);
        }
    });
}); 
});

function submitForm(action){
  document.getElementById('forminreviewpage').action = action;
  document.getElementById('forminreviewpage').submit();
}

revieworder.php revieworder.php

<?php
  $options = $_POST['options'];
  $request = $_POST['request'];
  $size = $_POST['size'];
  $name = $_POST['name'];
  $country = $_POST['country'];
  $shippingfee = $_POST['shippingfee'];
  $totalfee = $_POST['totalfee'];
?>
<html>
..
<form id="forminreviewpage" name="confirmation" method="POST">
  <input type="hidden" name="options" value="<?= $options; ?>"/>
  <input type="hidden" name="request" value="<?= $request; ?>"/>
  <input type="hidden" name="size" value="<?= $size; ?>"/>
  <input type="hidden" name="name" value="<?= $name; ?>"/>
  <input type="hidden" name="country" value="<?= $country; ?>"/>
  <input type="hidden" name="shippingfee" value="<?= $shippingfee; ?>"/>
  <input type="hidden" name="totalfee" value="<?= $totalfee; ?>"/>
</form>
<div class="row">
  <div class="col-md-4">
    <div class="col-md-4">
      <p><?= $options; ?></p>
      <p><?= $request; ?></p>
      <p><?= $size; ?></p>
    </div>
    <div class="col-md-4">
      <button id="editbutton" onclick="submitForm('options.php')">Edit</button>
    </div>
  </div>
  <div class="col-md-4">
    <div class="col-md-4">
      <p><?= $name; ?></p>
      <p><?= $country; ?></p>
      <p><?= $shippingfee; ?></p>
      <p><?= $totalfee; ?></p>
    </div>
    <div class="col-md-4">
      <button id="editbutton" onclick="submitForm('deliveryinformation.php')">Edit</button>
    </div>
  </div>
  <div>
    <button id="orderbutton" onclick="submitForm('ordered.php')">Order</button></br>
  </div>
</div>

Thank you so much for your help and best regards, 非常感谢您的帮助和最诚挚的问候,
Hiro

Using sessions will enable pages within your site to access a 'pool' of data - providing you set session variables yourself. 使用会话将使您站点中的页面能够访问“数据池”-允许您自己设置会话变量。 This can easily be done in PHP - see this page on W3 for basic details, there are plenty of other sites on the net with easy walkthroughs of sessions once you've got the basic concept. 这可以在PHP中轻松完成-有关基本详细信息, 请参见 W3上的此页面 ,一旦您掌握了基本概念,网络上还有许多其他站点可以轻松地进行会话的演练。

I'm not quite sure what you're asking in question two. 我不太确定您在第二个问题中要问什么。 Is it that you want the second page to automatically know the user's country? 您是否要第二页自动知道用户所在的国家? If so, this could be achieved by assigning a session variable on the previous page, or by using geo-location - see help here 如果是这样,可以通过在上一页上分配会话变量或使用地理位置来实现-请参见此处的帮助

As for question three, achieving proper flow is an architectural issue - you'll need to ensure that the pages visited by the user structured to only allow the through-flow that you want. 对于第三个问题,实现适当的流量是一个体系结构问题-您需要确保用户访问的页面的结构仅允许所需的流量。 Without spending hours working through your code, I should imagine this can again be achieved through assigning session variables when a page is successfully 'completed' and ready to move to the next page. 无需花费大量时间处理您的代码,我应该想象可以通过在页面成功“完成”并准备移至下一页时分配会话变量来再次实现这一点。

Sessions are powerful, and will allow you to control a lot of user data to ease their experience of your site throughout their period of use. 会话功能强大,可让您控制大量用户数据,以减轻他们在整个使用期间的网站使用体验。 Just be careful about what session data you record though - it should always be the minimum amount possible, and not containing any data that could compromise the user's personal details, or their account - ie don't store their username and password, just store an int of '1' or '0' if they pass authentication. 不过,请务必注意要记录的会话数据-它应该始终是最小量,并且不包含任何可能危及用户个人详细信息或帐户的数据-即不存储用户名和密码,只需存储如果它们通过身份验证,则为“ 1”或“ 0”的int值。

Hope that helps. 希望能有所帮助。

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

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