gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:44:51 25 4
gpt4 key购买 nike

我有一个提交数据的表单,我正在为 session 变量使用以下代码

    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,我如何将数据保存到 session 变量并重新填充它?

最佳答案

我冒昧地稍微改进了您的代码。

看看下面的代码片段。这会将您的表单数据添加到 session 中,并在页面刷新时保持可用。

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

关于javascript - 提交前将表单数据保存到 SESSION 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54574058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com