Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

php - How to keep already-set GET parameter values on form submission?

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.

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).

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

How can I keep them?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

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 .'">';
}
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...