I have a form to submit data and I am using the following code for session variables
if(!isset($_SESSION))
{
session_start();
}
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>
</select>
<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?
I've taken the liberty of improving your code a bit.
Take a look at the following code snippet. This will add your form data to a session and keep it available even if the page is refreshed.
<?php
// First, we'll check if the session is set. Otherwise, we'll set it
if (!isset($_SESSION)) {
session_start();
}
$fb = new Facebook\Facebook([/* . . . */]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes'];
$loginUrl = $helper->getLoginUrl($_SERVER['REQUEST_URI'], $permissions);
// Is the form submitted? Add the formData to our session
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$_SESSION['formData'] = $_POST;
unset($_POST);
// If the user is not logged in, redirect them to the facebook login
if (!$loggedIn) {
header("location: " . $loginUrl);
exit;
}
}
// Predefine our select dropdown values
$options = [
'loan' => 'Personal Loan',
'card' => 'Credit Card',
'savings' => 'Savings Account',
'mortgage' => 'Mortgage',
];
?>
<!-- Your form -->
<form id="msform" class="form-horizontal" method="POST" action="">
<label for="product">Select product:</label>
<select name="product" id="product">
<?php foreach ($options as $option => $optionValue): ?>
<option value="<?= $option; ?>" <?= isset($_SESSION['formData']['product']) && $_SESSION['formData']['product'] == $option ? 'selected="selected"' : ''; ?>><?= $optionValue; ?></option>
<?php endforeach; ?>
</select>
<textarea rows="4" name="liked" placeholder="I really liked..."><?= isset($_SESSION['formData']['liked'] ? $_SESSION['formData']['liked'] : ''; ?></textarea>
<textarea rows="4" name="disliked" placeholder="I did not like..."><?= isset($_SESSION['formData']['disliked']) ? $_SESSION['formData']['disliked'] : ''; ?></textarea>
<?php if ($loggedIn): ?>
<input type="submit" name="submit" class="submit action-button" value="Submit"/>
<?php else: ?>
<input type="submit" name="submit" class="submit action-button" value="Login with Facebook"/>
<?php endif; ?>
</form>