2014-11-03 112 views
3

我需要此表單的操作來調用我的腳本,然後如果電子郵件地址已經在數據庫中,它應該顯示警報。提交後保留字段值

這個工程,但很明顯,我被指向我的空白腳本頁面,當我回到表單時,數據已經消失。

我希望被重定向到數據完整的窗體並顯示警報。

我已經嘗試了整天,讓會議工作,現在我只是困惑。

如果有人能告訴我什麼以及爲每個頁面添加會話代碼的位置,我會非常感激。

本頁面握住我的形式:

<!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"; 
    } 

} 
?> 

回答

1

是的,你可以實現(某種)在這種情況下,閃光燈會:

所以在提交:

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

} 
?> 
+0

非常好!這正是我需要的。非常感謝!! – Lori 2014-11-03 01:26:15

+0

@Lori很高興這有幫助 – Ghost 2014-11-03 01:28:12

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'; 
} 

... 
+4

您應該進一步擴展以使其成爲正確的答案,因爲它目前只是一條評論。 – Darren 2014-11-03 00:36:52

+0

謝謝,但我需要使用會話。 – Lori 2014-11-03 00:41:15

+0

這是一個選擇,或者你沒有告訴我們所有的東西!因爲我沒有看到這種情況下的興趣! – akmozo 2014-11-03 00:55:25

0

我知道你發佈的代碼,但我仍然;不確定工作流程。但是,我會給你一個「例如」,看看你是否至少可以用這個想法來運行。

鑑於你的形式,還提供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'); ?> 
<!-- ... -->