$ _SESSION变量和AJAX表单 - 变量更新问题

时间:2013-03-17 22:44:12

标签: php ajax session session-variables

我四处搜索,找不到这个具体问题的答案。我有一个联系表单,我将其作为RSVP表单使用,我将基于本教程:http://net.tutsplus.com/tutorials/html-css-techniques/build-a-neat-html5-powered-contact-form/

该表单上的操作转到PHP脚本,该脚本将数据写入数据库并发送一些电子邮件。它是通过AJAX完成的。数据库交互和电子邮件工作正常。

我正在使用$ _SESSION在数据发布后将变量返回到表单中,这样我就可以检查错误并使用条件。问题是,当我点击提交按钮第二次时,这个会话数据似乎才可用。

我会试着简洁地解释这个问题:

  1. 我从rsvp的下拉菜单中选择一个值,然后按提交。当我测试时,我存储在$ _SESSION变量中的那个选择的值不会被回显。
  2. 我从同一下拉菜单中选择不同值,然后再次按提交。 FIRST 值从$ _SESSION变量回显。
  3. 这是我的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>
    

    很抱歉这里有大量的代码。任何想法你将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

你假设你总是得到一个贴出的值,但情况并非总是如此。

确保检查实际发布的内容:

$firstname = isset($_POST['firstname']) ? $_POST['firstname'] : false;
$lastname = isset($_POST['lastname']) ? $_POST['lastname'] : false;
// And so on.

现在,您可以这样做:

$noLastname = "Last name not found.";
if(!($name)){ $_SESSION['errorMessage'] .= $noLastname; }else{ $_SESSION['lastname'] = $lastname; }

所以我认为你通过构造那个数组来过度复杂你的代码,只需要处理单个会话,你可能会在其中存储一些空值,这是毫无意义的。

如何使用它的示例:

<input type="text" id="firstname" name="firstname" value="<?php if(isset($_SESSION['firstname'])){echo $_SESSION['firstname'];}else{echo '';}?>" placeholder="First Name" required autofocus />

终止阵列,确保您实际使用的是现有数据,依此类推。你会看到光。