提交后保留字段值

时间:2014-11-03 00:33:07

标签: php html mysql

我需要此表单的操作来调用我的脚本,然后如果电子邮件地址已经在数据库中,它应该显示警报。

这很有效,但显然我已经转到了我的空白脚本页面,当我返回到表单时,数据就消失了。

我希望重定向到数据完整的表单并显示警报。

我整天都试着让课程上班,现在我只是感到困惑。

如果有人可以告诉我每个页面添加会话代码的内容和位置,我会非常感激。

此页面包含我的表单:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Untitled Document</title>

    <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js'></script>
    <script src='http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js'></script>
    <script src='http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js'></script>

    <script src="../js/register_validate.js" type="text/javascript"></script>

    <link href="../css/styles.css" rel="stylesheet" type="text/css">

    <base target="_top">
</head>

<body>

    <p><?php include ('register_form2.php') ?></p>                    

</body>
</html>

这是包含的表单页面:

<form action="register_script2.php" method="POST" name="form_register" id="form_registerID" accept-charset="UTF-8">

    <aside class="field_reg_form">

    <input name="field_email1" type="text" required id="field_email1ID" /><br /><br />
    <input name="field_email2" type="text" required id="field_email2ID" /><br /><br />
    <input type="submit" value="submit" id="submit" name="submit" />

    </aside>


</form>

这是PHP脚本:

<?php

require_once('../scripts/connect.php');

$con = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME) or die('Could not connect to database server.');

if(isset($_POST['submit'])) {

    $var_Email1 = mysqli_real_escape_string($con, $_POST['field_email1']);
    $var_Email2 = mysqli_real_escape_string($con, $_POST['field_email2']);

        if ($var_Email1 == $var_Email2){

            $sql = mysqli_query($con, "SELECT * FROM membership WHERE Email = '$var_Email1' "); 

                if(mysqli_num_rows($sql) > 0 ){
                    print '<script type="text/javascript">'; 
                    print 'alert("The email address '. $_POST['field_email1'].' is already in our database")'; 
                    print '</script>'; 
                      exit();
                }

        echo "not in database";
   }

}
?>

3 个答案:

答案 0 :(得分:1)

您可以使用ajax发送数据,然后显示php脚本的返回内容,如下所示:

$.ajax({
    type: 'POST',
    url: 'register_script2.php',
    data: $('form#form_register').serialize()
})
.done(function(msg) {
      if(msg != 'error'){
          alert('The email address '+msg+' is already in our database');
      } else {
          // init your form
      }
});

PHP代码中执行此操作;

...

if(mysqli_num_rows($sql) > 0 ){
    echo $var_Email1;
} else {
    echo 'error';
}

...

答案 1 :(得分:1)

是的,你可以在这种情况下实现(某种)flash会话:

提交时:

<?php

session_start(); // don't forget
require_once('../scripts/connect.php');

$con = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME) or die('Could not connect to database server.');

if(isset($_POST['submit'])) {

    $var_Email1 = mysqli_real_escape_string($con, $_POST['field_email1']);
    $var_Email2 = mysqli_real_escape_string($con, $_POST['field_email2']);

    if ($var_Email1 == $var_Email2){

        $sql = mysqli_query($con, "SELECT * FROM membership WHERE Email = '$var_Email1' "); 
        if(mysqli_num_rows($sql) > 0){
            // set session
            $_SESSION['email_exists'] = $var_Email1;
            header('Location: the_starting_php.php');
            exit;
        }
    }

    echo "not in database";

}
?>

然后在表单页面中添加以下内容:

<?php
session_start();

// check if there is
$email = '';
if(isset($_SESSION['email_exists'])) {
    $email = $_SESSION['email_exists'];
    unset($_SESSION['email_exists']); // unset it
    echo "
      <script type='text/javascript'>
      alert('The email address $email already exists');
      </script>
    ";
}

?>


<form action="register_script2.php" method="POST" name="form_register" id="form_registerID" accept-charset="UTF-8">

    <aside class="field_reg_form">
          <input name="field_email1" type="text" required id="field_email1ID" value="<?php echo $email; ?>" />
          <br /><br />
          <input name="field_email2" type="text" required id="field_email2ID" value="<?php echo $email; ?>" />
          <br /><br />
          <input type="submit" value="submit" id="submit" name="submit" />
    </aside>

</form>

旁注:我建议使用准备好的陈述。

<?php

session_start(); // don't forget
require_once('../scripts/connect.php');

$con = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME) or die('Could not connect to database server.');

if(isset($_POST['submit'])) {

    $var_Email1 = $_POST['field_email1'];
    $var_Email2 = $_POST['field_email2'];

    if ($var_Email1 == $var_Email2){

        $sql = 'SELECT * FROM membership WHERE Email = ?'; 
        $select = $con->prepare($sql);
        $select->bind_param('s', $var_Email1);
        $select->execute();
        if($select->num_rows > 0){
            // set session
            $_SESSION['email_exists'] = $var_Email1;
            header('Location: the_starting_php.php');
            exit;
        }
    }

    echo "not in database";

}
?>

答案 2 :(得分:0)

我知道你发了代码,但我还是;不确定工作流程。但是,我会给你一个&#34;例如&#34;并且看看你是否至少可以接受这个想法。

根据您的表单,还提供value属性并从$_POST值中提取它们:

<强> register_form.php

<!-- keeping it brief... -->
<form method="POST">
  <input type="email" name="email1" value="<?= $_POST['email1']; ?>" />
  <input type="email" name="email2" value="<?= $_POST['email2']; ?>" />
  <input type="submit" value />
</form>

然后在原始请求中输出:

<!-- ... -->
<?php include('register_form.php'); ?>
<!-- ... -->

然后,在您提交的页面中也会调用它(这次它将由传入的值预先填充):

<!-- ... -->
<?php if (isset($_POST['submit'])) { ... } ?>
<!-- ... -->
<?php include('register_form.php'); ?>
<!-- ... -->