提交前将表单数据保存到SESSION变量

时间:2019-02-07 13:06:30

标签: javascript php forms 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 = '';
}

表单是这样的

        <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"/>

仅对登录用户显示“提交”按钮。如果用户未登录,则显示Facebook登录按钮。当他单击该链接时,他将被发送到Facebook,如果他接受该应用程序的权限,他将以登录用户的身份重定向回表单。但随后,所有字段均已清除。如果尚未调用GET / POST,如何将数据保存到会话变量中并重新填充?

2 个答案:

答案 0 :(得分:3)

我已经自由地对代码进行了一些改进。

看看下面的代码片段。这样会将您的表单数据添加到会话中,即使刷新页面也保持可用状态。

<?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>

答案 1 :(得分:0)

据我了解,您想将FormData存储在内存中,以便以后用于重新填充字段。为此,最好使用LocalStorage而不是SessionStorage。此处记录了LocalStorage API。

https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage