0

我有一个名为“delete-wish-form.php”的 PHP 文件,其表单包含以下字段:

  • wishId(隐藏字段) 该字段的值应等于通过 URL 传入的 GET 变量。所以我的问题是如果 URL 是 delete-wish-form.php?wishId=1 那么我希望我的隐藏字段看起来像这样:

代码:

<!DOCTYPE HTML>  
<html>
<title>Delete Wishes</title>
<body>  
<h2>Wishes Form</h2>

<?php

 $wishId = $_GET['wishId'];
 
 ?>

<form method="get" action="process-delete-wish-form"> 
  
  <input type="hidden" name="wishId">
  
  <input type="submit" name="submit" value="Submit"> 
  
</form>

</body>
</html>
4

1 回答 1

0

您应该检查是否在您的 url 参数中定义了 wishId 以避免在表单中抛出错误,并使用htmlspecialchars来避免对 GET 参数的 xss 攻击

<?php
  
  $wishId = '';

  if(isset($_GET['wishId'])) {

    $wishId = htmlspecialchars($_GET['wishId'],ENT_QUOTES);

  }

?>

然后在您的输入隐藏字段中

<input type="hidden" name="wishId" value="<?php echo $wishId;?>">
于 2020-11-19T03:53:21.183 回答