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

从表单发布到同一页面时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 '';
    }
    
    打赏 评论

相关推荐 更多相似问题