1

这是一个自我提交的表格。我有 3 个独立的选择列表,它们的选择值作为 sql 查询的 WHERE 参数传递。

当我点击按钮时,所有数据都显示得很好,有限,链接页面在下面。但:

当我点击页面链接时,结果集和所有链接都消失了(只有选择列表仍然可见)。我认为这与 isset 条件有关,如果尚未设置值,则显示表单,如果已设置值,则处理表单。因此,第一个结果集正确显示,但是当您尝试转到第二页时,我的表单解释为未设置选择列表,因为它们已重置,并且如果我点击链接,我没有点击按钮,而是因此,它消除了它不是 html 表单的所有内容。

分页代码没有错误。错误在于我如何将此表单与此分页方法联系起来。我不知道我应该怎么做。

        if (isset($_POST['submitted'])) { /***************IF IT IS SET, WE PROCESS THE VALUES*************************/
        $oldcountry = FALSE;
        $oldfrom = FALSE;
        $oldinto = FALSE;

 // Here just checking that all variables have been selected

       if (isset($_POST['country']))


        {
        $oldcountry = $_POST['country'];
        $country = filter_var($oldcountry, FILTER_SANITIZE_STRING);
        }
        else {
        echo 'Please, select a country';
        }
        if (isset($_POST['from_language']))
        {
        $oldfrom = $_POST['from_language'];
        $from_language = filter_var($oldfrom, FILTER_SANITIZE_STRING);
        }
        else {
        echo 'no has metido el from language';
        }
        if (isset($_POST['into_language']))
        {
        $oldinto = $_POST['into_language'];
        $into_language = filter_var($oldinto, FILTER_SANITIZE_STRING);
        }

    // so, once we have selected them and clicked the button, we go for the query

    // and paginate the results


        require_once('bdd1.php');
        $per_page = 6;
        $pages_query = mysql_query("SELECT COUNT(FName)
        FROM work_assignment, developer
        WHERE AES_DECRYPT(country, 'elperrodesanroquenotienerabo') = '".$country."'
        AND from_language = '".$from_language."'
        AND into_language = '".$into_language."'
        AND work_assignment.developer_id = developer.developer_id
        ORDER BY FName ASC ");
        $pages = ceil (mysql_result($pages_query, 0) /$per_page);
        $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
        $start = ($page - 1) * $per_page;
        $query = mysql_query("SELECT FName
        FROM work_assignment, developer
        WHERE AES_DECRYPT(country, 'elperrodesanroquenotienerabo') = '".$country."'
        AND from_language = '".$from_language."'
        AND into_language = '".$into_language."'
        AND work_assignment.developer_id = developer.developer_id
        ORDER BY FName ASC LIMIT $start, $per_page");
        while ($query_row = mysql_fetch_assoc($query)){
        echo '<p>', $query_row['FName'], '</p>';
        }
        if ($pages >=1) {
        for($x = 1; $x <=$pages; $x++){
        echo '<a href="?page='.$x. '">'.$x.'</a> ';
        }
        }
        } /************* END OF IF ISSET TO PROCESS *******************************/

    ?>

更新更新

在所有帮助之后,我设法编写了一个 URL 字符串,使页面链接工作。然而,当我硬编码国家名称(基本上是爱沙尼亚)而不是嵌入代表它的变量时,我仍然需要对其进行调整,但我无法正确编写该 URL:

这有效:

echo '<a href="?country=Estonia&from_language=Russian&into_language=Latvian&submitted=true&
page='.$x. '">'.$x.'</a> ';

我只需要用 $country 替换爱沙尼亚,用 $from_language 替换俄语,用 $into_language 替换拉脱维亚语。但是我已经尝试了单引号和双引号以及点的所有可能组合,但我得到了语法错误。有人知道怎么写吗?

4

2 回答 2

1

正如 Slomo 指出的那样,POST 变量会丢失,因为单击链接时未提交表单。

除非您希望对用户隐藏这些值,否则您可以将表单的方法设置为 GET 而不是 POST。这样,变量就会添加到 URL 中,就像 $_GET['page'] 一样。

然后必须以保持 URL 完整的方式呈现链接,但需要更改的页面变量除外。可以这样做(只需将 currentpage 更改为 page):

分页链接中重复参数的问题?

更新:

如果要在字符串中插入变量,可以将它们放在双引号内(并在字符串内转义其他双引号),或连接字符串和变量,请参见以下两个示例:

echo "<a href=\"?country=$country&from_language=$from_language&into_language=$into_language&submitted=true&page=$x\">$x</a>";
echo '<a href="?country=' . $country . '&from_language=' . $from_language . '&into_language=' . $into_language . '&submitted=true&page=' . $x . '">' . $x . '</a>';
于 2012-06-04T18:20:31.620 回答
0

$_POST 值仅在使用 POST 请求发送信息时设置,例如您的最有可能的方法 = POST。当您使用 POST (button=submit) 方法提交表单时,会设置 $_POST 值。每当您单击另一个链接时,只会向服务器发送对新页面的请求。

如果您需要在第一次提交表单后提供信息,您应该使用 Session。(http://php.net/manual/de/features.sessions.php) 为每个客户端(网络浏览器)存储会话,并且在会话超时之前可用(请参阅手册)。您必须在第一次提交表单时设置会话变量。之后,您可以使用会话中的值。

/编辑

如果您不想使用会话,可以将过滤条件作为 url 参数 (index.php?groupby=country) 添加到普通链接。然后将使用 $_GET 访问它们。

于 2012-06-04T17:47:42.813 回答