如何将php变量发送到另一个php页面?

时间:2016-04-15 12:30:11

标签: php

首先,我将id发送到rec_update.php文件,如下所示:

location.href="http://localhost/pk/rec_update.php?id="+id;

在rec_update.php文件中,我像这样访问该值:

$id = $_GET['id'];

现在,我想将此$ id发送到rec_update1.php文件。可以通过单击按钮调用。在按钮上我已经应用了一个javascript函数,页面将指向rec_update1.php 如何在另一个php文件中获取此值。

2 个答案:

答案 0 :(得分:1)

  

HTML / HTTP是无状态的,换句话说,就是你所做的/看到的   上一页,与当前页面完全断开连接。   除非你使用会话,cookie或GET / POST之类的东西   变量。通过会话,会话和cookie非常易于使用   远比cookie更安全。更安全,但不完全   安全

<强>会话

<?php
//On page 1
$_SESSION['varname'] = $var_value;

//On page 2
$var_value = $_SESSION['varname'];
?>
  

请记住在这两个页面上运行session_start()语句   在尝试访问$ _SESSION数组之前,以及任何之前   输出发送到浏览器。

答案 1 :(得分:1)

  

在rec_update.php

   <td><input type="text" name="oweight[]" id="oweight[]"  class="form-control input-sm mb15 wweight" required  maxlength="5" onkeypress="javascript:return isNumber (event)" required></td>
   <td><input type="text" name="orweight[]" id="orweight[]" class="form-control input-sm mb15 rweight" onblur="wtotal()" maxlength="5" required onkeypress="javascript:return isNumber (event)" required></td>
   <div class="col-sm-12">
   <label class="col-sm-4 control-label">Total Weight:</label>
   <label class="col-sm-4 control-label" style="text-align:left;" id="weight"></label> </div>
  

在rec_update1.php中

function wtotal() {
    var wweight = document.getElementsByClassName("wweight");
    var rweight = document.getElementsByClassName("rweight");
    var total_wweight = 0;
    var total_rweight = 0;
    var total_tweight = 0;
    for (var i = 0; i < wweight.length; i++) {
      for (var j = 0; j < rweight.length; j++) {


        if (parseInt(rweight[j].value) > parseInt(wweight[i].value)) {
            total_rweight=parseInt(rweight[j].value);
            total_tweight = total_tweight + total_rweight;
            console.log("total_rweight=="+total_rweight);

        } else {
            total_wweight= parseInt(wweight[i].value);
            total_tweight = total_tweight + total_wweight;
            //console.log("total_wweight=="+total_wweight);
        }
        //total_tweight =total_rweight + total_wweight;
        document.getElementById("weight").innerHTML =total_tweight;
        //console.log("total_tweight=="+total_tweight);
      }
    }