Home > Net >  How do I preserve parameters in PHP form code
How do I preserve parameters in PHP form code

Time:02-28

I have a script which is run with a parameter (e.g. details.php?studentid=10325)

On this script I have a form with the following form code so that the form data is sent to the current script but what's happening is that the script is run without the parameter. How do I preserve the parameter in this form code?

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

CodePudding user response:

Not really recommended, but you could add a hidden <input> tag with the studentid so it is sent back when you submit the form. Like so :

<input type="hidden" name="studentid" value="<?= $_GET['studentid'] ?>">

CodePudding user response:

Leave the action blank, and the form will submit back to the current url

<form method="post" action="">

Keep in mind you have specified post method, so the form values will come thru in $_POST, whereas your studentid on the query string will be in $_GET['studentid'], you can work around that by using $_REQUEST['studentid'] instead, but make sure you don't have a field in the form also called studentid

  • Related