如何在php中将变量从页面传递到另一个页面

时间:2013-10-17 16:20:16

标签: javascript php mysql

我有一个页面* book_order *,用于将订单添加到表* order_management *中,其中order_id在该表中自动递增。提交此页面后,我希望将order_id传递到其他页面* book_order2 *以在相同的order_id下添加产品。为此,我创建了seperete * order_management2 *表,其中order_id不会自动递增。

我的要求是我想将order_id从book_order页面传递到book_order2页面,并且该变量将被记住,直到我继续添加....如果我想添加新订单,我将转到book_order页面,否则我会使用book_order2页面。

book_order.php

 <div class="grid_4">
   <div class="da-panel">
   <div class="da-panel-header">
   <span class="da-panel-title">
   <img src="images/icons/color/wand.png" alt="" />
   <font face="Cambria" size="7" color="#009900"><b>Book Order</b></font>
   </span>

   </div>
   <div class="da-panel-toolbar top">
   <ul>

   <li><a href="main.php?page=view_orders"><div class="da-button blue large">View all Orders</div></a></li>

   </ul>
   </div>

   <div class="da-panel-content">



   <?php

   if(isset($_POST['submit']))
     {
       extract($_POST);

       $order_date=date("Y-m-d");

       $sql=mysql_query("select sku,quantity_in_stock,sold_quantity,crdate from stock_info where product_name = '$prod'");
       $array=mysql_fetch_array($sql);
       $sku = $array[0];
       $qis = $array[1];
       $sold_quan = $array[2];
       $crdate = $array[3];

       $sql2=mysql_query("INSERT INTO order_management(order_date,brand,product,price,customer_name,phone_number,email,address,quantity,channel,courier,order_status,sku)
    VALUES
    ('$order_date','$brand','$prod','$pri','$customername','$phonenumber', '$email','$address','$quantity','$channel','$courier','booked','$sku')");



       if($sql2)
         {
           echo "<div class='da-message success'>Successfully Booked Your Order</div>"; 
           ?>
           <script>

           var r = confirm("want to add more products?");
           if (r == true)
             {
               //x="You pressed OK!";
               window.location = "main.php?page=book_order2";
             }
           else
             {
               //x="You pressed Cancel!";
               window.location = "main.php";
             }

           </script>
           <?php
         }
       else 
         {
           die(mysql_error());
         }


       $quantity_left = $qis - $quantity;
       $sold_quan = $sold_quan + $quantity;

       $diff_in_days = (strtotime($order_date) - strtotime($crdate))/(60 * 60 * 24);
       $expctd_stock=round((7*$quantity_left)/$diff_in_days);
       //echo $expctd_stock;



       $sql3 =mysql_query("UPDATE stock_info SET quantity_in_stock = '$quantity_left',last_sold_date='$order_date', sold_quantity='$sold_quan', expected_stock='$expctd_stock' WHERE sku='$sku'");
       /*$sql3 = mysql_query("update order_management set sku='$sku' where order_date=''");*/

       $sql4 =mysql_query("select order_id from order_management where sku='$sku'");
       $idarray=mysql_fetch_array($sql4);
       $id = $idarray[0];
       //$_SESSION['id'] = $id;
     }
   ?>

   <form id="da-ex-validate1" class="da-form" method="post" action="">


   <div class="da-form-row">
   <label>Brand<span class="required">*</span></label>
   <div class="da-form-item small">
   <!--<input type="text" name="brand"  id="brand" class="required" value=""/>-->
   <select name="brand" id="brand" onChange="retrievedata(this.value)">
   <option value="">--- select brand ---</option>

   <?php
   $ord=mysql_query("select * from brand_info");

while($ord1=mysql_fetch_array($ord))
  {

    ?>  
    <option value="<?php echo $ord1['brand'];?>"><?php echo $ord1['brand'];?></option> 

    <?php

  }
?>   
</select>
</div>
</div>
<div class="da-form-row">
  <label>Product<span class="required">*</span></label>
  <div class="da-form-item small">
  <select name="prod" id="prod" onChange="retrievequantity(this.value)">
  <option value="">--- select product    ---</option>
  </select>



  </div>
  </div> 
  <div class="da-form-row">
  <label>Customer name<span class="required">*</span></label>
  <div class="da-form-item small">
  <input type="text" name="customername"  id="customername" class="required char" value=""/>
  </div>
  </div>
  <div class="da-form-row">
  <label>Phone Number<span class="required">*</span></label>
  <div class="da-form-item small">
  <input type="text" name="phonenumber"  id="phonenumber" class="required number" value=""/>
  </div>
  </div> 

  <div class="da-form-row">
  <label>Email<span class="required">*</span></label>
  <div class="da-form-item small">
  <input type="text" name="email"  id="email" class="required email" value=""/>
  </div>
  </div>
  <div class="da-form-row">
  <label>Address<span class="required">*</span></label>
  <div class="da-form-item small">
  <textarea name="address" id="address" class="required"></textarea>

  </div>
  </div>

  <div class="da-form-row">
  <label>Quantity<span class="required">*</span></label>
  <div class="da-form-item small">

  <select name="quantity" id="quantity">
  <option value=""> --- select Quantity--- </option>
  </select>

  </div>
  </div>

  <div class="da-form-row">
  <label>Total Price<span class="required">*</span></label>
  <div class="da-form-item small">
  <input type="text" name="pri" id="pri" class="required number" value=""/>
  </div>
  </div>


  <div class="da-form-row">
  <label>Courier<span class="required"></span></label>
  <div class="da-form-item small">
  <!--<input type="text" name="courier"  id="courier" class="required" value=""/>-->
  <select name="courier" id="courier">
  <option value=""> ---select courier --- </option>
  <?php
  $ord=mysql_query("select courier_name from courier_info");

while($ord1=mysql_fetch_array($ord))
  {

    ?>  
    <option value="<?php echo $ord1['courier_name'];?>"><?php echo $ord1['courier_name'];?></option> 

    <?php

  }
?>   
</select>

</div>
</div> 

<div class="da-form-row">
  <label>Channel<span class="required"></span></label>
  <div class="da-form-item small">
  <!--<input type="text" name="channel"  id="channel" class="required" value=""/>-->
  <select name="channel" id="channel">
  <option value=""> --- select channel ---</option>
  <?php
  $ord=mysql_query("select channel from channel_info");

while($ord1=mysql_fetch_array($ord))
  {

    ?>  
    <option value="<?php echo $ord1['channel'];?>"><?php echo $ord1['channel'];?></option> 

    <?php

  }
?>   
</select>
</div>
</div>
<div class="da-button-row">
  <input type="submit"  name="submit"  value="submit" class="da-button grey" />


  </div>

  </fieldset>
  </form>

  </div>
  <!-- End of .grid_4 --> </div>
  </div>

  <script>
  function retrievedata(data)
{
  var option_html = "";

  <?php
  $sql=mysql_query("SELECT distinct brand,product_name FROM stock_info");
  while($ord1=mysql_fetch_array($sql))
    {

      ?>
      if(data == '<?php echo $ord1['brand']; ?>')
        {
          option_html += "<option><?php echo $ord1['product_name']; ?></option>";
          /*alert(option_html);*/
        }
      <?php
    }

  ?>
  var par = document.getElementById("prod");
  par.innerHTML = "<option>--- select product ---</option>"+option_html;
}

function retrievequantity(product)
{
  var option_quantity_html = "";

  <?php
  $sql=mysql_query("SELECT product_name, quantity_in_stock FROM stock_info");
  while($ord2=mysql_fetch_array($sql))
    {
      $i=1;
      ?>
      if(product == '<?php echo $ord2['product_name']; ?>')
        {
          <?php
          while($i<=intval($ord2['quantity_in_stock'])){?>
            option_quantity_html += "<option><?php echo $i++; ?></option>";
            <?php }?>
        }
      <?php
    }

  ?>

  var par = document.getElementById("quantity");
  par.innerHTML = option_quantity_html;
}

</script>           




book_order2.php





<?php /*?><?php 
        $id = $_SESSION['id'];
        echo $id;
        ?><?php */?>
<?php
include("includes/db.php");



?>


<div class="grid_4">
  <div class="da-panel">
  <div class="da-panel-header">
  <span class="da-panel-title">
  <img src="images/icons/color/wand.png" alt="" />
  <font face="Cambria" size="7" color="#009900"><b>Book Order</b></font>
  </span>

  </div>
  <div class="da-panel-toolbar top">
  <ul>

  <li><a href="main.php?page=view_orders"><div class="da-button blue large">View all Orders</div></a></li>

  </ul>
  </div>

  <div class="da-panel-content">



  <?php

  if(isset($_POST['submit']))
    {
      extract($_POST);

      $order_date=date("Y-m-d");

      $sql=mysql_query("select sku,quantity_in_stock,sold_quantity,crdate from stock_info where product_name = '$prod'");
      $array=mysql_fetch_array($sql);
      $sku = $array[0];
      $qis = $array[1];
      $sold_quan = $array[2];
      $crdate = $array[3];

      $sql2=mysql_query("INSERT INTO order_management2(order_id,order_date,brand,product,price,customer_name,phone_number,email,address,quantity,channel,courier,order_status,sku)
    VALUES
    ('$id','$order_date','$brand','$prod','$pri','$customername','$phonenumber', '$email','$address','$quantity','$channel','$courier','booked','$sku')");


      if($sql2)
        {
          echo "<div class='da-message success'>Successfully Booked Your Order</div>"; 
          ?>
          <script>

          var r = confirm("want to add more products?");
          if (r == true)
            {
              //x="You pressed OK!";
              window.location = "main.php?page=book_order2";
            }
          else
            {
              //x="You pressed Cancel!";
              window.location = "main.php";
            }

          </script>
          <?php
        }
      else 
        {
          die(mysql_error());
        }


      $quantity_left = $qis - $quantity;
      $sold_quan = $sold_quan + $quantity;

      $diff_in_days = (strtotime($order_date) - strtotime($crdate))/(60 * 60 * 24);
      $expctd_stock=round((7*$quantity_left)/$diff_in_days);
      //echo $expctd_stock;



      $sql3 =mysql_query("UPDATE stock_info SET quantity_in_stock = '$quantity_left',last_sold_date='$order_date', sold_quantity='$sold_quan', expected_stock='$expctd_stock' WHERE sku='$sku'");
      /*$sql3 = mysql_query("update order_management set sku='$sku' where order_date=''");*/

      //$sql4 =mysql_query("select order_id from order_management where sku='$sku'");
      //$idarray=mysql_fetch_array($sql4);

    }
?>

<form id="da-ex-validate1" class="da-form" method="post" action="">


  <div class="da-form-row">
  <label>Brand<span class="required">*</span></label>
  <div class="da-form-item small">
  <!--<input type="text" name="brand"  id="brand" class="required" value=""/>-->
  <select name="brand" id="brand" onChange="retrievedata(this.value)">
  <option value="">--- select brand ---</option>
  <?php
  $ord=mysql_query("select * from brand_info");

while($ord1=mysql_fetch_array($ord))
  {

    ?>  
    <option value="<?php echo $ord1['brand'];?>"><?php echo $ord1['brand'];?></option> 

    <?php

  }
?>   
</select>
</div>
</div>
<div class="da-form-row">
  <label>Product<span class="required">*</span></label>
  <div class="da-form-item small">
  <select name="prod" id="prod" onChange="retrievequantity(this.value)">
  <option value="">--- select product    ---</option>
  </select>



  </div>
  </div> 
  <div class="da-form-row">
  <label>Customer name<span class="required">*</span></label>
  <div class="da-form-item small">
  <input type="text" name="customername"  id="customername" class="required char" value=""/>
  </div>
  </div>
  <div class="da-form-row">
  <label>Phone Number<span class="required">*</span></label>
  <div class="da-form-item small">
  <input type="text" name="phonenumber"  id="phonenumber" class="required number" value=""/>
  </div>
  </div> 

  <div class="da-form-row">
  <label>Email<span class="required">*</span></label>
  <div class="da-form-item small">
  <input type="text" name="email"  id="email" class="required email" value=""/>
  </div>
  </div>
  <div class="da-form-row">
  <label>Address<span class="required">*</span></label>
  <div class="da-form-item small">
  <textarea name="address" id="address" class="required"></textarea>

  </div>
  </div>

  <div class="da-form-row">
  <label>Quantity<span class="required">*</span></label>
  <div class="da-form-item small">

  <select name="quantity" id="quantity">
  <option value=""> --- select Quantity--- </option>
  </select>

  </div>
  </div>

  <div class="da-form-row">
  <label>Total Price<span class="required">*</span></label>
  <div class="da-form-item small">
  <input type="text" name="pri" id="pri" class="required number" value=""/>
  </div>
  </div>


  <div class="da-form-row">
  <label>Courier<span class="required"></span></label>
  <div class="da-form-item small">
  <!--<input type="text" name="courier"  id="courier" class="required" value=""/>-->
  <select name="courier" id="courier">
  <option value=""> ---select courier --- </option>
  <?php
  $ord=mysql_query("select courier_name from courier_info");

while($ord1=mysql_fetch_array($ord))
  {

    ?>  
    <option value="<?php echo $ord1['courier_name'];?>"><?php echo $ord1['courier_name'];?></option> 

    <?php

  }
?>   
</select>

</div>
</div> 

<div class="da-form-row">
  <label>Channel<span class="required"></span></label>
  <div class="da-form-item small">
  <!--<input type="text" name="channel"  id="channel" class="required" value=""/>-->
  <select name="channel" id="channel">
  <option value=""> --- select channel ---</option>
  <?php
  $ord=mysql_query("select channel from channel_info");

while($ord1=mysql_fetch_array($ord))
  {

    ?>  
    <option value="<?php echo $ord1['channel'];?>"><?php echo $ord1['channel'];?></option> 

    <?php

  }
?>   
</select>
</div>
</div>
<div class="da-button-row">
  <input type="submit"  name="submit"  value="submit" class="da-button grey" />
  <?php /*?><a href="book_order2.php?&id=<?php echo $id; ?>" name="submit" value="submit"  class="da-button grey">Book Order</a<?php */?>

  </div>

  </fieldset>
  </form>

  </div>
  <!-- End of .grid_4 --> </div>
  </div>

  <script>
  function retrievedata(data)
{
  var option_html = "";

  <?php
  $sql=mysql_query("SELECT distinct brand,product_name FROM stock_info");
  while($ord1=mysql_fetch_array($sql))
    {

      ?>
      if(data == '<?php echo $ord1['brand']; ?>')
        {
          option_html += "<option><?php echo $ord1['product_name']; ?></option>";
          /*alert(option_html);*/
        }
      <?php
    }

  ?>
  var par = document.getElementById("prod");
  par.innerHTML = "<option>--- select product ---</option>"+option_html;
}

function retrievequantity(product)
{
  var option_quantity_html = "";

  <?php
  $sql=mysql_query("SELECT product_name, quantity_in_stock FROM stock_info");
  while($ord2=mysql_fetch_array($sql))
    {
      $i=1;
      ?>
      if(product == '<?php echo $ord2['product_name']; ?>')
        {
          <?php
          while($i<=intval($ord2['quantity_in_stock'])){?>
            option_quantity_html += "<option><?php echo $i++; ?></option>";
            <?php }?>
        }
      <?php
    }

  ?>

  var par = document.getElementById("quantity");
  par.innerHTML = option_quantity_html;
}

</script>                      

2 个答案:

答案 0 :(得分:0)

您可以在会话中存储订单号。通过这种方式,它将在页面中受到保护和持久化。

book_order.php中插入订单时,请将其存储在会话中:

$sql2=mysql_query(....); // inserting

if($sql2){
    $_SESSION['order_id'] = mysql_insert_id();
}

现在,在book_order2.php中,您可以在插入产品之前检索订单ID:

$id = $_SESSION['order_id'];
// insert product with order_id = $id

要使用PHP会话,您必须在使用会话的任何脚本的开头调用session_start()。如果您有全局/标题包含,那么您可以在那里进行。


附注:

  • mysql_*已弃用。考虑升级到PDO或MySQLi。 This是一个很好的PDO教程,特别是如果您从mysql_*升级。
  • 使用带有绑定参数的Prepared语句,而不是将变量连接到SQL中。

答案 1 :(得分:0)

我会使用ajax。例如:

$(document).ready(function()
{
    $("form").on('submit',function(event)
    {
        event.preventDefault();

        data = "var=data";

        $.ajax
        ({
            type: "GET",
            url: "parser.php",
            data: data
        }).done(function(msg)
            {

                alert(msg);
            });
    });
});

它会将GET发送到parser.php。数据字段在data

中为$_GET['var']
相关问题