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 '';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接