如何在表单提交时保留已设置的 GET 参数值?

2023-10-12php开发问题
2

本文介绍了如何在表单提交时保留已设置的 GET 参数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个 URL:foo.php?name=adam&lName=scott,在 foo.php 中我有一个表单,它给了我 矩形长度 &rectangleBreadth 带有提交按钮.

I have a URL : foo.php?name=adam&lName=scott, and in foo.php I have a form which gives me values of rectangleLength & rectangleBreadth with a submit button.

当我点击表单操作为 $_SERVER['REQUEST_URI'] 的提交按钮时,我得到这个结果 URL:foo.php?rectangleLength=10&rectangleBreadth=5代码>(这些值已由用户填写).

When I click this submit button with form action as $_SERVER['REQUEST_URI'], I get this result URL: foo.php?rectangleLength=10&rectangleBreadth=5 (these values have been filled in by the user).

请注意,我丢失了以前的值 name &lName 来自 URL.

Notice that I am losing my previous values name & lName from the URL.

我怎样才能保留它们?

另外,请记住,我必须回到 foo.php,如果用户想再次提交表单,那么长度和宽度值应该改变.

Also, keep in mind that I have to come back to foo.php and if the user wants to submit the form again then the length and breadth values should change.

推荐答案

您可以在第一个目标站点的表单中添加两个隐藏字段,在您的情况下为 blabla.php:

You can add two hidden fields in the form on the first target site, blabla.php in your case:

<form ...>
  <input type="hidden" name="name" value="<?php echo htmlspecialchars($_GET['name']);?>">
  <input type="hidden" name="lName" value="<?php echo htmlspecialchars($_GET['lName']);?>">

  <!-- rest of the form here -->
</form>

对于动态解决方案,请使用 foreach 循环:

For a dynamic solution, use a foreach loop:

<?php
foreach($_GET as $name => $value) {
  $name = htmlspecialchars($name);
  $value = htmlspecialchars($value);
  echo '<input type="hidden" name="'. $name .'" value="'. $value .'">';
}
?>

您可以考虑将动态方法锁定到已知可能键的列表:

You may consider locking the dynamic approach down to a list of known possible keys:

<?php
$keys = array('name', 'lName', ...);
foreach($keys as $name) {
  if(!isset($_GET[$name])) {
    continue;
  }
  $value = htmlspecialchars($_GET[$name]);
  $name = htmlspecialchars($name);
  echo '<input type="hidden" name="'. $name .'" value="'. $value .'">';
}
?>

这篇关于如何在表单提交时保留已设置的 GET 参数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17