dongpanbo4727 2014-08-13 14:38
浏览 22
已采纳

从表单发布到同一页面时ISSET的解决方法

I'm trying to test whether a $_GET variable is set when a specific PHP page is visited:

I have a form which posts back to the same page (see below) below the form is PHP code to check whether the product_id variable is set and performs actions accordingly.

FORM

<form action="form_update_products.php" method="post" name="search" id="search">         
          <table width="100%" cellspacing="0" cellpadding="2">
            <tr>
              <td colspan="2" class="text-18">
                View / Update Product Barcodes by Product ID:
              </td>
            </tr>
            <tr>
              <td colspan="2" align="right">
                <img src="images/spacer.png" width="100" height="4" />
              </td>
            </tr>
            <tr>
              <td width="12%" align="right">
                <input name="product_id" type="text" class="size-box" id="product_id"
                       onclick="this.value = '';" style="font-size: 22px; width:500px"/>
              </td>
              <td width="88%">
                <input name="submit" type="submit" class="submit-button"
                       id="submit" value="View Product Barcodes" />
</form>

form_update_products.php

<?php 

$product_id=$_POST['product_id'];

header('Location: update_products.php?id='.$product_id.'');

?>

PHP

if (isset($_GET['id']))
      {
        $product_id = $_GET['id'];
      }

      if (!isset($product_id))
      {
        echo '';
      }
      else { // output something }

For some reason the $_GET['id'] variable always seems to default to the same product_id and only changes when entering a different product_id in the form. Even when closing the browser and opening the page afresh, the $_GET['id'] variable is set.

Can anybody tell me why the $_GET['id'] variable always seems to be set?

  • 写回答

1条回答 默认 最新

  • dongyou6795 2014-08-13 14:41
    关注

    the id is always set you need to do a empty() check empty() does the same as isset() but will also check if something is entered

    if(!empty($_GET['id'])) {
          $product_id = $_GET['id'];
          //your code
    } else {
       echo '';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式