重新加载同一页面后,将post值赋给变量或session var

时间:2014-02-24 19:05:26

标签: php variables post assign

我有一个php页面接受上一页的发布值,这个发布的值被分配给一个变量,但是在同一页面我有一个重新加载页面的按钮,导致发布的值为“null”。我得到未确定的索引通知。而且我也尝试用isset函数解决它也无法正常工作。尝试将它分配给会话变量,我也得到相同的错误!关于如何解决这个问题的任何想法??

<?php
session_start();
include_once("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping Cart</title>
<link href="style/style.css" rel="stylesheet" type="text/css">
</head>

<body>
<div id="products-wrapper">
    <h1>products</h1>
    <div class="products">
    <?php
    //current URL of the Page. cart_update.php redirects back to this URL

    $current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    if (isset ($_POST["shop_id"])) { $_SESSION["shop_id"] =  $_POST['shop_id'];}
    $results = $mysqli->query("SELECT * FROM products2 where shop_id='".$_SESSION["shop_id"]."'");
    if ($results) { 

        //fetch results set as object and output HTML
        while($obj = $results->fetch_object())
        {
            echo '<div class="product">'; 
            echo '<form method="post" action="cart_update.php">';
            echo '<div class="product-thumb"><img src="images/'.$obj->product_img_name.'"></div>';
            echo '<div class="product-content"><h3>'.$obj->product_name.'</h3>';
            echo '<div class="product-desc">'.$obj->product_desc.'</div>';
            echo '<div class="product-info">';
            echo 'Price '.$currency.$obj->price.' | ';
            echo 'Qty <input type="text" name="product_qty" value="1" size="3" />';
            echo '<button class="add_to_cart">Add To Cart</button>';
            echo '</div></div>';
            echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
            echo '<input type="hidden" name="type" value="add" />';
            echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
            echo '</form>';
            echo '</div>';
        }

    }
    ?>
    </div>

1 个答案:

答案 0 :(得分:0)

试试这个,在页面顶部添加@session_start();

 if (isset ($_POST["shop_id"])) { $_SESSION["shop_id"] =  $_POST['shop_id'];}