PHP 检查变量是否设置

PHP To Check If Variable Is Set

我想使用从两个不同页面链接到的一个页面。以下是我的场景:

1) The user clicks a hyperlink to be directed to the page with a salesID in the URL
2) The user navigates directly to the page and no salesID is passed in the URL

现在解释以上每种情况 ->

1) If salesID IS NULL then get parameters from the page
2) If salesID IS NOT NULL then onload() display all relevant data for the salesID passed

我知道如何编写查询以默认使用参数加载页面,并且我知道如何使用从按钮按下事件在页面上捕获的参数加载页面。我不知道该怎么做是将两者结合起来并检查多个标准。我想要做的是在 pageload() 上评估标准 1 - 如果将 salesID 传递到页面,则继续并使用该 salesID 的所有数据填充页面。如果条件 2 为真,则不要填充页面,等待用户从 select 框中输入 select 并按下按钮。

这是页面语法的基础:

<?php
    //Capture variable from URL
    $salesID = urldecode($_GET['salesID']);

    //Check if variable is set
    if (isset($_GET['salesID'])) {
        //A salesID was passed to the page so load that data by default
        //ignoring any variables set on the page
    }
    else {
        if (isset($_POST['btnpressevent'])) {
            //button has been pressed capture variables from page and run query
        }
    }
?>

您应该将 $salesID = urldecode($_GET['salesID']); 更改为 $salesID = to_utf8($_GET['salesID']);

然后简单地 if(isset($salesID) {}

GET 已经解码,您已经将其声明为变量。

除了它通常不起作用之外,我不确定你到底要问什么,但这两个更改应该有助于它按预期工作。

你可以试试这个:-

    // if salesId is not set or is null then show form with select and button

 if (!isset($_GET['salesId']) || is_null($_GET['salesId'])) {
      $html = "<form action='samepage.php'><select><option>Select one</option><option>Item 1</option></select><button name='btnpressevent'>Press button</button></form>";
      echo $html;
      return; // or die();
    }

    // do stuff with salesId
    echo "${$_GET['salesId']} helllo w";