jurij
jurij

Reputation: 383

PHP/HTML: saving form data once entered

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');
    }
  }
}

Upvotes: 0

Views: 86

Answers (2)

CiroRa
CiroRa

Reputation: 510

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

Upvotes: 1

Basir Askari
Basir Askari

Reputation: 61

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

Upvotes: 0

Related Questions