user3812411
user3812411

Reputation: 352

keeping php values in the page after redirecting

i did this little website to convert values to md5. but i want to save all the words entered in my database to create a sort of dictionary that i could use later. when the user clicks on the convert button the page redirects to insert the data in the table and when it redirects back to the index page the md5 value is gone and the user has to click the back button to see it. how can i make the value come back in the text box after the page's redirection?

Upvotes: 1

Views: 991

Answers (2)

ffflabs
ffflabs

Reputation: 17481

You have several methods to do this

  • Keep the original string and md5 hash in a session variable. It will persist even after redirects.

  • Instead of using the process.php, use the index.php as the target for your form. You can check the $_POST superglobal to see if you are loading the page for the first time, or displaying previously sent values.

  • Put the values passed to process.php in the query string of the redirection url, so instead of

    header("Location: index.php");

you do

header("Location: index.php?string=$mystring&md5=$mymd5");

Be careful though, you need to sanitize $mystring or your form would be an easy XSS bait. Thanks @machineaddict.

  • Since you are generating the md5 hash on the front, use js and ajax to send values to process.php without ever leaving the index. This one is the most elegant if you know your way around simple js libraries like jQuery.

Upvotes: 1

machineaddict
machineaddict

Reputation: 3236

This is just to understand one method of doing it:

In your process.php file:

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['mott'])) {
    $value = $_POST['mott'];
    $md5 = md5($value);

    // code to insert the value and the md5 in database. remember to escape the string

    //redirect the user
    header('Location: showmd5.php?md5=' . $md5);
    exit;
}

The showmd5.php file:

<?php

$md5 = isset($_GET['md5']) ? $_GET['md5'] : '';

// check this md5 in database and show the value

Upvotes: 2

Related Questions