programmer
programmer

Reputation: 167

Is there any way to re use form data from previous page?

I am sending data from page1.php to page2.php with the GET method. So in the URL, I am getting the values as:

http://mydomain.com/page2.php?value1=myname&value2=xyz123

Now, here, in page2.php I am adding another form that I want to send to page2.php (self) on submit with the format as follows:

http://mydomain.com/page2.php?value1=myname&value2=xyz123&value3=1234

What I want to do is to add the new value at the end of the previous values in the URL.

Is there any way I can reuse the data that I got from the previous page? I have tried the following code

<form method="get" action="'."http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'">

But it is sending value to http://mydomain.com/page2.php?value3=1234 rather to http://mydomain.com/page2.php?value1=myname&value2=xyz123&value3=1234

I would appreciate any help or suggestions.

Upvotes: 1

Views: 1040

Answers (2)

Happy
Happy

Reputation: 1855

You should use POST method (instead of GET) to avoid the password in the URL.

With GET, the data are shown into the URL. With POST, the data are included into the HTTP request but are not displayed into the URL. Using POST, the user think it's safer.


About your question, one way is to put in your form some hidden input:

<input type="hidden" name="uname" value="<?php echo $_GET['uname']; ?>" />
<input type="hidden" name="pass" value="<?php echo $_GET['pass']; ?>" />

But again, this is not a good idea because the password is visible in the source code.

An other way is to set a session variable like this:

<?php

$_SESSION['uname'] = $_GET['uname'];
$_SESSION['pass'] = $_GET['pass'];

?>

But again, it is not a good idea to store passwords into session array. If you want to avoid that, you can store a unique id per user into $_SESSION['id'].

In order to use sessions, you will need a call to session_start() before html (avoid any space).

You can find more information about sessions in the documentation.

Upvotes: 1

Mat&#237;as Insaurralde
Mat&#237;as Insaurralde

Reputation: 1222

You need to use hidden inputs on your form. So when page1 posts to page2, the generated HTML of page2 will contain those values. Later when page2 is submitted, it will send out the values on these hidden fields + the entered value for phone. Something like the following:

  <form>
    <input type="hidden" name="uname" value="<?= $uname ?>" />
    <input type="hidden" name="pass" value="<?= $pass ?>" />

    <input type="text" name="phone" value="" />
  </form>

Remember to escape HTML before putting it on the 's value attribute.

Upvotes: 0

Related Questions