我四处搜寻,找不到这个特定问题的答案。我有一个联系表格,我将其用作基于本教程的 RSVP 表格:http: //net.tutsplus.com/tutorials/html-css-techniques/build-a-neat-html5-powered -联系表/
该表单上的操作转到一个 PHP 脚本,该脚本将数据写入数据库并发送一些电子邮件。它是通过 AJAX 完成的。数据库交互和电子邮件工作得很好。
发布数据后,我正在使用 $_SESSION 将变量带回表单,以便我可以检查错误并使用条件。问题是这个会话数据似乎只有在我第二次点击提交按钮时才可用。
我将尝试简洁地解释这个问题:
- 我从 rsvp 的下拉菜单中选择一个值,然后按提交。我存储在 $_SESSION 变量中的该选择的值在我测试时不会回显。
- 我从同一个下拉菜单中选择不同的值,然后再次按提交。FIRST值从该 $_SESSION 变量中回显。
这是我的 submit.php 文件。我已经遗漏了很多,但我认为这是重要的东西:
<?php session_start(); ?>
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
if(isset($_POST)) {
//form validation vars
$formok = true;
$errors = array();
//sumbission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('Y-m-d');
$time = date('H:i:s');
$timestamp = $date . " " . $time;
//form data
$firstname = mysql_prep($_POST['firstname']);
$lastname = mysql_prep($_POST['lastname']);
$dinner = mysql_prep($_POST['dinner']);
$reqsong = mysql_prep($_POST['reqsong']);
$email = mysql_prep($_POST['email']);
$rsvp = mysql_prep($_POST['rsvp']);
$message = mysql_prep($_POST['message']);
//what we need to return back to our form
$returndata = array(
'posted_form_data' => array(
'firstname' => $firstname,
'lastname' => $lastname,
'dinner' => $dinner,
'reqsong' => $reqsong,
'email' => $email,
'rsvp' => $rsvp,
'message' => $message
),
'form_ok' => $formok,
'errors' => $errors
);
然后这是我的 rsvp.php 表单:
<?php session_start(); ?>
<div id="contact-form" class="clearfix">
<?php
//init variables
$cf = array();
$sr = false;
if(isset($_SESSION['cf_returndata'])){
$cf = $_SESSION['cf_returndata'];
$sr = true;
}
?>
<ul id="errors" class="<?php echo ($sr && !$cf['form_ok']) ? 'visible' : ''; ?>">
<li id="info">There were some problems with your form submission:</li>
<?php
if(isset($cf['errors']) && count($cf['errors']) > 0) :
foreach($cf['errors'] as $error) :
?>
<li><?php echo $error ?></li>
<?php
endforeach;
endif;
?>
</ul>
<?php if($cf['posted_form_data']['rsvp'] == "Will Attend"): ?>
<p id="success" class="<?php echo ($sr && $cf['form_ok']) ? 'visible' : ''; ?>">Thank you for your RSVP! <br> You will receive an email with additional information soon. <br><br> If your invitation was addressed to more than one person, please enter their information and submit again. <br><br> We look forward to seeing you soon!</p>
<?php endif; ?>
<?php if($cf['posted_form_data']['rsvp'] == "Will Not Attend"): ?>
<p id="success" class="<?php echo ($sr && $cf['form_ok']) ? 'visible' : ''; ?>">Too bad you can't make it.</p>
<?php endif; ?>
<form method="post" action="submit.php">
<input type="text" id="firstname" name="firstname" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['firstname'] : '' ?>" placeholder="First Name" required autofocus />
<input type="text" id="lastname" name="lastname" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['lastname'] : '' ?>" placeholder="Last Name" required autofocus />
<input type="email" id="email" name="email" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['email'] : '' ?>" placeholder="Email Address" required />
<select id="rsvp" name="rsvp">
<option value="Will Attend">Will Attend</option>
<option value="Will Not Attend">Will Not Attend</option>
</select>
<select id="dinner" name="dinner">
<option value="select">Dinner Selection</option>
<option value="Chicken" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['dinner'] == 'Chicken') ? "selected='selected'" : '' ?>>Chicken</option>
<option value="Pork" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['dinner'] == 'Pork Fillet') ? "selected='selected'" : '' ?>>Pork</option>
</select>
<input type="reqsong" id="reqsong" name="reqsong" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['reqsong'] : '' ?>" placeholder="Request a Song to be Played!"/>
<textarea id="message" name="message" placeholder="Leave us a message if you like! If you have any allergies or dietary needs, please indicate them here."><?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['message'] : '' ?></textarea>
<span id="loading"></span>
<input type="submit" value="Submit" id="submit-button" />
</form>
<?php unset($_SESSION['cf_returndata']); ?>
</div>
抱歉这里有大量代码。您的任何想法将不胜感激。谢谢!