Linked Questions

Popular Questions

Save form data to SESSION variables before submit

Asked by At

I have a form to submit data and I am using the following code for session variables

if (isset($_SESSION['product'])) {
    $product = $_SESSION['product'];
    if($product == "loan"){$loan='selected="selected"';} else {$loan='';}
    if($product == "card"){$card='selected="selected"';} else {$card='';}
    if($product == "savings"){$savings='selected="selected"';} else {$savings='';}
    if($product == "morgage"){$mortgage='selected="selected"';} else {$mortgage='';}
} else { $loan = $card = $savings = $mortgage = '';}
if (isset($_SESSION['liked'])) {
    $liked = stripslashes(htmlspecialchars(($_SESSION['liked'])));
} else {
    $liked = '';
if (isset($_SESSION['disliked'])) {
    $disliked = stripslashes(htmlspecialchars(($_SESSION['disliked'])));
} else {
    $disliked = '';

The form is something like this

        <form id="msform" class="form-horizontal" method="POST" action="">
        <label for="product">Select product:</label>
          <select name="product" id="product">
              <option value="loan" '. $loan .'>Personal Loan</option>
              <option value="card" '. $card .'>Credit Card</option>
              <option value="savings" '. $savings .'>Savings Account</option>
              <option value="mortgage" '. $mortgage .'>Mortgage</option>
        <textarea rows="4" name="liked" placeholder="I really liked...">'. $liked .'</textarea>
        <textarea rows="4" name="disliked" placeholder="I did not like...">'. $disliked .'</textarea>

        <!-- php if statements here, if logged in show submit button if not show Facebook login button -->

        <input type="submit" name="submit" class="submit action-button" value="Submit"/>

The submit button is shown only to logged in users. If the user is not logged in a Facebook login button is shown. When he clicks on that he is sent to Facebook where if he accepts permissions of the app he is redirected back to the form as a logged in user. But then, all fields have been cleared. How can I save data to the session variables and repopulate it, if the GET/POST is not called yet?

Related Questions