3

I have 2 forms for a shopping cart. When they go back I would like the input fields to be filled with info they originally put in before submitting.

1st uses basic form fields to submit billing and shipping info.
2nd is used for CC info. What I would like to do is in that second form have a BACK BTN that will populate all the fields from the first form with the data they entered.

I have a PHP variable $response with an array returning values, but I don't believe they are all included. It brings back a token from PayPal. I think that might be purposely for security reasons. Perhaps I can store them locally in some JS variables?

I tried the below with no results:

<input name="BILLTOFIRSTNAME" id="billtofirstname_id" value="<?php echo $response['BILLTOFIRSTNAME'];?>"/>
NewCodeMan
  • 227
  • 4
  • 16

2 Answers2

1

use this in your first page -

session_start();
$data = array("input1"=>"value1","input2"=>"value2");
// store any data in this array
$_SESSION['data'] = $data;

Now if user returns to this page -

if(isset($_SESSION['data'])){
// check if data is available
$value1 = $_SESSION['data']['input1'];
}
else{
$value1 = '';
}

In your input's html add this -

<input name="input1" value="<?php echo $value1?>" />
  • This is great also I will try it out in a bit. Thanks – NewCodeMan Aug 22 '17 at 13:36
  • Hey @Devanshu could you elaborate a bit on this. It is returning a value of "value1" & "value2" - But I would have thought your code $value1 = $_SESSION['data']['BILLTOFIRSTNAME']; would replace it. SO I guess 1st prob is how to make it not put "value1" in as initial val and 2nd why would it not replace? Does it matter where in the code I place the PHP I.E. Before FORM, inside, or after? Thanks – NewCodeMan Aug 22 '17 at 16:12
1

Here you can use local storage variable

    <button onclick="store()" type="button">first form submit</button>

<script  type="text/javascript">
  function store(){
     var billtofirstname_id= document.getElementById("billtofirstname_id");
     localStorage.setItem("billtofirstname_id", billtofirstname_id.value);
    }
</script>



<button onclick="back()" type="button"> Second form back button</button>

<script  type="text/javascript">
  function back(){
       document.getElementById('Id').value=localStorage.getItem("billtofirstname_id");
    }
</script>
hiren207
  • 184
  • 6
  • Excellent method. I will give this a shot and PHP method @Devanshu mentioned. Thanks – NewCodeMan Aug 22 '17 at 13:35
  • I am new to this. What would be a good way to store all the fields like lastname, email, etc in an array or object to use with your above code? Thanks – NewCodeMan Aug 22 '17 at 16:29
  • Both are easy and good, for more help please look over this https://stackoverflow.com/questions/19952403/save-form-using-localstorage-html5 – hiren207 Aug 23 '17 at 04:44