2017-08-23 25 views
0

我使用模式框中的窗體將值插入到數據庫中,除彈出成功外,它都可以正常工作。

查詢獲取funtions.php頁面

//create account 
if (isset($_POST['btn-signup'])){ 
    $firstname = test_input($_POST['firstname']); 
    $email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL); 
    $pass = test_input($_POST['password']); 
    $cpass = test_input($_POST['confirm_password']); 

    if ($pass == $cpass){ 
     $password = password_hash($pass, PASSWORD_DEFAULT); 

     $signupQuery = "INSERT INTO draadlozeAccount (firstname, email, pass) VALUES 
     ('$firstname','$email','$password')"; 
     $signupResult = $conn->query($signupQuery); 
     if (!$signupResult) { 
      echo $conn->error; 
      $conn->close(); 

     } else { 
      $success = '<div class="notification pos-right pos-bottom col-sm-4 col-md-3" data-animation="from-bottom" data-notification-link="trigger"> 
           <div class="boxed boxed--border border--round box-shadow"> 
            <img alt="avatar" class="image--sm" src="img/logo.png" /> 
            <div class="text-block"> 
             <h5>Success</h5> 
             <p> 
              Your account has been created successfully. You will receive an email soon. 
             </p> 
            </div> 
           </div> 
          </div>'; 
     } 
    } 

} 

然後在index.php文件上運行我回聲出成功

    <?php 
        if (isset($_POST['btn-signup'])) { 
         echo $success; 
        } 
        ?> 

我的問題是,爲什麼不來了我成功的窗口?我在想什麼

回答

2

我相信你正在嘗試做的是,在你的function.php文件

$_SESSION['success'] = "<div ...... "; 

而在你index.php

if (isset($_SESSION['success']){ 
    echo $_SESSION['success']; 
} 

但我建議像你function.php

$_SESSION['success'] = true; 

並在您的index.php

個比 success其他
<?php 
if(isset($_SESSION['success']){ 
    ?> 
    <div ............... your html ...... /> 
    <?php 
} 

使用特定的名稱,如signupsuccess

+0

謝謝你的隊友,你的答案幫助我們調整了我的代碼,它現在都在工作。 –

0

你知道變量作用域嗎?變量僅限於當前頁面。您可以使用會話變量來維護狀態管理。會話變量僅限於當前會話。

} else { 
     $_SESSION['success'] = '<div class="notification pos-right pos-bottom col-sm-4 col-md-3" data-animation="from-bottom" data-notification-link="trigger"> 
          <div class="boxed boxed--border border--round box-shadow"> 
           <img alt="avatar" class="image--sm" src="img/logo.png" /> 
           <div class="text-block"> 
            <h5>Success</h5> 
            <p> 
             Your account has been created successfully. You will receive an email soon. 
            </p> 
           </div> 
          </div> 
         </div>'; 
    } 

,並使用它像這樣

echo $_SESSION['success']; 
+0

你好,是的,我明白這一點,但是這並沒有什麼差別。我的表單仍然提交併發佈了數據,但沒有通知顯示 –

+0

做了調試bro,使用die()或exit()函數來檢查代碼流。你是開發者,你不能問這樣的問題。 –

+0

問題都是關於學習夥伴。提出問題幫助我理清了這個問題。 –

0

從我所看到的,你要打印在另一個文件中,這是不可能的變量集。在腳本結尾處,所有聲明都被燒燬,用戶瀏覽頁面所調用腳本的任何內容都不可用。

紅外只有三種方式可以讓你的數據在接下來的頁面上可用的STIL:

  • 堅持它在你的數據庫,然後在以後檢索。
  • 將它發送到HTTP請求的頁面作爲GET或POST。
  • 使用會話並將$success作爲會話變量存儲在$_SESSION中。
+0

我在頂部開始會話,然後在索引文件上回顯$ _SESSION ['success'],但它就像模型從不觸發,我已將表單代碼添加到問題 –

+0

您的代碼顯示您分配'成功'在一個頁面上,並嘗試在另一個上打印'$ success'。沒有會話變量。我在這個問題中沒有看到你的表單代碼。 – ksjohn

0

您也可以將您的success-html-code-snippet放置在單獨的文件中,例如「success-snippet.inc.html」,然後在需要時使用「file_get_contents()」獲取內容。如果您喜歡上述的會話,則必須先開始會話。

0

,你是他需要一個表單或AJAX Ajax代碼:

$(document).ready(function(){ 
 
    $("#senden").click(function(){ 
 
\t \t var daten = $('#formular').serialize(); 
 
     $.ajax({ 
 
\t \t \t url: "test.php", 
 
\t \t \t type: 'post', 
 
\t \t \t data: daten, 
 
\t \t \t dataType: 'json', 
 
\t \t \t success: function(result){ 
 
\t \t \t \t $("#result").val(result.passwort); 
 
\t \t \t \t $('#test').html(result.test); 
 
\t \t \t }, 
 
\t \t \t error: function(xhr, status){ 
 
\t \t \t \t console.log('AJAX returned ' + status); 
 
\t \t \t } 
 
\t \t }); 
 
    }); 
 
});

你可以看你自己如何將其納入,並使用它。

相關問題