0

I have a PHP webpage with basic HTML, which includes form, where user can enter some data. The form can be filled in by user directly, but can also be auto-filled via GET method, returned by one of the actions which are called from original PHP webpage.

Problem is, after the form gets auto-filled, if any other action is called on the webpage (external php pages; non of those actions actually result in leaving the webpage), the form data gets deleted and user is left with blank fields.

I would like to have data in the form saved and always displayed once entered. I tried using $_SESSION after variables arrive via GET method to save array of values, but it is not working for me:

<?php if (isset($_GET['decrypted'])) : ?>
<?php session_start(); 
$_SESSION['saved'] = array();
// Add items based on item ID
$_SESSION['saved'][$decrypt_data] = array('s_id' => $_GET['s_id'], 'u_id' => $_GET['u_id'], 'l_id' => $_GET['l_id'], 'cu_id' => $_GET['cu_id']);
?>

...

 <form action="./encrypt.php">
 <p>2) Select options:</p>
 <input type="radio" name="mode" value="one">one<br>
 <input type="radio" name="mode" value="two">two<br>
 <p></p>
 s ID: <br><input type="text" name="s_id" value="<?php echo $_SESSION['saved'][decrypt_data]['s_id'] ?>"><br> 
 u ID: <br><input type="text" name="u_id" value="<?php echo $_SESSION['saved'][decrypt_data]['u_id'] ?>"><br> 
 l ID: <br><input type="text" name="l_id" value="<?php echo $_SESSION['saved'][decrypt_data]['l_id'] ?>"><br>
 cu ID: <br><input type="text" name="cu_id" value="<?php echo $_SESSION['saved'][decrypt_data]['cu_id'] ?>"><br>
 <p></p>
 <input type="submit" value="Do it!">
 </form>

FULL CODE:

<html lang="en">
<head>
<Title>WEBPAGE</Title>
</head>
<body>
 <h1>DECRYPT</h1>
 <form action="webpage.php" method="post" enctype="multipart/form-data">
 <p>
 <label for="my_upload">1) Select a file to upload:</label>
 <input id="my_upload" name="my_upload" type="file">
 </p>
 <input type="submit" value="Upload">
 </form>
 <?php if (isset($_GET['uploaded'])) : ?>
 <span style="background-color:#29a329"> <font color="white">File successfully uploaded!</font></span>
 <?php endif; ?>
 <form action="./decrypt.php">
 <p>2) Select decrypt mode:</p>
 <input type="radio" name="mode" value="one">one<br>
 <input type="radio" name="mode" value="two">two<br> 
 <p></p>
 <input type="submit" value="Decrypt">
 </form>
 <?php if (isset($_GET['decrypted'])) : ?>
 <?php session_start(); 
 $_SESSION['saved'] = array();
 // Add items based on item ID
 $_SESSION['saved'][$decrypt_data] = array('s_id' => $_GET['s_id'], 'u_id' => $_GET['u_id'], 'l_id' => $_GET['l_id'], 'cu_id' => $_GET['cu_id']);
 ?>
 <span style="background-color:#29a329"> <font color="white">File successfully decrypted!</font></span>
 <p></p>
 <form method="get" action="/decrypted_downloaded/<?php echo $_GET['final_name'] ?>">
 <button type="submit">Download decrypted file</button>
 </form>
 <?php endif; ?>
 <hr>
 <h1>ENCRYPT normal file</h1>
 <form action="webpage.php" method="post" enctype="multipart/form-data">
 <p>
 <label for="my_upload2">1) Select a file to upload:</label>
 <input id="my_upload2" name="my_upload2" type="file">
 </p>
 <input type="submit" value="Upload">
 </form>
 <?php if (isset($_GET['uploaded_bin'])) : ?>
 <span style="background-color:#29a329"> <font color="white">File successfully uploaded!</font></span>
 <?php endif; ?>
 <form action="./encrypt.php">
 <p>2) Select encrypt options:</p>
 <input type="radio" name="mode" value="one">one<br>
 <input type="radio" name="mode" value="two">two<br>
 <p></p>
 s ID: <br><input type="text" name="s_id" value="<?php echo $_SESSION['saved'][decrypt_data]['s_id'] ?>"><br> 
 u ID: <br><input type="text" name="u_id" value="<?php echo $_SESSION['saved'][decrypt_data]['u_id'] ?>"><br> 
 l ID: <br><input type="text" name="l_id" value="<?php echo $_SESSION['saved'][decrypt_data]['l_id'] ?>"><br>
 cu ID: <br><input type="text" name="cu_id" value="<?php echo $_SESSION['saved'][decrypt_data]['cu_id'] ?>"><br>
 <p></p>
 <input type="submit" value="Encrypt">
 </form>
 <?php if (isset($_GET['encrypted'])) : ?>
 <span style="background-color:#29a329"> <font color="white">File successfully encrypted!</font></span>
 <p></p>
 <form method="get" action="/encrypted_downloaded/<?php echo $_GET['final_name_webpage'] ?>">
 <button type="submit">Download encrypted file</button>
 </form>
 <?php endif; ?>
</body>
</html>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{
 if (is_uploaded_file($_FILES['my_upload']['tmp_name'])) 
 { 
 //delete folder contents
 $files = glob('./encrypted_uploaded/*'); // get all file names
 foreach($files as $file){ // iterate files
 if(is_file($file))
 unlink($file); // delete file
 }
 //Validate the file name
 if(empty($_FILES['my_upload']['name']))
 {
 echo " File name is empty! ";
 exit;
 }
 $upload_file_name = $_FILES['my_upload']['name'];
 //Too long file name?
 if(strlen ($upload_file_name)>150)
 {
 echo " too long file name ";
 exit;
 }
 //set a limit to the file upload size
 if ($_FILES['my_upload']['size'] > 10000000) 
 {
 echo " too big file ";
 exit; 
 }
 //Save the file
 $dest=__DIR__.'/encrypted_uploaded/'.$upload_file_name;
 if (move_uploaded_file($_FILES['my_upload']['tmp_name'], $dest)) 
 {
 header('Location: webpage.php?uploaded=yes'); 
 }
 }
 if (is_uploaded_file($_FILES['my_upload2']['tmp_name'])) 
 { 
 echo " test ";
 //delete folder contents
 $files = glob('./decrypted_uploaded/*'); // get all file names
 foreach($files as $file){ // iterate files
 if(is_file($file))
 unlink($file); // delete file
 }
 //Validate the file name
 if(empty($_FILES['my_upload2']['name']))
 {
 echo " File name is empty! ";
 exit;
 }
 $upload_file_name = $_FILES['my_upload2']['name'];
 //Too long file name?
 if(strlen ($upload_file_name)>150)
 {
 echo " too long file name ";
 exit;
 }
 //set a limit to the file upload size
 if ($_FILES['my_upload2']['size'] > 10000000) 
 {
 echo " too big file ";
 exit; 
 }
 //Save the file
 $dest=__DIR__.'/decrypted_uploaded/'.$upload_file_name;
 if (move_uploaded_file($_FILES['my_upload2']['tmp_name'], $dest)) 
 {
 header('Location: webpage.php?uploaded_bin=yes');
 }
 }
}
asked Nov 17, 2019 at 10:19
6
  • Do you have any other forms on the page? Also, doesn't [decrypt_data] give you an error? Commented Nov 17, 2019 at 10:33
  • Yes, there are 4 forms on the webpage, but this is the only one with text fields. No, [decrypt_data], does not give me an error. Commented Nov 17, 2019 at 10:39
  • This might be a long shot, but do you update the other forms by these other actions? If so, how are you updating those? Commented Nov 17, 2019 at 10:41
  • No, I am not updating other forms. I am only displaying certain messages or buttons via HTML when I get certain GET responses. Commented Nov 17, 2019 at 10:46
  • Are any of these buttons inside the forms? How do you select the button to update? Because I do not see classes or ids in forms or form fields. Are you using name or type attributes to select? I am asking this because I am thinking this is happening because you might have forgotten to properly target the element... Commented Nov 17, 2019 at 10:58

2 Answers 2

1

You save data in $_SESSION['saved'][$decrypt_data] But you read $_SESSION['saved'][decrypt_data]['l_id'] Add the $ sign to decrypt_data when you read data

answered Nov 17, 2019 at 13:45
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that does help, data gets displayed now after GET parameters are received, but as soon as another action is called, data gets erased from text fields ... :/
Looks like my data in "decrypt_data" is lost, maybe session is lost? I don't know why ...
OK, your answer was correct for my specific question, the "$" was missing. I had further problems with session, which I solved by putting <?php session_start(); ?> directly after html tag. Thanks!
0

You must use the Post Method in Html form; and get with the Post method;

answered Nov 17, 2019 at 10:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.